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        groups: kvm
30        comment: GitLab Runner
31        home: /home/gitlab-runner
32        shell: /bin/bash
33
34    - name: Remove the .bash_logout file when on Ubuntu systems
35      file:
36        path: /home/gitlab-runner/.bash_logout
37        state: absent
38      when: "ansible_facts['distribution'] == 'Ubuntu'"
39
40    - name: Set the Operating System for gitlab-runner
41      set_fact:
42        gitlab_runner_os: "{{ ansible_facts[\"system\"]|lower }}"
43    - debug:
44        msg: gitlab-runner OS is {{ gitlab_runner_os }}
45
46    - name: Set the architecture for gitlab-runner
47      set_fact:
48        gitlab_runner_arch: "{{ ansible_to_gitlab_arch[ansible_facts[\"architecture\"]] }}"
49    - debug:
50        msg: gitlab-runner arch is {{ gitlab_runner_arch }}
51
52    # Debian/Ubuntu setup
53    - name: Get gitlab-runner repo setup script (DEB)
54      get_url:
55        dest: "/root/"
56        url: "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh"
57        mode: 0755
58      when:
59        - ansible_facts['distribution'] == 'Ubuntu'
60
61    - name: Run gitlab-runner repo setup script (DEB)
62      shell: "/root/script.deb.sh"
63      when:
64        - ansible_facts['distribution'] == 'Ubuntu'
65
66    - name: Install gitlab-runner (DEB)
67      ansible.builtin.apt:
68          name: gitlab-runner
69          update_cache: yes
70          state: present
71      when:
72        - ansible_facts['distribution'] == 'Ubuntu'
73
74    # RPM setup
75    - name: Get gitlab-runner repo setup script (RPM)
76      get_url:
77        dest: "/root/"
78        url: "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh"
79        mode: 0755
80      when:
81        - ansible_facts['distribution'] == 'CentOS'
82
83    - name: Run gitlab-runner repo setup script (RPM)
84      shell: "/root/script.rpm.sh"
85      when:
86        - ansible_facts['distribution'] == 'CentOS'
87
88    - name: Install gitlab-runner (RPM)
89      yum:
90        name: gitlab-runner
91        update_cache: yes
92        state: present
93      when:
94        - ansible_facts['distribution'] == 'CentOS'
95
96    # Register Runners
97    - name: Register the gitlab-runner
98      command: "/usr/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\"] }})'"
99
100    # The secondary runner will still run under the single gitlab-runner service
101    - name: Register secondary gitlab-runner
102      command: "/usr/bin/gitlab-runner 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\"] }})'"
103      when:
104        - ansible_facts['distribution'] == 'Ubuntu'
105        - ansible_facts['architecture'] == 'aarch64'
106        - ansible_facts['distribution_version'] == '22.04'
107
108    - name: Install the gitlab-runner service using its own functionality
109      command: "/usr/bin/gitlab-runner install --user gitlab-runner --working-directory /home/gitlab-runner"
110      register: gitlab_runner_install_service_result
111      failed_when: "gitlab_runner_install_service_result.rc != 0 and \"already exists\" not in gitlab_runner_install_service_result.stderr"
112
113    - name: Enable the gitlab-runner service
114      service:
115        name: gitlab-runner
116        state: started
117        enabled: yes
118