Deploying Ansible inside a Docker container is an easy way to have a lightweight, agile environment for using Ansible. In this post, we will take a look at how to get Ansible up and running inside a Docker container on a container host.

IT operations and DevOps engineers use cloud-native tools to deploy and automate applications more effectively. There is no question that containerized applications provide a tremendous advantage over traditionally installed applications in the data center.

Using containers to run automation and configuration management tools can provide flexibility in deploying and running these applications. In this post, we see how the very popular automation and configuration management tool, Ansible, can easily be deployed and run from a Docker container.

What is Ansible? ^

In case you have not run across Ansible as an Infrastructure-as-Code (IaC) tool, it is a great way to get started automating the infrastructure in your environment. Ansible provides an easy way to install applications, provide configuration management, and achieve continuous delivery. It allows you to interact with the various types of infrastructure in your environment by using easy-to-understand YAML playbooks.

Ansible provides a great way to detect and correct configuration "drift" in your environment. You can easily integrate your Ansible YAML code into Git for proper version control as well. It can connect to your Linux and Windows servers without the need to deploy agents, and you can do it with very simple, easy-to-understand text files that make up your playbooks.

What is Docker? ^

When you consider running containers in your environment, you will most likely make use of Docker. Docker is the de facto standard in running containers across your environment. Docker containers have been around since 2013 and have been embraced across the industry for packaging applications in containerized constructs.

The great thing about using Docker for containerizing your apps is that its wide adoption generally means you have interoperability across the board with other third-party infrastructure tools and applications. Migration to the cloud and "cloud-native" applications generally means using containers.

Containers are a much leaner and more efficient way of packaging applications that make running applications across many types of infrastructure achievable. Instead of having to build an entire virtual machine with the environmental components needed to run your applications, you can build everything you need for the app inside the container and simply run the container on any VM that has the Docker engine available.

Ansible and Docker together ^

You may wonder why you would want to run Ansible inside a container. Well, it comes down to being able to easily use your Ansible environment wherever you need it. Ansible does not require a significant amount of infrastructure to run. Quite frankly, spinning up an entire VM to run Ansible is overkill.

Additionally, running Ansible inside a container means you can simply build your Ansible container image and copy it across environments as needed. It also means that updates and other lifecycle management activities for your Ansible deployment are much easier. You simply build a new container image and run the new image to take advantage of new versions, components, etc.

Installing Docker in Ubuntu 18.04 ^

To begin with, you will need to have Docker running on your system. This can be as simple as using Docker Desktop for Windows or Mac as an example, or you may have a Windows or Linux server you want to use to run Docker.

For the purposes of this post, I am installing Docker on an Ubuntu 18.04 Server. To install Docker inside an Ubuntu 18.04 Server, run the following commands:

sudo apt-get update
sudo apt install docker.io -y

To verify that Docker is installed successfully and check the version, use the following command:

docker –version
Installing Docker on an Ubuntu 18.04 Server

Installing Docker on an Ubuntu 18.04 Server

Docker installed and setting Docker to run at startup

Docker installed and setting Docker to run at startup

Creating a Dockerfile ^

Next, to build a Docker container image that will be used to run Ansible, you need to build a Dockerfile. The Dockerfile contains the directives that Docker uses to build the actual Docker container image along with the specified components.

To build the Dockerfile, I am creating the file "Dockerfile" in my home directory on my Ubuntu 18.04 Server. It contains the components needed for Ansible as well as a few other modules that are used for connecting and managing a Windows environment with Kerberos.

You can add and adjust the following as needed for your environment:

FROM ubuntu:18.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
  apt-get install -y gcc python-dev libkrb5-dev && \
  apt-get install python3-pip -y && \
  pip3 install --upgrade pip && \
  pip3 install --upgrade virtualenv && \
  pip3 install pywinrm[kerberos] && \
  apt install krb5-user -y && \ 
  pip3 install pywinrm && \
  pip3 install ansible

Build the Docker Image ^

To build the Docker image using the directives found in your Docker file, use the following syntax. Don't overlook the trailing period.

docker build -t ansible:latest .
Docker image containing Ansible is successfully built

Docker image containing Ansible is successfully built

Run your Docker image with Ansible ^

To run the newly built Docker image containing Ansible in interactive mode, use the following command:

docker run -it ansible
Running the Docker container with Ansible and running the Ansible command

Running the Docker container with Ansible and running the Ansible command

Now, with the Docker container image with Ansible installed, we have a ready-made Docker container image that can be copied to any container host and used to run Ansible with all the components already installed and ready. You can also upload your image to your corporate container registry for wider distribution.

Subscribe to 4sysops newsletter!

Concluding thoughts ^

Using Docker container images for housing infrastructure automation tools like Ansible makes a lot of sense, since it offers an easy and very agile way to get Ansible up and running. The prerequisites and other Ansible modules are already installed and ready to connect to your environment. It is a lot easier and more efficient to have a container image with these types of command line tools instead of building out a full-blown VM for installing simple command line tools.

+4
avataravatar
2 Comments
  1. Harsh 7 months ago

    Thanks for Help in my issue

     

    0

  2. lion 6 months ago

    hey, 
    when running Jenkins job which needs to launch Ansible in the docker I get:
     

    Unhandled error:
     Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/ansible/utils/path.py", line 85, in makedirs_safe
        os.makedirs(b_rpath, mode)
      File "/usr/lib/python3.6/os.py", line 210, in makedirs
        makedirs(head, mode, exist_ok)
      File "/usr/lib/python3.6/os.py", line 220, in makedirs
        mkdir(name, mode)
    PermissionError: [Errno 13] Permission denied: b'/.ansible'
    
    any clues?
    Cheers
    
    0

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account