Prerequisites:Module 5 (AWS — having an EC2) or Module 6 (Terraform — having created an EC2 with Terraform)
In a nutshell: Terraform creates the servers, Ansible configures them. You learn to automate Docker installation, project cloning, and app launch on a remote server — all with a single YAML file and one command.
The problem: Terraform creates the infra (the server exists). But who installs Docker on it? Who configures nginx? Who copies the config files? Who makes sure everything is up to date? You could do it over SSH, but what if you have 10 servers? 50?
Ansible configures and maintains what runs ON the servers. Terraform builds the house, Ansible furnishes it.
The analogies:
Inventory = the list of houses to visit
Playbook = the task checklist to complete in each house
Module = a specific action (install software, copy a file, start a service)
Idempotence = you can re-run the checklist 10 times, the result will be the same (if the paint is already done, we don’t repaint)
The key thing: Ansible is agentless — no need to install anything on the target servers. Other similar tools (Chef, Puppet) require installing a program (“agent”) on each server you want to manage. Ansible doesn’t: it simply connects via SSH (the remote connection covered in Module 1) and runs the tasks. That’s what makes it easy to get started with.
⚠️ Error Ansible requires the locale encoding to be UTF-8; Detected None? It’s common on a minimal Ubuntu image (no locale configured). Ansible refuses to start until the locale is UTF-8. Fix it with:
💡 The exact numbers vary depending on what was already installed on the server. If Docker is already present, changed will be smaller. What matters: failed=0 and unreachable=0.
You run the playbook for the first time: Ansible installs Docker, copies files, starts services. You run it again: Ansible checks that everything is already done and does nothing. Same result, no side effects.
Fenêtre de terminal
# First run
ansible-playbook-iinventory.inisetup.yml
# changed=4 (the exact number depends on what was missing on the server)
Roles: Reusable and organized playbooks. Like functions. We won’t create any in this course, but know that they exist (and Ansible Galaxy provides thousands ready to use).
Create the file with nano deploy.yml and paste this content:
The {{ variable }} syntax is how Ansible inserts a variable’s value. It’s like $variable in bash or ${var} in Terraform — each tool has its own syntax.
# (the numbers vary by server; re-run, the playbook should end with changed=0)
💡 The community.docker.docker_compose_v2 module ships with the full ansible package (the one installed by apt install ansible). If you use ansible-core alone, install the collection: ansible-galaxy collection install community.docker.
Open http://YOUR_EC2_IP — the app is running.
💡 If “unreachable”: check that the IP is correct, that the Security Group allows SSH (22), and that the .pem key is correct.
Q: What is Ansible?
A: A configuration management tool. It configures servers (installing software, copying files, starting services) in an automated and reproducible way.
Q: Ansible vs Terraform?
A: Terraform creates infrastructure (servers, networks). Ansible configures what runs on them (software, files). They are complementary.
Q: What is idempotence?
A: The ability to execute an operation multiple times with the same result. If Docker is already installed, Ansible won’t reinstall it.
Q: Why is Ansible “agentless”?
A: No need to install software on the target servers. Ansible connects via SSH. This simplifies setup compared to Chef/Puppet which require an agent.
Q: What is a playbook?
A: A YAML file that describes a list of tasks to execute on servers. It’s the main file you write and run.
Q: What is an Ansible inventory?
A: The file that lists the servers Ansible will act on. It contains IP addresses or hostnames, organized in groups (web, db, etc.). Ansible connects via SSH to each machine in the inventory to execute tasks.
Q: What is an Ansible role?
A: A way to organize a playbook into reusable components. A role bundles tasks, files, templates, and variables related to a function (e.g., a “docker” role that installs and configures Docker). You can reuse the same role across multiple playbooks.
Use Ansible modules, not command/shell. Modules (apt, service, copy) are idempotent. command: apt install nginx is not — it will reinstall on every run.
Test with --check first.ansible-playbook --check simulates execution without modifying anything (dry run). Like terraform plan.
Encrypt secrets with Ansible Vault. Passwords, API keys -> ansible-vault encrypt secrets.yml. Never commit secrets in plain text.
Organize into roles as things grow. A 500-line playbook is unmaintainable. Roles break things into reusable blocks.
Ansible Vault: encrypt secrets in playbooks — essential as soon as you have passwords in your Ansible files
Ansible Roles: organize your playbooks into reusable modules — essential when managing more than 2-3 servers
Ansible Galaxy: community role library — instead of writing everything yourself, reuse existing roles
Automate the trigger: instead of running ansible-playbook deploy.yml by hand, you can trigger it from the pipeline’s deploy job (see Module 6, step 9). Ansible describes the how of configuring the machine; the pipeline decides the when. In companies the two are combined: CI builds the image, the deploy job runs the playbook.