Skip to content

Commit 0ccb281

Browse files
committed
Add support for configuring runner through template
Signed-off-by: Leonidas Avdelas <avdelasleonidas@gmail.com>
1 parent 72d34dd commit 0ccb281

File tree

5 files changed

+356
-0
lines changed

5 files changed

+356
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Role Variables
3333
- `gitlab_runner_config_update_mode` - Defines how configuration updates are applied:
3434
- Set to `by_config_toml` (default) to apply configuration changes directly by updating the `config.toml` file.
3535
- Set to `by_registering` if changes should be applied by unregistering and re-registering the runner when configuration changes.
36+
- 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.
3637
- `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`.
3738

3839
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.

tasks/main-container.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,20 @@
3939

4040
- name: (Container) Set global options
4141
ansible.builtin.import_tasks: global-setup.yml
42+
when: gitlab_runner_config_update_mode == 'by_config_toml' or
43+
gitlab_runner_config_update_mode == 'by_registering'
4244

4345
- name: (Container) Configure GitLab Runner
4446
ansible.builtin.include_tasks: config-runners-container.yml
4547
when: gitlab_runner_config_update_mode == 'by_config_toml'
4648

49+
- 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"
54+
when: gitlab_runner_config_update_mode == 'by_template'
55+
4756
- name: (Container) Start the container
4857
community.docker.docker_container:
4958
name: "{{ gitlab_runner_container_name }}"

tasks/main-unix.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@
4242

4343
- name: Set global options (macOS/Debian/RedHat)
4444
ansible.builtin.import_tasks: global-setup.yml
45+
when: gitlab_runner_config_update_mode == 'by_config_toml' or
46+
gitlab_runner_config_update_mode == 'by_registering'
4547

4648
- name: (Unix) Configure GitLab Runner
4749
ansible.builtin.include_tasks: config-runners.yml
4850
when: gitlab_runner_config_update_mode == 'by_config_toml'
51+
52+
- 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"
57+
when: gitlab_runner_config_update_mode == 'by_template'

tasks/main-windows.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
- name: (Windows) Set global options
3131
ansible.builtin.import_tasks: global-setup-windows.yml
32+
when: gitlab_runner_config_update_mode == 'by_config_toml' or
33+
gitlab_runner_config_update_mode == 'by_registering'
3234

3335
- name: (Windows) Configure GitLab Runner
3436
ansible.builtin.include_tasks: config-runners-windows.yml
@@ -39,3 +41,10 @@
3941
args:
4042
chdir: "{{ gitlab_runner_config_file_location }}"
4143
when: gitlab_runner_windows_start_runner
44+
45+
- 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"
50+
when: gitlab_runner_config_update_mode == 'by_template'

templates/config.toml.j2

Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
concurrent = {{ gitlab_runner_concurrent }}
2+
{% if gitlab_runner_check_interval is defined %}
3+
check_interval = {{ gitlab_runner_check_interval }}
4+
{% endif %}
5+
{% if gitlab_runner_listen_address | length > 0 %}
6+
listen_address = {{ gitlab_runner_listen_address | tojson }}
7+
{% endif %}
8+
{% if gitlab_runner_log_format is defined %}
9+
log_format = {{ gitlab_runner_log_format | default("runner") }}
10+
{% endif %}
11+
{% if gitlab_runner_log_level is defined %}
12+
log_level = {{ gitlab_runner_log_level }}
13+
{% endif %}
14+
{% if gitlab_runner_sentry_dsn | length > 0 %}
15+
sentry_dsn = "{{ gitlab_runner_sentry_dsn }}"
16+
{% endif %}
17+
{% if gitlab_runner_session_server is defined %}
18+
[session_server]
19+
{% if gitlab_runner_session_server.listen_address | length > 0 %}
20+
listen_address = {{ gitlab_runner_session_server.listen_address }}
21+
{% endif %}
22+
{% if gitlab_runner_session_server.advertise_address | length > 0 %}
23+
advertise_address = {{ gitlab_runner_session_server.advertise_address }}
24+
{% endif %}
25+
{% if gitlab_runner_session_server.session_timeout is defined and gitlab_runner_session_server.session_timeout > 0 %}
26+
session_timeout = {{ gitlab_runner_session_server.session_timeout }}
27+
{% endif %}
28+
{% endif %}
29+
30+
{% for gitlab_runner in gitlab_runner_runners %}
31+
[[runners]]
32+
name = {{ gitlab_runner.name | tojson }}
33+
limit = {{ gitlab_runner.concurrent_specific | default(0) }}
34+
url = {{ gitlab_runner.url | default(gitlab_runner_coordinator_url) | tojson }}
35+
{% if gitlab_runner.clone_url is defined %}
36+
clone_url = {{ gitlab_runner.clone_url | tojson }}
37+
{% endif %}
38+
environment = {{ gitlab_runner.env_vars | default([]) | tojson }}
39+
{% if gitlab_runner.pre_get_sources_script is defined %}
40+
pre_get_sources_script = {{ gitlab_runner.pre_get_sources_script | tojson }}
41+
{% endif %}
42+
{% if gitlab_runner.post_get_sources_script is defined %}
43+
post_get_sources_script = {{ gitlab_runner.post_get_sources_script | tojson }}
44+
{% endif %}
45+
{% if gitlab_runner.pre_build_script is defined %}
46+
pre_build_script = {{ gitlab_runner.pre_build_script | tojson }}
47+
{% endif %}
48+
{% if gitlab_runner.tls_ca_file is defined %}
49+
tls-ca-file = {{ gitlab_runner.tls_ca_file | tojson }}
50+
{% endif %}
51+
{% if gitlab_runner.tls_cert_file is defined %}
52+
tls-cert-file = {{ gitlab_runner.tls_cert_file | default([]) | tojson }}
53+
{% endif %}
54+
{% if gitlab_runner.tls_key_file is defined %}
55+
tls-key-file = {{ gitlab_runner.tls_key_file | default([]) | tojson }}
56+
{% endif %}
57+
{% if gitlab_runner.post_build_script is defined %}
58+
post_build_script = {{ gitlab_runner.post_build_script | tojson }}
59+
{% endif %}
60+
executor = {{ gitlab_runner.executor | default("shell") | tojson }}
61+
{% if gitlab_runner.builds_dir is defined %}
62+
builds_dir = {{ gitlab_runner.builds_dir | default("") | tojson }}
63+
{% endif %}
64+
{% if gitlab_runner.cache_dir is defined %}
65+
cache_dir = {{ gitlab_runner.cache_dir | default("") | tojson }}
66+
{% endif %}
67+
{% if gitlab_runner.shell is defined %}
68+
shell = {{ gitlab_runner.shell | default("") | tojson }}
69+
{% endif %}
70+
output_limit = {{ gitlab_runner.output_limit | default(4096) }}
71+
id = {{ gitlab_runner.id }}
72+
token = {{ gitlab_runner.token | tojson }}
73+
token_obtained_at = {{ gitlab_runner.token_obtained_at }}
74+
token_expires_at = {{gitlab_runner.token_expires_at | default("0001-01-01T00:00:00Z") }}
75+
{#### [runners.docker] section ####}
76+
{% if gitlab_runner.executor == "docker" or gitlab_runner.executor == "docker+machine" %}
77+
[runners.docker]
78+
image = {{ gitlab_runner.docker_image | default("") | tojson }}
79+
{% if gitlab_runner.docker_helper_image is defined %}
80+
helper_image = {{ gitlab_runner.docker_helper_image | default("") | tojson }}
81+
{% endif %}
82+
{% if gitlab_runner.docker_privileged is defined %}
83+
privileged = {{ gitlab_runner.docker_privileged | default(false) | lower }}
84+
{% endif %}
85+
{% if gitlab_runner.docker_wait_for_services_timeout is defined %}
86+
wait_for_services_timeout = {{ gitlab_runner.docker_wait_for_services_timeout | default(30) }}
87+
{% endif %}
88+
{% if gitlab_runner.docker_tls_verify is defined %}
89+
tls_verify = {{ gitlab_runner.docker_tls_verify | default(false) | lower }}
90+
{% endif %}
91+
{% if gitlab_runner.docker_shm_size is defined %}
92+
shm_size = {{ gitlab_runner.docker_shm_size | default(false) | lower }}
93+
{% endif %}
94+
{% if gitlab_runner.docker_disable_cache is defined %}
95+
disable_cache = {{ gitlab_runner.docker_disable_cache | default(false) | lower }}
96+
{% endif %}
97+
{% if gitlab_runner.docker_dns is defined %}
98+
dns = {{ gitlab_runner.docker_dns | default(false) | tojson }}
99+
{% endif %}
100+
{% if gitlab_runner.docker_dns_search is defined %}
101+
dns_search = {{ gitlab_runner.docker_dns_search | default(false) | tojson }}
102+
{% endif %}
103+
{% if gitlab_runner.docker_pull_policy is defined %}
104+
pull_policy = {{ gitlab_runner.docker_pull_policy | default([]) | tojson }}
105+
{% endif %}
106+
{% if gitlab_runner.docker_allowed_pull_policies is defined %}
107+
allowed_pull_policies = {{ gitlab_runner.docker_allowed_pull_policies | default([]) | tojson }}
108+
{% endif %}
109+
{% if gitlab_runner.docker_volumes is defined %}
110+
volumes = {{ gitlab_runner.docker_volumes | default([]) | tojson }}
111+
{% endif %}
112+
{% if gitlab_runner.docker_devices is defined %}
113+
devices = {{ gitlab_runner.docker_devices | default([]) | tojson }}
114+
{% endif %}
115+
{% if gitlab_runner.docker_network_mode is defined %}
116+
network_mode = {{ gitlab_runner.docker_network_mode | default("bridge") | tojson }}
117+
{% endif %}
118+
{% if gitlab_runner.docker_disable_entrypoint_overwrite is defined %}
119+
disable_entrypoint_overwrite = {{ gitlab_runner.docker_disable_entrypoint_overwrite | default(false) | tojson }}
120+
{% endif %}
121+
{% if gitlab_runner.docker_oom_kill_disable is defined %}
122+
oom_kill_disable = {{ gitlab_runner.docker_oom_kill_disable | default(false) | tojson }}
123+
{% endif %}
124+
{% if gitlab_runner.docker_security_opt is defined %}
125+
security_opt = {{ gitlab_runner.docker_security_opt | tojson }}
126+
{% endif %}
127+
{#### [[runners.docker.services]] section ####}
128+
{% if gitlab_runner.docker_services is defined %}
129+
{% for service in gitlab_runner.docker_services %}
130+
[[runners.docker.services]]
131+
{% for attr in service %}
132+
{{ attr }} = {{ service[attr] | to_json }}
133+
{% endfor %}
134+
{% endfor %}
135+
{% endif %}
136+
{% endif %}
137+
{#### [runners.ssh] section #####}
138+
{% if gitlab_runner.executor == "ssh" %}
139+
[runners.ssh]
140+
{% if gitlab_runner.ssh_host is defined %}
141+
host = {{ gitlab_runner.ssh_host | default("") | tojson }}
142+
{% endif %}
143+
{% if gitlab_runner.ssh_user is defined %}
144+
user = {{ gitlab_runner.ssh_user | default("") | tojson }}
145+
{% endif %}
146+
{% if gitlab_runner.ssh_port is defined %}
147+
port = {{ gitlab_runner.ssh_port | default("") | tojson }}
148+
{% endif %}
149+
{% if gitlab_runner.ssh_password is defined %}
150+
password = {{ gitlab_runner.ssh_password | default("") | tojson }}
151+
{% endif %}
152+
{% if gitlab_runner.ssh_identity_file is defined %}
153+
identity_file = {{ gitlab_runner.ssh_identity_file | default("") | tojson }}
154+
{% endif %}
155+
{% endif %}
156+
{#### [runners.virtualbox] section #####}
157+
{% if gitlab_runner.executor == "virtualbox" %}
158+
[runners.virtualbox]
159+
{% if gitlab_runner.virtualbox_base_name is defined %}
160+
base_name = {{ gitlab_runner.virtualbox_base_name | default("") | tojson }}
161+
{% endif %}
162+
{% if gitlab_runner.virtualbox_base_snapshot is defined %}
163+
base_snapshot = {{ gitlab_runner.virtualbox_base_snapshot | tojson }}
164+
{% endif %}
165+
{% if gitlab_runner.virtualbox_base_folder is defined %}
166+
base_folder = {{ gitlab_runner.virtualbox_base_folder | tojson }}
167+
{% endif %}
168+
{% if gitlab_runner.virtualbox_disable_snapshots is defined %}
169+
disable_snapshots = {{ gitlab_runner.virtualbox_disable_snapshots | default(false) | lower }}
170+
{% endif %}
171+
#### [runners.custom_build_dir] section #####
172+
{% if gitlab_runner.custom_build_dir_enabled is defined %}
173+
[runners.custom_build_dir]
174+
enabled = {{ gitlab_runner.custom_build_dir_enabled | default(false) | lower }}
175+
{% endif %}
176+
{% endif %}
177+
{#### [runners.cache] section ####}
178+
[runners.cache]
179+
{% if gitlab_runner.cache_type is defined %}
180+
Type = {{ gitlab_runner.cache_type | default("") | to_json }}
181+
{% endif %}
182+
{% if gitlab_runner.cache_path is defined %}
183+
Path = {{ gitlab_runner.cache_path | default("") | to_json }}
184+
{% endif %}
185+
{% if gitlab_runner.cache_shared is defined %}
186+
Shared = {{ gitlab_runner.cache_shared | default("") | lower }}
187+
{% endif %}
188+
{#### [runners.cache.s3] section ####}
189+
{% if gitlab_runner.cache_s3_bucket_name is defined %}
190+
[runners.cache.s3]
191+
BucketName = {{ gitlab_runner.cache_s3_bucket_name | default("") | tojson }}
192+
{% if gitlab_runner.cache_s3_server_address is defined %}
193+
ServerAddress = {{ gitlab_runner.cache_s3_server_address | default("") | tojson }}
194+
{% endif %}
195+
{% if gitlab_runner.cache_s3_access_key is defined %}
196+
AccessKey = {{ gitlab_runner.cache_s3_access_key | default("") | tojson }}
197+
{% endif %}
198+
{% if gitlab_runner.cache_s3_secret_key is defined %}
199+
SecretKey = {{ gitlab_runner.cache_s3_secret_key | default("") | tojson }}
200+
{% endif %}
201+
{% if gitlab_runner.cache_s3_bucket_location is defined %}
202+
BucketLocation = {{ gitlab_runner.cache_s3_bucket_location | default("") | tojson }}
203+
{% endif %}
204+
{% if gitlab_runner.cache_s3_insecure %}
205+
Insecure = {{ gitlab_runner.cache_s3_insecure | default("") | lower }}
206+
{% endif %}
207+
{% endif %}
208+
{#### [runners.cache.gcs] section ####}
209+
{% if gitlab_runner.cache_gcs_bucket_name is defined %}
210+
[runners.cache.gcs]
211+
BucketName = {{ gitlab_runner.cache_gcs_bucket_name | default("") | tojson }}
212+
{% if gitlab_runner.cache_gcs_credentials_file is defined %}
213+
CredentialsFile = {{ gitlab_runner.cache_gcs_credentials_file | default("") | tojson }}
214+
{% endif %}
215+
{% if gitlab_runner.cache_gcs_access_id is defined %}
216+
AccessID = {{ gitlab_runner.cache_gcs_access_id | default("") | tojson }}
217+
{% endif %}
218+
{% if gitlab_runner.cache_gcs_private_key is defined %}
219+
PrivateKey = {{ gitlab_runner.cache_gcs_private_key | default("") | tojson }}
220+
{% endif %}
221+
{% endif %}
222+
{#### [runners.cache.azure] section ####}
223+
{% if gitlab_runner.cache_azure_account_name is defined %}
224+
[runners.cache.azure]
225+
AccountName = {{ gitlab_runner.cache_azure_account_name | default("") | tojson }}
226+
{% if gitlab_runner.cache_azure_account_key is defined %}
227+
AccountKey = {{ gitlab_runner.cache_azure_account_key | default("") | tojson }}
228+
{% endif %}
229+
{% if gitlab_runner.cache_azure_container_name is defined %}
230+
ContainerName = {{ gitlab_runner.cache_azure_container_name | default("") | to_json }}
231+
{% endif %}
232+
{% if gitlab_runner.cache_azure_storage_domain is defined %}
233+
StorageDomain = {{ gitlab_runner.cache_azure_storage_domain | default("") | to_json }}
234+
{% endif %}
235+
{% endif %}
236+
{% if gitlab_runner.feature_flags is defined %}
237+
[runners.feature_flags]
238+
{% for flag in gitlab_runner.feature_flags %}
239+
{{ flag }} = true
240+
{% endfor %}
241+
{% endif %}
242+
{#### [runners.machine] section ####}
243+
{% if gitlab_runner.machine_MachineOptions is defined %}
244+
[runners.machine]
245+
MachineOptions = {{ gitlab_runner.machine_MachineOptions | default("") | tojson }}
246+
{% if gitlab_runner.machine_MaxGrowthRate is defined %}
247+
MaxGrowthRate = {{ gitlab_runner.machine_MaxGrowthRate | default("") | tojson }}
248+
{% endif %}
249+
{% if gitlab_runner.machine_IdleCount is defined %}
250+
IdleCount = {{ gitlab_runner.machine_IdleCount | default("") | tojson }}
251+
{% endif %}
252+
{% if gitlab_runner.machine_IdleScaleFactor is defined %}
253+
IdleScaleFactor = {{ gitlab_runner.machine_IdleScaleFactor | default("") | tojson }}
254+
{% endif %}
255+
{% if gitlab_runner.machine_IdleCountMin is defined %}
256+
IdleCountMin = {{ gitlab_runner.machine_IdleCountMin | default("") | tojson }}
257+
{% endif %}
258+
{% if gitlab_runner.machine_IdleTime is defined %}
259+
IdleTime = {{ gitlab_runner.machine_IdleTime | default("") | tojson }}
260+
{% endif %}
261+
{% if gitlab_runner.machine_MaxBuilds is defined %}
262+
MaxBuilds = {{ gitlab_runner.machine_MaxBuilds | default("") | tojson }}
263+
{% endif %}
264+
{% if gitlab_runner.machine_MachineName is defined %}
265+
MachineName = {{ gitlab_runner.machine_MachineName | default("") | tojson }}
266+
{% endif %}
267+
{% if gitlab_runner.machine_MachineDriver is defined %}
268+
MachineDriver = {{ gitlab_runner.machine_MachineDriver | default("") | tojson }}
269+
{% endif %}
270+
{% endif %}
271+
{#### [runners.autoscaler] section ####}
272+
{% if gitlab_runner.machine_autoscaling is defined %}
273+
[runners.autoscaler]
274+
plugin = "{{ gitlab_runner.autoscaler.plugin }}" # for >= 16.11, ensure you run `gitlab-runner fleeting install` to automatically install the plugin
275+
capacity_per_instance = {{ gitlab_runner.autoscaler.capacity_per_instance }}
276+
max_use_count = {{ gitlab_runner.autoscaler.max_use_count }}
277+
max_instances = {{ gitlab_runner.autoscaler.max_instances }}
278+
[runners.autoscaler.plugin_config]
279+
{% if gitlab_runner.autoscaler.plugin_config.name is defined %}
280+
name = "{{ gitlab_runner.autoscaler.plugin_config.name }}"
281+
{% endif %}
282+
{% if gitlab_runner.autoscaler.plugin_config.profile is defined %}
283+
profile = "{{ gitlab_runner.autoscaler.plugin_config.profile }}"
284+
{% endif %}
285+
{% if gitlab_runner.autoscaler.plugin_config.config_file is defined %}
286+
config_file = "{{ gitlab_runner.autoscaler.plugin_config.config_file }}"
287+
{% endif %}
288+
{% if gitlab_runner.autoscaler.plugin_config.credentials_file is defined %}
289+
credentials_file = "{{ gitlab_runner.autoscaler.plugin_config.credentials_file }}"
290+
{% endif %}
291+
{% if gitlab_runner.autoscaler.plugin_config.subscription_id is defined %}
292+
subscription_id = "{{ gitlab_runner.autoscaler.plugin_config.subscription_id }}"
293+
{% endif %}
294+
{% if gitlab_runner.autoscaler.plugin_config.resource_group_name is defined %}
295+
resource_group_name = "{{ gitlab_runner.autoscaler.plugin_config.resource_group_name }}"
296+
{% endif %}
297+
{% if gitlab_runner.autoscaler.plugin_config.project is defined %}
298+
project = "{{ gitlab_runner.autoscaler.plugin_config.project }}"
299+
{% endif %}
300+
{% if gitlab_runner.autoscaler.plugin_config.zone is defined %}
301+
zone = "{{ gitlab_runner.autoscaler.plugin_config.zone }}"
302+
{% endif %}
303+
{% if gitlab_runner.autoscaler.plugin_config.endpoint is defined %}
304+
endpoint = "{{ gitlab_runner.autoscaler.plugin_config.endpoint }}"
305+
{% endif %}
306+
[runners.autoscaler.connector_config]
307+
{% for key, value in gitlab_runner.autoscaler.connector_config.items() %}
308+
{{ key }} = {{ '"' ~ value ~ '"' if value is string else value | lower if value is boolean else value }}
309+
{% endfor %}
310+
311+
{% for policy in gitlab_runner.autoscaler.policies %}
312+
[[runners.autoscaler.policy]]
313+
{% for key, value in policy.items() %}
314+
{{ key }} = {{ '"' ~ value ~ '"' if value is string else value | lower if value is boolean else value }}
315+
{% endfor %}
316+
{% endfor %}
317+
{% endif %}
318+
{#### [[runners.machine.autoscaling]] section ####}
319+
{% if gitlab_runner.machine_autoscaling is defined %}
320+
{% for machine in gitlab_runner.machine_autoscaling %}
321+
[[runners.machine.autoscaling]]
322+
{% for attr in machine %}
323+
{{ attr }} = {{ machine[attr] | to_json }}
324+
{% endfor %}
325+
{% endfor %}
326+
{% endif %}
327+
328+
{% endfor %}

0 commit comments

Comments
 (0)