1# Copyright (c) 2021 Red Hat, Inc.
2#
3# Author:
4#  Cleber Rosa <crosa@redhat.com>
5#
6# This work is licensed under the terms of the GNU GPL, version 2 or
7# later.  See the COPYING file in the top-level directory.
8#
9# This is an ansible playbook file.  Run it to set up systems with the
10# gitlab-runner agent.
11---
12- name: Installation of gitlab-runner
13  hosts: all
14  vars_files:
15    - vars.yml
16  tasks:
17    - debug:
18        msg: 'Checking for a valid GitLab registration token'
19      failed_when: "gitlab_runner_registration_token == 'PLEASE_PROVIDE_A_VALID_TOKEN'"
20
21    - name: Create a group for the gitlab-runner service
22      group:
23        name: gitlab-runner
24
25    - name: Create a user for the gitlab-runner service
26      user:
27        user: gitlab-runner
28        group: gitlab-runner
29        comment: GitLab Runner
30        home: /home/gitlab-runner
31        shell: /bin/bash
32
33    - name: Remove the .bash_logout file when on Ubuntu systems
34      file:
35        path: /home/gitlab-runner/.bash_logout
36        state: absent
37      when: "ansible_facts['distribution'] == 'Ubuntu'"
38
39    - name: Set the Operating System for gitlab-runner
40      set_fact:
41        gitlab_runner_os: "{{ ansible_facts[\"system\"]|lower }}"
42    - debug:
43        msg: gitlab-runner OS is {{ gitlab_runner_os }}
44
45    - name: Set the architecture for gitlab-runner
46      set_fact:
47        gitlab_runner_arch: "{{ ansible_to_gitlab_arch[ansible_facts[\"architecture\"]] }}"
48    - debug:
49        msg: gitlab-runner arch is {{ gitlab_runner_arch }}
50
51    - name: Download the matching gitlab-runner
52      get_url:
53        dest: /usr/local/bin/gitlab-runner
54        url: "https://s3.amazonaws.com/gitlab-runner-downloads/v{{ gitlab_runner_version  }}/binaries/gitlab-runner-{{ gitlab_runner_os }}-{{ gitlab_runner_arch }}"
55        owner: gitlab-runner
56        group: gitlab-runner
57        mode: u=rwx,g=rwx,o=rx
58
59    - name: Register the gitlab-runner
60      command: "/usr/local/bin/gitlab-runner register --non-interactive --url {{ gitlab_runner_server_url }} --registration-token {{ gitlab_runner_registration_token }} --executor shell --tag-list {{ ansible_facts[\"architecture\"] }},{{ ansible_facts[\"distribution\"]|lower }}_{{ ansible_facts[\"distribution_version\"] }} --description '{{ ansible_facts[\"distribution\"] }} {{ ansible_facts[\"distribution_version\"] }} {{ ansible_facts[\"architecture\"] }} ({{ ansible_facts[\"os_family\"] }})'"
61
62    - name: Install the gitlab-runner service using its own functionality
63      command: /usr/local/bin/gitlab-runner install --user gitlab-runner --working-directory /home/gitlab-runner
64      register: gitlab_runner_install_service_result
65      failed_when: "gitlab_runner_install_service_result.rc != 0 and \"already exists\" not in gitlab_runner_install_service_result.stderr"
66
67    - name: Enable the gitlab-runner service
68      service:
69        name: gitlab-runner
70        state: started
71        enabled: yes
72
73    - name: Download secondary gitlab-runner
74      get_url:
75        dest: /usr/local/bin/gitlab-runner-arm
76        url: "https://s3.amazonaws.com/gitlab-runner-downloads/v{{ gitlab_runner_version  }}/binaries/gitlab-runner-{{ gitlab_runner_os }}-arm"
77        owner: gitlab-runner
78        group: gitlab-runner
79        mode: u=rwx,g=rwx,o=rx
80      when:
81        - ansible_facts['distribution'] == 'Ubuntu'
82        - ansible_facts['architecture'] == 'aarch64'
83        - ansible_facts['distribution_version'] == '20.04'
84
85    - name: Register secondary gitlab-runner
86      command: "/usr/local/bin/gitlab-runner-arm register --non-interactive --url {{ gitlab_runner_server_url }} --registration-token {{ gitlab_runner_registration_token }} --executor shell --tag-list aarch32,{{ ansible_facts[\"distribution\"]|lower }}_{{ ansible_facts[\"distribution_version\"] }} --description '{{ ansible_facts[\"distribution\"] }} {{ ansible_facts[\"distribution_version\"] }} {{ ansible_facts[\"architecture\"] }} ({{ ansible_facts[\"os_family\"] }})'"
87      when:
88        - ansible_facts['distribution'] == 'Ubuntu'
89        - ansible_facts['architecture'] == 'aarch64'
90        - ansible_facts['distribution_version'] == '20.04'
91
92    - name: Install the secondary gitlab-runner service using its own functionality
93      command: /usr/local/bin/gitlab-runner-arm install --user gitlab-runner --working-directory /home/gitlab-runner/arm -n gitlab-runner-arm
94      register: gitlab_runner_install_service_result
95      failed_when: "gitlab_runner_install_service_result.rc != 0 and \"already exists\" not in gitlab_runner_install_service_result.stderr"
96      when:
97        - ansible_facts['distribution'] == 'Ubuntu'
98        - ansible_facts['architecture'] == 'aarch64'
99        - ansible_facts['distribution_version'] == '20.04'
100
101    - name: Enable the secondary gitlab-runner service
102      service:
103        name: gitlab-runner-arm
104        state: started
105        enabled: yes
106      when:
107        - ansible_facts['distribution'] == 'Ubuntu'
108        - ansible_facts['architecture'] == 'aarch64'
109        - ansible_facts['distribution_version'] == '20.04'
110