Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Set up a continuous integration pipeline with Ansible Automation Platform & GitLab

August 15, 2023
Nagesh Rathod
Related topics:
Automation and managementCI/CDIntegration
Related products:
Red Hat Ansible Automation Platform

Share:

    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. 

    Prerequisites

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

    • Ansible Automation Platform
    • GitLab Runner

    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
      script:
        - echo "Compiling the code..."
        - echo "Compile complete."
    
    
    deploy-job:      # This job runs in the deploy stage.
      stage: deploy
      environment: production
      script:
        - 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:
    • $ANSIBLE_CONTROLLER_USER

    • $ANSIBLE_CONTROLLER_PASSWORD 

    • $ANSIBLE_CONTROLLER_URL

    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:

    • Creating a project
    • Configuring job templates

    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: September 27, 2024

    Related Posts

    • Test container images in Red Hat OpenShift 4 with Ansible and CI/CD

    • How to install Red Hat Ansible Automation Platform on RHEL 9

    • What's new in Ansible Automation Platform 2.4

    • Automate your SSO with Ansible and Keycloak

    • Four reasons developers should use Ansible

    • Introducing Ansible Molecule with Ansible Automation Platform

    Recent Posts

    • The benefits of auto-merging GitHub and GitLab repositories

    • Supercharging AI isolation: microVMs with RamaLama & libkrun

    • Simplify multi-VPC connectivity with amazon.aws 9.0.0

    • How HaProxy router settings affect middleware applications

    • Fly Eagle(3) fly: Faster inference with vLLM & speculative decoding

    What’s up next?

    Get a preview of the Red Hat Certified Engineer (RHCE) Ansible Automation Study Guide (O’Reilly), which covers key Ansible concepts for your system administration needs.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue