Skip to content

Using Ansible

If you also use an Ansible inventory for your hosts, you can of course integrate them into the GitLab pipelines too. To make this work, you need a separate GitLab runner tagged with ansible and a configuration like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
[[runners]]
  name = "Name of ansible runner"
  url = "https://gitlab.lakedrops.com/"
  token = "TOKEN"
  executor = "docker"
  environment = ["DOCKER_AUTH_CONFIG={\"auths\":{\"registry.lakedrops.com\":{\"auth\":\"YOURAUTHTOKEN\"}}}"]
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
  [runners.docker]
    tls_verify = false
    hostname = "Ansible-INVENTORYNAME"
    image = "registry.lakedrops.com/ansible-inventories/INVENTORYNAME:latest"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/home/gitlab-runner/.ssh/id_rsa:/root/.ssh/id_rsa", "/home/gitlab-runner/.a/variables.yml:/root/.ansible/secrets", "/home/gitlab-runner/.a/inventories/INVENTORYNAME:/root
/.ansible", "/etc/ansible.yml:/etc/ansible.yml", "/var/log/ansible:/var/log/ansible"]
    pull_policy = "always"
    shm_size = 0

The used placeholders in this example are:

  • TOKEN: the token of the GitLab runner being generated during setup.
  • YOURAUTHTOKEN: your auth token to the LakeDrops GitLab Docker registry.
  • INVENTORYNAME: the name of the Ansible inventory to be used.

To learn more about the Ansible images for an inventory and how to set up some configuration files, please have a look into Docker for Ansible.

Once you have the GitLab runner for Ansible setup for the Drupal project, you can adjust your GitLab CI configuration:

Using Ansible to Dump DB

You can replace the task above with this simple instruction:

1
2
Download DB:
  extends: '.dumpdb'

Using Ansible for Deployment

You need a couple of additional variables and then you can replace the deploy task above with this simple instruction:

1
2
3
4
5
6
variables:
  HOST_NAME: myhostname
  SITE_NAME: mysiteid

Deploy:
  extends: '.deploy'

The host name needs to be set to how the destination host is known in the Ansible inventory, and the site name needs to be set to the ID of the Drupal site in your inventory.