Create an Ansible playground with Docker
Dive into the world of Ansible by building your own playground environment with Docker. This blog post will guide you through the steps to set up a Docker environment. Get ready to explore, experiment, and enhance your Ansible skills in a controlled and dynamic environment.
Why use Ansible with Docker ?
Using Ansible inside Docker containers offers several advantages:
- Local Environment Convenience: By encapsulating Ansible within Docker containers locally, you mitigate the risk of inadvertently disrupting external systems. Enjoy the freedom to experiment and iterate without concerns about impacting servers utilized by others.
- Effortless Installation and Reinstallation: Docker simplifies the deployment of Ansible, allowing for swift installation and reinstallation as needed.
- Ideal for Testing Scenarios: Testing your Ansible playbooks becomes easier with Docker containers, ensuring flawless performance in various scenarios.
- Seamless Integration with CI/CD Pipelines: Automating playbook tests is effortless when combined with CI/CD pipelines such as GitLab CI/CD or Jenkins. Dockerized Ansible playbooks fit seamlessly into your automated testing workflows, enhancing efficiency and reliability.
Let’s build the Ansible environment
Set the Docker images
First, let’s create two Docker images for two distinct environments:
- The first environment will have Ansible installed
- The second environment will serve as the target for Ansible playbooks
Let’s install Ansible:
FROM alpine:latest
RUN apk add ansible openssh
CMD ["tail", "-f", "/dev/null"]
And now let’s set up the targets and install openssh-server
and python3
:
FROM alpine:latest
RUN apk add openssh-server python3
RUN adduser -D ansibleRUN echo "ansible:ansible" | chpasswd
RUN ssh-keygen -A
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
Import the playbooks
Next, let’s create the necessary files within a directory named code_ansible/
. We’ll then mount this directory to the Ansible container.
Here’s an example of what a host file might look like:
[targets:vars]ansible_python_interpreter=/usr/bin/python3
[targets]target
And here is an example of playbook:
---- hosts: targets remote_user: ansible
tasks: - name: My first playbook debug: msg: "A super debug message !"
- name: Create a file called "hello.txt" on all the servers copy: dest: /tmp/hello.txt content: My super content !
Run the containers
Let’s create the containers with a docker-compose.yml
file:
version: '3.8'
services:
ansible: image: ansible build: context: . dockerfile: Dockerfile.ansible volumes: - ./code_ansible:/code_ansible/ working_dir: /code_ansible/
target: image: target build: context: . dockerfile: Dockerfile.target
Finally, we’ll start the containers. After that, we’ll generate SSH keys across the containers to allow connections from Ansible.
#!/bin/bash
docker-compose downdocker-compose up -d --build# docker-compose scale target=3
docker-compose exec ansible mkdir -p "/root/.ssh/"docker-compose exec ansible ssh-keygen -t rsa -b 4096 -N "" -f "/root/.ssh/id_rsa"SSH_PUBLIC_KEY=$(docker-compose exec ansible cat "/root/.ssh/id_rsa.pub" | tr -d '\r\n')
docker-compose exec target sh -c "mkdir -p /home/ansible/.ssh/"docker-compose exec target sh -c "echo '$SSH_PUBLIC_KEY' > /home/ansible/.ssh/authorized_keys"docker-compose exec target sh -c "chown ansible:ansible /home/ansible/.ssh/authorized_keys"
# Avoid to have this first SSH connection message:# TASK [Gathering Facts]# The authenticity of host 'target (192.168.176.2)' can't be established.# ED25519 key fingerprint is SHA256:AdO48xg01Oe7sslgmC6/SJoN7AzR1fCF0cz0lVzBSpM.# This key is not known by any other names.# Are you sure you want to continue connecting (yes/no/[fingerprint])? nodocker-compose exec ansible sh -c "echo 'Host * StrictHostKeyChecking no' > /root/.ssh/config"
docker-compose exec ansible sh -c "echo 'ansible-playbook -i hosts playbook.yml'"docker-compose exec ansible sh
Once the SSH keys are generated and configured, your Ansible setup will be ready to manage the target environments effectively.
Conclusion
In conclusion, the combination of Ansible and Docker offers a potent solution for managing and automating tasks with ease. By following these steps, you can harness the full potential of both tools, empowering your infrastructure with efficiency and reliability.
Practice code with the "Quick Sort" algorithm
Enhance your coding skills by learning how the Quick Sort algorithm works!
The SOLID/STUPID principles
Learn what are the SOLID and STUPID principles with examples
Create a Docker Swarm playground
Let's create Docker Swarm playground on your local machine
Setup a Kubernetes cluster with K3S, Traefik, CertManager and Kubernetes Dashboard
Let's setup step by step our own K3S cluster !
HashiCorp Vault - Technological watch
Learn what is HashiCorp Vault in less than 5 minutes !
How to internationalize an AstroJS website while maintaining good SEO ?
We will see how to create an implementation of i18n with AstroJS
Database ACID/BASE - Understanding the CAP Theorem
Learn what is the CAP Theorem in less than 5 minutes !
LFTP - Deploy an application in command line
Here we will see how to automatically deploy an application with lftp in command line.