Either way, the Dockerfile doesnât contain anything foolish: I install the packages needed for Ansible, the jumit_xml library (youâll see why later), I turn off host checking for SSH, and I create a generic user who will run the playbook. I could have added collections or Ansible modules as well as dependencies in python if I needed to.
Weâll automatically build this image in our pipeline, upload it into the built-in registry on Gitlab, and then use it in later steps.
Gitlab CI pipeline
Here we get to the heart of the matter! I took as an example the model that Gitlab provided for a Terraform pipeline available here. From this example, I removed the job to delete the âterraform destroyâ command and put the âbuildâ job in automatic mode instead of manual.
I defined the stages of my pipeline like this:
- Preparation: The preparation step for the build will contain the jobs initializing the Terraform workspace and building the Docker Ansible image.
- Validation: Validation step performing syntax tests on Terraform and Ansible files.
- Building: Step performing a âterraform planâ to create the infrastructure.
- Deploying: Stage building the infrastructure and retrieving the machineâs IP address.
- Provisioning: Stage running the Ansible playbook.
I used Gitlabâs âneedsâ keyword to tell it which tasks are dependent on others. For example, this makes it possible to launch the jobâ planâ after the job âvalidationâ without waiting for theâlintâ job (the lint of Ansible). With this rearrangement, the pipeline can run much faster.