Skip to content

Commit dd79bd4

Browse files
committed
gitlab-runner: Add explicit become directives
Signed-off-by: Norman Ziegner <[email protected]>
1 parent 0cd36e2 commit dd79bd4

File tree

10 files changed

+44
-8
lines changed

10 files changed

+44
-8
lines changed

molecule/gitlab_runner/converge.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
- name: "Converge"
88
hosts: "all"
99
become: false
10+
vars:
11+
# Override to disable docker dependency during converge phase
12+
# Docker is already installed in prepare.yml for instances that need it
13+
# This allows us to test explicit become directives without the dependency interfering
14+
gitlab_runner_install_docker: false
1015
tasks:
1116
- name: "Include gitlab_runner role"
1217
ansible.builtin.include_role:

molecule/gitlab_runner/prepare.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@
3838
community.general.alternatives:
3939
name: "ip6tables"
4040
path: "/usr/sbin/ip6tables-legacy"
41+
42+
- name: "Install Docker for instances that need it"
43+
hosts: "all"
44+
become: true
45+
tasks:
46+
- name: "Include geerlingguy.docker role"
47+
when: "gitlab_runner_install_docker | default(false)"
48+
ansible.builtin.include_role:
49+
name: "geerlingguy.docker"
50+
vars:
51+
docker_install_compose: false

molecule/gitlab_runner/verify.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
---
77
- name: "Verify"
88
hosts: "all"
9+
become: true
910
tasks:
1011
- name: "Gather package facts"
1112
ansible.builtin.package_facts:
@@ -41,7 +42,6 @@
4142
failed_when: "'0.16.2-gitlab.25' not in machine_version.stdout"
4243

4344
- name: "Identify installed fleeting plugins"
44-
become: true
4545
ansible.builtin.command: "gitlab-runner fleeting list"
4646
register: "fleeting_cmd"
4747
changed_when: false
@@ -65,7 +65,6 @@
6565
- "gitlab_runner_version is defined"
6666

6767
- name: "Assert that ignition.json file was created"
68-
become: true
6968
ansible.builtin.stat:
7069
path: "/etc/gitlab-runner/ignition.json"
7170
register: "ignition"
@@ -75,14 +74,12 @@
7574
when: "gitlab_runner_ssh_public_key | default('') | length == 0 or gitlab_runner_ssh_public_key | default('') | length == 0"
7675
block:
7776
- name: "Assert that SSH key pair was created"
78-
become: true
7977
ansible.builtin.stat:
8078
path: "/etc/gitlab-runner/gitlab_runner_key"
8179
register: "ssh_key"
8280
failed_when: "not ssh_key.stat.isreg or ssh_key.stat.mode != '0600'"
8381

8482
- name: "Read generated SSH public key"
85-
become: true
8683
ansible.builtin.command: "cat /etc/gitlab-runner/gitlab_runner_key.pub"
8784
register: "generated_pub_key"
8885
changed_when: false
@@ -99,7 +96,6 @@
9996
- "gitlab_runner_ssh_private_key | default('') | length > 0"
10097

10198
- name: "Read flatcar linux config"
102-
become: true
10399
ansible.builtin.command: "cat /etc/gitlab-runner/ignition.json"
104100
register: "flatcar_linux_config"
105101
changed_when: false
@@ -114,7 +110,6 @@
114110
gitlab_runner_authentication_token: "{{ lookup('env', 'AUTHENTICATION_TOKEN') }}"
115111

116112
- name: "Assert that the runner was registered successfully"
117-
become: true
118113
ansible.builtin.command: "gitlab-runner list"
119114
changed_when: false
120115
register: "runners"
@@ -123,7 +118,6 @@
123118
when: "gitlab_runner_authentication_token | length > 0"
124119

125120
- name: "Assert that the verify command is successful"
126-
become: true
127121
ansible.builtin.command: "gitlab-runner verify"
128122
changed_when: false
129123
register: "runners_verify"
@@ -138,7 +132,6 @@
138132
failed_when: '"# TYPE gitlab_runner_version_info" not in metrics.content'
139133

140134
- name: "Unregister GitLab-Runner"
141-
become: true
142135
ansible.builtin.command: "gitlab-runner unregister --all-runners"
143136
changed_when: false
144137
# Do not verify runner registration in forks

roles/gitlab_runner/handlers/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
---
77

88
- name: "Transpile the flatcar linux configuration"
9+
become: true
910
ansible.builtin.command: "butane -o /etc/gitlab-runner/ignition.json /etc/gitlab-runner/butane-config.bu"
1011
changed_when: true
1112

1213
- name: "Restart GitLab-Runner"
14+
become: true
1315
ansible.builtin.service:
1416
name: "gitlab-runner"
1517
state: "restarted"

roles/gitlab_runner/tasks/configuration.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- "gitlab_runner_ssh_private_key | default('') | length > 0"
1111
block:
1212
- name: "Place SSH public key on the host for communicating with Runners."
13+
become: true
1314
ansible.builtin.copy:
1415
src: "{{ gitlab_runner_ssh_public_key }}"
1516
dest: "{{ gitlab_runner_ssh_public_key_path }}"
@@ -18,6 +19,7 @@
1819
mode: "0644"
1920

2021
- name: "Place SSH private key on the host for communicating with Runners."
22+
become: true
2123
ansible.builtin.copy:
2224
src: "{{ gitlab_runner_ssh_private_key }}"
2325
dest: "{{ gitlab_runner_ssh_private_key_path }}"
@@ -26,20 +28,23 @@
2628
mode: "0600"
2729

2830
- name: "Create SSH key pair for communicating with Runners."
31+
become: true
2932
community.crypto.openssh_keypair: # noqa: args[module]
3033
path: "{{ gitlab_runner_ssh_private_key_path }}"
3134
type: "{{ gitlab_runner_ssh_key_type | default('ed25519') }}"
3235
register: "__gitlab_runner_ssh_keypair"
3336
when: "not __gitlab_runner_is_initial_dryrun" # skip if run for the first time in check mode
3437

3538
- name: "Download and install container-linux-config-transpiler"
39+
become: true
3640
ansible.builtin.get_url:
3741
url: "{{ gitlab_runner_transpiler_binary_url }}"
3842
dest: "/usr/local/bin/butane"
3943
mode: "0755"
4044
checksum: "{{ gitlab_runner_transpiler_binary_checksum }}"
4145

4246
- name: "Place the container linux configuration on the host"
47+
become: true
4348
ansible.builtin.template:
4449
src: "{{ gitlab_runner_butane_config_template }}"
4550
dest: "/etc/gitlab-runner/butane-config.bu"
@@ -52,6 +57,7 @@
5257
- "Transpile the flatcar linux configuration"
5358

5459
- name: "Check if ignition.json is available and create it in any case"
60+
become: true
5561
when: "not __flatcar_config_task.changed" # noqa no-handler
5662
ansible.builtin.stat:
5763
path: "/etc/gitlab-runner/ignition.json"
@@ -77,11 +83,13 @@
7783
check_mode: false
7884

7985
- name: "Dry-run of transpile the flatcar linux configuration"
86+
become: true
8087
ansible.builtin.command: "butane -o {{ (__temp_directory.path, 'ignition.json') | path_join }} /etc/gitlab-runner/butane-config.bu"
8188
changed_when: false
8289
check_mode: false
8390

8491
- name: "Stat temporary ignition.json file"
92+
become: true
8593
ansible.builtin.stat:
8694
path: "{{ (__temp_directory.path, 'ignition.json') | path_join }}"
8795
register: "__temp_ignition_stats"

roles/gitlab_runner/tasks/docker-machine-init.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
---
77
- name: "Check if docker-machine initialization is necessary"
8+
become: true
89
ansible.builtin.stat:
910
path: "/root/.docker/machine/certs/ca.pem"
1011
register: "__docker_machine_config"
@@ -17,6 +18,7 @@
1718
__machine_options: "--{{ gitlab_runner.machine_options | join(' --') }}"
1819

1920
- name: "Create a VM once via docker-machine"
21+
become: true
2022
when: "not ansible_check_mode"
2123
ansible.builtin.command: "docker-machine create -d {{ gitlab_runner.machine_driver }} {{ __machine_options }} test"
2224
register: "__creation_cmd"
@@ -25,6 +27,7 @@
2527

2628
always:
2729
- name: "Remove the VM"
30+
become: true
2831
when: "not ansible_check_mode"
2932
ansible.builtin.command: "docker-machine rm -y --force test"
3033
register: "__removal_cmd"

roles/gitlab_runner/tasks/install.autoscaler-plugin.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
changed_when: false
3232

3333
- name: "Download fleeting-plugin-openstack"
34+
become: true
3435
ansible.builtin.get_url:
3536
url: "{{ gitlab_runner_autoscaler_plugin_url }}"
3637
dest: "{{ (__tempdir_fleeting_plugin.path, 'fleeting-plugin-openstack.tar.gz') | path_join }}"
@@ -41,6 +42,7 @@
4142
check_mode: false
4243

4344
- name: "Extract fleeting-plugin-openstack binary"
45+
become: true
4446
ansible.builtin.unarchive:
4547
src: "{{ (__tempdir_fleeting_plugin.path, 'fleeting-plugin-openstack.tar.gz') | path_join }}"
4648
dest: "/usr/local/bin/"
@@ -61,6 +63,7 @@
6163
changed_when: false
6264

6365
- name: "Place clouds.yaml template"
66+
become: true
6467
ansible.builtin.template:
6568
src: "clouds.yaml.j2"
6669
dest: "/etc/gitlab-runner/clouds.yaml"

roles/gitlab_runner/tasks/install.debianlike.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
---
77

88
- name: "Install GitLab-Runner dependencies"
9+
become: true
910
ansible.builtin.apt:
1011
pkg:
1112
- "debian-archive-keyring"
@@ -20,6 +21,7 @@
2021

2122
block:
2223
- name: "Add packages repository packages.gitlab.com/runner/gitlab-runner"
24+
become: true
2325
ansible.builtin.deb822_repository:
2426
name: "gitlab-runner"
2527
types: "deb"
@@ -31,6 +33,7 @@
3133
enabled: true
3234

3335
- name: "Use APT pinning for Debian os"
36+
become: true
3437
ansible.builtin.template:
3538
src: "pin-gitlab-runner.pref.j2"
3639
dest: "/etc/apt/preferences.d/pin-gitlab-runner.pref"
@@ -40,6 +43,7 @@
4043
when: "ansible_facts.distribution == 'Debian'"
4144

4245
- name: "Install gitlab-runner-helper-images with downgrade option"
46+
become: true
4347
ansible.builtin.apt:
4448
name: "{{ gitlab_runner_helper_images_package_name }}"
4549
state: "present"
@@ -50,6 +54,7 @@
5054
- "gitlab_runner_version is version('17.7.0', 'ge') or gitlab_runner_version | length == 0"
5155

5256
- name: "Install gitlab-runner with downgrade option"
57+
become: true
5358
ansible.builtin.apt:
5459
name: "{{ gitlab_runner_package_name }}"
5560
state: "present"
@@ -69,12 +74,14 @@
6974
block:
7075

7176
- name: "Install gitlab-runner-helper-images from a .deb file"
77+
become: true
7278
ansible.builtin.apt:
7379
deb: "{{ gitlab_runner_helper_images_deb_file }}"
7480
allow_downgrade: true
7581
when: "gitlab_runner_version is version('17.7.0', 'ge') or gitlab_runner_version | length == 0"
7682

7783
- name: "Install gitlab-runner from a .deb file"
84+
become: true
7885
ansible.builtin.apt:
7986
deb: "{{ gitlab_runner_deb_file }}"
8087
allow_downgrade: true

roles/gitlab_runner/tasks/install.docker-machine.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
---
77
- name: "Download and install docker-machine binary"
8+
become: true
89
ansible.builtin.get_url:
910
url: "{{ gitlab_runner_docker_machine_binary_url }}"
1011
dest: "/usr/local/bin/docker-machine"

roles/gitlab_runner/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
loop_var: "gitlab_runner"
4949

5050
- name: "Slurp ignition json"
51+
become: true
5152
ansible.builtin.slurp:
5253
src: "/etc/gitlab-runner/ignition.json"
5354
register: "__ignition_json"
@@ -56,6 +57,7 @@
5657
- "not __gitlab_runner_is_initial_dryrun"
5758

5859
- name: "Template config file"
60+
become: true
5961
ansible.builtin.template:
6062
src: "config.toml.j2"
6163
dest: "{{ gitlab_runner_config_path }}"
@@ -69,6 +71,7 @@
6971
when: "not __gitlab_runner_is_initial_dryrun"
7072

7173
- name: "Start GitLab-Runner"
74+
become: true
7275
ansible.builtin.service:
7376
name: "gitlab-runner"
7477
state: "started"

0 commit comments

Comments
 (0)