- New features in VMware vSphere 8 - Mon, Dec 5 2022
- Split-brain DNS deployment using Windows Server DNS policy - Wed, Nov 30 2022
- Veeam Backup for Microsoft 365—Why you need to back up your M365 data - Tue, Nov 15 2022
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:
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 .
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
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.