This repository provides a self-contained Ansible environment using Docker. It includes an Ansible control node and a target managed node, allowing you to practice Ansible playbooks without installing anything on your host machine other than Docker.
- Docker
- Docker Compose
-
Start the environment:
docker-compose up -d --build
This will start two containers:
ansible-tutorial-control-1: The control node where you will run Ansible commands.ansible-tutorial-target-1: The target node that Ansible will manage.
-
Access the Control Node:
docker-compose exec control bashYou are now inside the container with Ansible installed. The project directory is mounted at
/ansible. -
Run Example Playbooks:
Verify connectivity with the ping playbook:
ansible-playbook playbooks/01-ping.yml
Install Nginx on the target node:
ansible-playbook playbooks/02-setup-webserver.yml
Create multiple users using loops and variables:
ansible-playbook playbooks/03-create-users.yml
Demonstrate handlers (restart service on change):
ansible-playbook playbooks/04-handlers-demo.yml
Demonstrate conditionals:
ansible-playbook playbooks/05-conditionals.yml
Read and print Ansible facts:
ansible-playbook playbooks/06-read-facts.yml
Filtering for Specific Facts Since the full output is usually hundreds of lines long, you can use the filter argument to find exactly what you need:
ansible all -m setup -a "filter=ansible_distribution*"OS name and versionansible all -m setup -a "filter=ansible_eth*"Network interface detailsansible all -m setup -a "filter=ansible_memtotal_mb"Total RAM availableansible all -m setup -a "filter=ansible_processor*"CPU detailsUse an Ansible Role to configure Nginx:
ansible-playbook playbooks/07-use-role.yml
docker/: Contains Dockerfiles for the control and target nodes.playbooks/: Contains example Ansible playbooks.inventory: Defines the hosts that Ansible manages.ansible.cfg: Ansible configuration file.docker-compose.yml: Defines the services and their relationship.
If you encounter SSH issues, ensure that the control container has successfully copied its SSH key to the target container. You can verify this by trying to SSH from control to target:
docker-compose exec control ssh targetIf it asks for a password, something went wrong with the setup command in docker-compose.yml. You can manually fix it by running:
docker-compose exec control sshpass -p 'root' ssh-copy-id -o StrictHostKeyChecking=no root@target