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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Gitanjali Sahoo user avatar by
Gitanjali Sahoo
·
Jan. 10, 23 · Tutorial
Like (2)
Save
Tweet
Share
3.53K 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.

Popular on DZone

  • Type Variance in Java and Kotlin
  • Simulate Network Latency and Packet Drop In Linux
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • Three SQL Keywords in QuestDB for Finding Missing Data

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: