This repository provides a hands-on example of understanding Ansible through progressively complex playbooks. We start with a simple playbook.yml and build up to more complex configurations. The folder filenames are self-explanatory.
- Python 3.x
- Docker
- Docker Compose
- Linux, MacOS, WSL2
- Create a Python Virtual Environment:
cd ansible_quickstart python3 -m venv venv source venv/bin/activate pip install -r requirements.txt mkdir ssh_keys
- Generate Secrets public keys for ansible:
Assuming you do not have a public key set up
Enter an e-mail you use, you might need the same pub key for other things you use as wel
ssh-keygen -t rsa -b 4096 -C "user@user.com" cat ~/.ssh/id_rsa.pub > ssh_keys/authorized_keys
- Build and Start Docker Containers:
docker-compose up -d
- Enable SSH connection of ansible:
#Test if you can ssh to the container ssh -p 2222 root@localhost #Enable port on localhost as known_hosts for machine for ansible to connect ssh-keyscan -H -p 2222 localhost >> ~/.ssh/known_hosts ssh-keyscan -H -p 2223 localhost >> ~/.ssh/known_hosts
- Easy rebuilding docker image after adjustment, remove images:
# Remove the latest two images # You can change awk cmd to head -n 2 docker image rm $(docker image list --format='{{.ID}}' | awk 'NR<=2') # Rebuild docker compose up
- Manually test if install is failing on ansible on container:
# Access runnning container # You can change awk cmd to head -n 1 docker exec -it $(docker ps -q | awk 'NR<=1') /bin/bash
├── Dockerfile
├── ansible.cfg
├── code_editor.yml
├── create_profiles.yml
├── docker-compose.yml
├── inventory.ini
├── playbook.yml
├── profiles.yml
├── requirements.txt
├── site.yml
├── software_install.yml
└── ssh_keys
└── authorized_keys- Dockerfile: Defines the Docker image for the Debian containers.
- ansible.cfg: Ansible configuration file.
- code_editor.yml: Playbook to install a code editor.
- create_profiles.yml: Playbook to create user profiles.
- docker-compose.yml: Docker Compose file to set up the environment with two Debian containers.
- inventory.ini: Inventory file listing the hosts, the debian container running.
- playbook.yml: Simple playbook to get started with Ansible.
- profiles.yml: Playbook to manage user profiles.
- site.yml: Main playbook that includes other playbooks.
- software_install.yml: Playbook to install various software.
- ssh_keys/authorized_keys: Directory containing SSH keys.
-
Run a Simple Playbook:
ansible-playbook -i inventory.ini playbook.yml
-
Run the Main Playbook:
ansible-playbook -i inventory.ini site.yml
Note 1: add -vvv to see all verbose output at the end of the command Note 2: most of the playbooks have lots of commented to clarify why it is there or not working.
- name: My first play
hosts: debian_hosts
tasks:
- name: Ping my hosts
ansible.builtin.ping:
- name: Print message
ansible.builtin.debug:
msg: Hello world- name: Create user profiles
import_playbook: profiles.yml
- name: Install and configure Code Editor
import_playbook: code_editor.yml
- name: Install remaining developping software
import_playbook: software_install.yml
This repository provides a structured approach to learning Ansible by starting with simple tasks and gradually introducing more complex configurations. By using Docker, we ensure a consistent environment for testing and deployment.
Feel free to explore the playbooks and modify them to suit your needs. Happy automating!