Using ansible to deploy a gitlab runner.

After deploying watchtower to one of my docker instances, I noticed that it was having issues with restarting some of the containers. The actual fault turned out to be the networking with portainer, but in the meantime, I wrote an ansible playbook to redeploy / restart the container.

The first thing I do is set up the variables in the playbook. Bear in mind, that these can still be overwritten on the commandline, if necessary.

---
  - name: "Gitlab runner deployer"
    hosts: docker.host
    become: yes
    become_method: sudo

    vars:
    - apiurl: "https://gitlab.host"
    - apitoken: <Token from gitlab>
    - runnerdesc: "Generic Runner deployed via ansible"
    - runnertags: ['x86', 'amd64', 'ansible', 'docker']
  

I run docker on various architectures, so I do change the tags quite a bit. The api token (or authentication token) is obtained after configuring the runner in gitlab to start with. NB, Registration tokens are the old way of doing the registration. More details can be found here: https://docs.gitlab.com/ee/security/token_overview.html#runner-authentication-tokens

The first thing we actually need to do is to the docker host itself. We need to install a dependency. My docker host is based on debian (specifically turnkey linux), so I add python3-gitlab via apt.

    - name: Install dependencies
      apt:
        pkg:
        - python3-gitlab

The next stage is to set up the volumes that the runner requires. In this case, we need 2. One for the configuration and one for the runner's home directory.

    - name: Create the runner configuration volume
      docker_volume:
        name: gitlabrunner-config
      tags: volumecreate

    - name: Create the runner home volume
      docker_volume:
        name: gitlabrunner-home
      tags: volumecreate

The next stage is to setup the actual container. The container also needs access to the docker socket. This is needed to deploy other containers.

    - name: Start gitlab runner
      docker_container:
        name: gitlab-runner
        state: started
        image: 'gitlab/gitlab-runner:latest'
        volumes:
        - /var/run/docker.sock:/var/run/docker.sock
        - gitlabrunner-config:/etc/gitlab-runner
        - gitlabrunner-home:/home/gitlab-runner
      tags: deploygitlabrunner

Once the container is set up, the last step is to register the container with gitlab. This is straightforward and is done via:

    - name: "Register runner"
      community.general.gitlab_runner:
        api_url: "{{ apiurl }}"
        api_token: "{{ apitoken }}"
#        registration_token: {{ reg }}
        description: "{{ runnerdesc }}"
        active: true
        tag_list: "{{ runnertags }}"
        run_untagged: false
        locked: false

Finally, putting it all together, we get the following playbook:

---
  - name: "Gitlab runner deployer"
    hosts: docker.host
    become: yes
    become_method: sudo

    vars:
    - apiurl: "https://gitlab.host"
    - apitoken: <Token from gitlab>
    - runnerdesc: "Generic Runner deployed via ansible"
    - runnertags: ['x86', 'amd64', 'ansible', 'docker']

    tasks:

    - name: Install dependencies
      apt:
        pkg:
        - python3-gitlab

    - name: Create the runner configuration volume
      docker_volume:
        name: gitlabrunner-config
      tags: volumecreate

    - name: Create the runner home volume
      docker_volume:
        name: gitlabrunner-home
      tags: volumecreate

    - name: Start gitlab runner
      docker_container:
        name: gitlab-runner
        state: started
        image: 'gitlab/gitlab-runner:latest'
        volumes:
        - /var/run/docker.sock:/var/run/docker.sock
        - gitlabrunner-config:/etc/gitlab-runner
        - gitlabrunner-home:/home/gitlab-runner
      tags: deploygitlabrunner

    - name: "Register runner"
      community.general.gitlab_runner:
        api_url: "{{ apiurl }}"
        api_token: "{{ apitoken }}"
#        registration_token: XJoEGxz-k
        description: "{{ runnerdesc }}"
#        state: present
        active: true
        tag_list: "{{ runnertags }}"
        run_untagged: false
        locked: false

As an Amazon Associate I earn from qualifying purchases.

If you have found this post useful, please consider donating.