Some Intro about Ansible
Ansible is an open-source software provisioning, configuration management, and application-deployment tool. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows. YAML is used by ansible but it's declarative for ansible. Unlike terraform has got its own declarative language. Some of the other configuration management tools are Chef, Puppet, and Salt. Under Infrastructure as Code (IaC), we have Terraform, Cloudformation, etc that are used for the same purpose. This article particularly focuses on Ansible and how to leverage it in your infrastructure and yield the best results.
For DevOps engineers/companies to perform deployments/software provisioning from their local machines without any complex setups.
Docker: You’ll need Docker installed in your local machine (we will be using Docker Desktop for Mac in this article).
Ansible Docker image: We will be adding all the environment-related settings and other dependencies that will be used in this image so that it can be used by all the folks by pulling from their internal SCM repository across the board.
Cloud environment (AWS/Azure/GCP): We will be taking AWS in this article but steps would be pretty much the same for all other cloud environments.
Setting things up
I have installed Docker Desktop on my MAC as a first step. Follow the Get Docker article to do the same on your machine.
I have created a customized ansible image for my own purpose of executing playbooks and it's available here. It’s a Alpine+python with Ansible installed. We will be using our ansible image for all the upcoming steps. You could create one container for your organization wide usage as per the requirements using my image or do it from scratch by following the article to do the same.
First, pull the image.
docker pull axldevops/ansible:v1.0
You will need to mount the playbooks directory to your ansible container as a docker volume so that you can use the playbooks that were developed locally on your MAC to be executed against the targets. I have used the sample playbooks in this Github account for this article. You will need to mount your project or Ansible playbooks directory as a docker volume.
I have cloned the folder into my MAC and changed into the playbooks directory. We will be mounting this directory to the /app directory on ansible container as a docker volume in the upcoming steps.
Let’s create the ansible.cfg file and inventory file in the playbooks directory to complete our setup
Inventory file - hosts
We will be mounting our public keys to /keys directory in the next step so we have created the inventory file accordingly.
Now that we have all the things ready, we will execute the docker run command along with the volumes option where we mount the playbooks directory and the public keys directory to the ansible container.
docker run -itd --name ansible -v $PWD:/app -v ~/.ssh/keys:/keys axldevops/ansible:v1.0
Let’s login into the container and check for the volumes
I have created an amazon Linux based t2 micro instance for the purpose of this article and have already added the hostname in the inventory file. Please set your target hosts accordingly in the inventory file. You could make use of dynamic inventory as well if you are having multiple sources like cloud providers, LDAP, Cobbler, or a piece of expensive enterprisey CMDB software.
Finally, we are now ready to execute our playbooks against the targets.
ansible-playbook -i hosts playbook.yml
ansible-playbook -i hosts remote.yml
Like this, you can kickstart your ansible journey without much effort using ansible in a docker container. You don’t need to maintain any servers for ansible like control nodes. The same code repo can be shared across the people maintaining those playbooks. All those can use the same image and collaborate easily.
Sreekar Achanta, Site Reliability Engineer - L2
He's a sweet tooth who hums constantly. He loves playing volleyball, and in his free time, he can be found listening to music or jumping in his car for a drive with his puppy named "Kutty."