ansible share image

In modern software development practices, automation plays a crucial role in streamlining processes and ensuring efficient and reliable deployments. Red Hat Ansible Automation Platform is an enterprise automation tool that allows you to define and manage configuration as code. GitLab, on the other hand, provides a robust CI/CD platform for managing code repositories and executing continuous integration and deployment pipelines.

Integrating Ansible Automation Platform into GitLab CI pipelines (as illustrated in Figure 1) enables organizations to automate infrastructure provisioning and configuration management alongside their application deployments. This tutorial will guide you through the process of integrating Ansible Automation Platform in a GitLab CI pipeline.

Architectural diagram
Figure 1: Architecture diagram of GitLab CI/CD with Ansible Automation Platform

The pipeline starts when you commit changes in the GitLab code repository. The gitlab-ci.yaml file contains the blueprints for the pipelines. The tasks for building and deploying are executed by GitLab Runner based on that file. Deployment occurs through the POST API call of Ansible's automation controller. Once the API call is made, the template will trigger and deploy the application in the appropriate environment. 


Before proceeding, make sure the following prerequisites are installed on your system:

1. Create the GitLab CI Pipeline

As we already know, GitLab CI is a popular tool for CI/CD. Integrating Ansible Automation Platform with GitLab CI gives you more possibilities to explore.

Before moving forward, make sure the GitLab Runner is live and running, as shown in Figure 2.

runner up and running
Figure 2: The GitLab Runner is up and running.

The following gitlab-ci.yaml file includes the build and deployment stage:

stages:          # List of stages for jobs, and their order of execution
  - build
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
    - echo "Compiling the code..."
    - echo "Compile complete."

deploy-job:      # This job runs in the deploy stage.
  stage: deploy
  environment: production
    - curl -k -X POST --user '$ANSIBLE_CONTROLLER_USER:$ANSIBLE_CONTROLLER_PASSWORD' -H "Content-Type:application/json"  --data '{"limit":"ansible"}'  http://$ANSIBLE_CONTROLLER_URL/api/v2/job_templates/7/launch/ -k -L
    - echo "deployment is done"

2. Add Red Hat API integration

Red Hat offers API catalogs and documentation for its products. Here, we will use the POST API to call Ansible controller.  

Add variable in GitLab

The variable is a secured way to pass your credentials in the pipeline. To add variables in the pipeline, follow these steps:

  1. Log in to the GitLab web console and from the left menu, select Settings.
  2. Click on the CI/CD section.
  3. At the center of the screen, you will see the fifth option listed as Variable. Click the Expand option in front of the variable.
  4. Click on Add Variable option, then add the key as given below and add value as your credentials.
  5. Add the following variables:
gitlab ci variables
Figure 3: Adding variables for the pipeline.

3. Start the pipeline

We can see in Figure 3 that the YAML file contains the deployment stage. We'll use the POST API to trigger the Ansible template. The template contains inventories, playbooks, host server credentials, secrets, etc.

To configure the template, refer to the following tutorials on bare-metal instances, Kubernetes clusters, and Red Hat OpenShift clusters:

After adding the YAML, it is time to test the pipeline to ensure it is working. To test the pipeline, first commit the changes.

Log in to the GitLab web console and select CI/CD → Pipeline from the left menu.

done pipeline
Figure 4: A successful pipeline run.

To check that the pipeline template job executed successfully, log in to Ansible controller, navigate to Jobs, and check for the latest job. You will get the following output in case of successful execution:

PLAY [Application deployed] ******************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]

Continue your automation journey

You can download the latest version of Ansible Automation Platform from our website at no cost. Get started with Ansible Automation Platform right away with our interactive labs

You can find more Red Hat APIs and documentation in the API Catalog.

Last updated: November 6, 2023