Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

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

    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

    • Best Practice Configuration and Tuning for Linux and Windows VMs

    • Red Hat UBI 8 builders have been promoted to the Paketo Buildpacks organization

    • Using eBPF in Red Hat products

    • How we made one data layer serve the UI, the mocks, and the E2E tests

    • Build trusted Python containers with Project Hummingbird and Calunga

    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

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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