Skip to content

Commit 7854e26

Browse files
Joanna Kotułaguenhter
authored andcommitted
resolved: #420 read some variables from config.toml
1 parent 2f16503 commit 7854e26

File tree

9 files changed

+72
-14
lines changed

9 files changed

+72
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ansible.cfg
22
.idea
33
.vscode
4+
*.pyc

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ Role Variables
3737
- `gitlab_runner_config_update_mode` - Defines how configuration updates are applied:
3838
- Set to `by_config_toml` (default) to apply configuration changes directly by updating the `config.toml` file.
3939
- Set to `by_registering` if changes should be applied by unregistering and re-registering the runner when configuration changes.
40-
- Set to `by_template` if changes for all runners should directly be written in the `config.toml` file. The difference with `by_config_toml` is that this method is faster, but does not take into account the existing configuration of the runners. You may also have to provide additional configuration options for each runner, such as `id` and `token_obtained_at`.
40+
- Set to `by_template` if changes for all runners should directly be written in the `config.toml` file. This method is faster than `by_config_toml`. From the original `config.toml` it reads only 3 fields per runner: `id`, `token_obtained_at` and `token_expires_at`.
41+
All other content from the file is ignored
4142
- `gitlab_unregister_runner_executors_which_are_not_longer_configured` - Set to `true` if executors should be unregistered from a runner when they are no longer configured in Ansible. Default: `false`.
4243

4344
See the [defaults/main.yml](https://github.com/riemers/ansible-gitlab-runner/blob/master/defaults/main.yml) file for a list of all possible options that can be passed to a runner registration command.

filter_plugins/regex_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def first_or_value(value, default=None):
2+
"""Return: the first element when value is list or tuple, otherwise value (or default when value is None or empty list/tuple)"""
3+
if value is None:
4+
return default
5+
if isinstance(value, (list, tuple)):
6+
return value[0] if value else default
7+
return value
8+
9+
class FilterModule(object):
10+
def filters(self):
11+
return {
12+
'first_or_value': first_or_value,
13+
}

tasks/main-container.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@
4747
when: gitlab_runner_config_update_mode == 'by_config_toml'
4848

4949
- name: (Container) Configure Gitlab Runner via template
50-
ansible.builtin.template:
51-
src: config.toml.j2
52-
dest: "{{ gitlab_runner_config_file }}"
53-
mode: "0600"
50+
ansible.builtin.include_tasks: template_config/main.yml
5451
when: gitlab_runner_config_update_mode == 'by_template'
5552

5653
- name: (Container) Start the container

tasks/main-unix.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@
5050
when: gitlab_runner_config_update_mode == 'by_config_toml'
5151

5252
- name: (Unix) Configure Gitlab Runner via template
53-
ansible.builtin.template:
54-
src: config.toml.j2
55-
dest: "{{ gitlab_runner_config_file }}"
56-
mode: "0600"
53+
# become cannot be used with include_task
54+
block:
55+
- ansible.builtin.include_tasks: template_config/main.yml
5756
when: gitlab_runner_config_update_mode == 'by_template'
5857
become: "{{ gitlab_runner_system_mode }}"

tasks/main-windows.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,5 @@
4343
when: gitlab_runner_windows_start_runner
4444

4545
- name: (Windows) Configure Gitlab Runner via template
46-
ansible.builtin.template:
47-
src: config.toml.j2
48-
dest: "{{ gitlab_runner_config_file }}"
49-
mode: "0600"
46+
ansible.builtin.include_tasks: template_config/main.yml
5047
when: gitlab_runner_config_update_mode == 'by_template'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
- name: "(Update config: template) Extract runner name from config.toml"
2+
set_fact:
3+
runner_name: "{{ current_runner | regex_search('name = \"([^\"]*)\"', '\\1') | first_or_value | trim }}"
4+
5+
- name: "(Update config: template) Load runner values from config.toml"
6+
block:
7+
- name: "(Update config: template) Set variables for runner {{ runner_name }}"
8+
set_fact:
9+
runner_id: "{{ current_runner | regex_search('id = (.*)', '\\1') | first_or_value | int }}"
10+
token_obtained_at: "{{ current_runner | regex_search('token_obtained_at = (.*)', '\\1') | first_or_value }}"
11+
token_expires_at: "{{ current_runner | regex_search('token_expires_at = (.*)', '\\1') | first_or_value }}"
12+
- name: "(Update config: template) Update existing data for runner {{ runner_name }}"
13+
set_fact:
14+
existing_runners: "{{ existing_runners | default({}) | combine({runner_name: {'id': runner_id, 'token_obtained_at': token_obtained_at, 'token_expires_at': token_expires_at} }) }}"
15+
when:
16+
- runner_name | default(False)
17+
- runner_name != 'None'

tasks/template_config/main.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
- name: '(Update config: template) Prepare configuration update'
3+
# Using template needs some data from registration
4+
block:
5+
- name: '(Update config: template) Read config.toml file'
6+
ansible.builtin.slurp:
7+
src: "{{ runner_config_file_path }}"
8+
register: runner_config_file
9+
10+
- name: '(Update config: template) Retrieve runners configuration content'
11+
ansible.builtin.set_fact:
12+
current_runners_configs: "{{ (runner_config_file['content'] | b64decode).split('[[runners]]\n') }}"
13+
14+
- name: "(Update config: template) Load existing runner fields from config.toml"
15+
include_tasks: load_existing_fields.yml
16+
loop: "{{ current_runners_configs }}"
17+
loop_control:
18+
loop_var: current_runner
19+
label: "{{ current_runner[:20] }}" # show only fragment to avoid printing tokens
20+
21+
- name: "(Update config: template) Merge existing runner fields with current configuration"
22+
loop: "{{ gitlab_runner_runners }}"
23+
loop_control:
24+
loop_var: gitlab_runner
25+
label: "{{ gitlab_runner.name }}"
26+
set_fact:
27+
filled_runners: "{{ filled_runners | default([]) + [existing_runners[gitlab_runner.name] | combine(gitlab_runner)] }}"
28+
29+
- name: '(Update config: template) Apply GitLab Runner configuration template'
30+
ansible.builtin.template:
31+
src: config.toml.j2
32+
dest: "{{ gitlab_runner_config_file }}"
33+
mode: "0600"

templates/config.toml.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sentry_dsn = "{{ gitlab_runner_sentry_dsn }}"
2727
{% endif %}
2828
{% endif %}
2929

30-
{% for gitlab_runner in gitlab_runner_runners %}
30+
{% for gitlab_runner in filled_runners %}
3131
[[runners]]
3232
name = {{ gitlab_runner.name | tojson }}
3333
limit = {{ gitlab_runner.concurrent_specific | default(0) }}

0 commit comments

Comments
 (0)