DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • CI/CD Pipelines for Kubernetes Using GitLab CI
  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Ansible and the Pre-Container Arts
  • Fast Deployments of Microservices Using Ansible and Kubernetes

Trending

  • Building a Skill-Based Agentic Reviewer with Claude Code: A Practical Guide Using Skills.MD, MCP Servers, Tools, and Tasks
  • A Scalable Framework for Enterprise Salesforce Optimization: Turning Outcomes Into an Operating System
  • How to Write for DZone Publications: Trend Reports and Refcards
  • Agentic AI Design Patterns and Principles: Building Autonomous, Collaborative Systems
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Dockerizing an Ansible Playbook, Part 2

Dockerizing an Ansible Playbook, Part 2

In Part 2 of this two-part series, we will create a GitLab pipeline to register the Docker image of the Ansible playbook to a Docker registry and deploy it to Kubernetes.

By 
Gitanjali Sahoo user avatar
Gitanjali Sahoo
·
Updated Jan. 10, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.5K Views

Join the DZone community and get the full member experience.

Join For Free

This is a two-part article series. This is part 2 of the series, where we will create a GitLab pipeline to register the Docker image of the Ansible playbook to a Docker registry and deploy it to Kubernetes. 

In Part 1, we created an Ansible playbook and Dockerized it to create a Docker image and run it as a Docker container. We logged in to that container to run the ansible playbook.

Why GitLab Pipeline?

In the technology world of efficiency, we tend to automate everything possible to reduce repetitive manual tasks and effort. The GitLab pipeline plays a vital role in this journey.

At a basic level, the GitLab pipeline gets used to build, test and deploy code in stages. It helps 

  • In reducing the chances of human error as it is implemented as an automated scripted 
  • In creating faster iterations through continuous integration, delivery, deployment 
  • In better quality code by leveraging GitLab stages such as pre-check, testing, post-validation, logging, and tracing job execution path

Create a Gitlab Pipeline

In this article, we will create a simple GitLab pipeline with two stages, as below:

GitLab Pipeline

Create a Repository in Docker Hub

To create a repository in the Docker Hub, you need to log in to the Docker Hub. If you do not have an account, then sign up to create an account.

Once you log in to the Docker Hub, create a repository named "demo":

Create a Repository

Check-In Codebase

Create a GitLab project and check in all the files created under "playbook" in folder Part 1 of this GitLab project:

Playbook

Find CI/CD on the left-hand side navigation bar. The pipelines can be viewed from that menu option. At this point, there will be no pipeline displayed as we have not created any yet.

Now we will have to find a runner where we can run our pipeline.

CI/CD

On the right-hand panel, scroll down to find runners available. Use a runner which provides a Linux environment. If there is none exists, then create one by following Registering runners | GitLab.

Save the runner's name to refer to it later.

On the same page, scroll to find the "Variables" section.

Add two variables, UserID, and password, saving the Docker Hub account and credential.

Login


Check-in Kubernetes config file. This file has to be retrieved from your K8s environment to log in to K8s.

Checking below code snippet as Deployment.yamlfile. This file will be executed by pipeline to deploy to K8s.

 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-ansible
  labels:
    app: helloworld-ansible
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld-ansible
  template:
    metadata:
      labels:
        app: helloworld-ansible
    spec:
      containers:
      - name: helloworld-ansible
        image: hub.docker.com/<docker-hub-user-id>/demo:hwa1.0
        ports:
        - containerPort: 80


Replace the <docker-hub-user-id> with your Docker Hub UserID.

Create .gitlab_ci.yml File

This is the key file to the pipeline. It is a YAML file that needs to be created under the project's root folder. This file automatically runs whenever you push a commit to the server. 

YAML
 
stages:
  - build
  - deploy

image: ubuntu:16.04

build:
  stage: build
  tags:
    - <runner-name>
  script:

   # login to docker
    - docker login  -u $userid -p $password

    # build docker image
    - docker build . -t hwa
    - docker tag  hwa:latest hub.docker.com/<docker-hub-user-id>/demo:hwa1.0
   

    # push docker image to docker hub
    - docker push hub.docker.com/<docker-hub-user-id>/demo:hwa1.0


deploy:
  stage: deploy
  tags:
    - <runner-name>
 
  script:

     -  kubectl --kubeconfig=<kubeconfig file> apply -f deployment.yaml


Please make sure to replace <runner-name> with your runner name <docker-hub-user-id> with your Docker Hub UserID.

Committing this file will trigger the CI/CD pipeline.

You can view and debug by accessing pipelines from CI/CD->Pipelines

Accessing Pipelines

Some of the common issues can be mitigated by making sure:

  • The runner has access to the K8s container to deploy
  • You have the correct K8s config file to log in to the K8s container
  • Docker Hub repository URL is correct in your deployment.yaml file and GitLab config file. You may check the URL by logging in to the Docker Hub and lick on the repository. Scroll right to find the URL below "Docker commands"

Docker commands

GitLab Kubernetes Ansible (software) Docker (software) Pipeline (software)

Opinions expressed by DZone contributors are their own.

Related

  • CI/CD Pipelines for Kubernetes Using GitLab CI
  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Ansible and the Pre-Container Arts
  • Fast Deployments of Microservices Using Ansible and Kubernetes

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook