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    - name: Download the matching gitlab-runner (DEB)
53      get_url:
54        dest: "/root/"
55        url: "https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_{{ gitlab_runner_arch }}.deb"
56      when:
57        - ansible_facts['distribution'] == 'Ubuntu'
58
59    - name: Download the matching gitlab-runner (RPM)
60      get_url:
61        dest: "/root/"
62        url: "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_{{ gitlab_runner_arch }}.rpm"
63      when:
64        - ansible_facts['distribution'] == 'CentOS'
65
66    - name: Install gitlab-runner via package manager (DEB)
67      apt: deb="/root/gitlab-runner_{{ gitlab_runner_arch }}.deb"
68      when:
69        - ansible_facts['distribution'] == 'Ubuntu'
70
71    - name: Install gitlab-runner via package manager (RPM)
72      yum: name="/root/gitlab-runner_{{ gitlab_runner_arch }}.rpm"
73      when:
74        - ansible_facts['distribution'] == 'CentOS'
75
76    - name: Register the gitlab-runner
77      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\"] }})'"
78
79    # The secondary runner will still run under the single gitlab-runner service
80    - name: Register secondary gitlab-runner
81      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\"] }})'"
82      when:
83        - ansible_facts['distribution'] == 'Ubuntu'
84        - ansible_facts['architecture'] == 'aarch64'
85        - ansible_facts['distribution_version'] == '22.04'
86
87    - name: Install the gitlab-runner service using its own functionality
88      command: "/usr/bin/gitlab-runner install --user gitlab-runner --working-directory /home/gitlab-runner"
89      register: gitlab_runner_install_service_result
90      failed_when: "gitlab_runner_install_service_result.rc != 0 and \"already exists\" not in gitlab_runner_install_service_result.stderr"
91
92    - name: Enable the gitlab-runner service
93      service:
94        name: gitlab-runner
95        state: started
96        enabled: yes
97