Chapter 05 · Ansible Playbooks
Ansible Playbooks
Writing playbooks (recap)
- A playbook is a list of plays.
- Each play targets hosts (
hosts) and runs tasks. - Execution can be controlled with
tags,when,register,failed_when,changed_when.
Managing the environment with Ansible
Ansible can orchestrate stages: provisioning, configuration, deployment, verification and switchover. At this stage, you can already build well-structured pipelines.
- name: Deploy an application
hosts: web
become: true
tasks:
- name: Fetch the source code
ansible.builtin.git:
repo: "https://example.invalid/monapp.git"
dest: /opt/monapp
version: main
- name: Dependencies
ansible.builtin.apt:
name: ["python3-venv", "python3-pip"]
state: present
- name: Service systemd
ansible.builtin.template:
src: templates/monapp.service.j2
dest: /etc/systemd/system/monapp.service
notify: reload systemd
handlers:
- name: reload systemd
ansible.builtin.command: systemctl daemon-reload
What about register ?
In Ansible, `register` allows you to capture the result of a task and store it in a variable, so that it can be used in subsequent tasks.
- name: Demonstrate register
hosts: all
become: false
tasks:
- name: Check system uptime
ansible.builtin.command: uptime
register: uptime_result
- name: Display registered result
ansible.builtin.debug:
var: uptime_result
Execution
ansible-playbook -i inventory.ini register.yml
Controlling tasks
- Tags: run a subset:
--tags install - Limit: target hosts:
-l node1 - Serial: deploy in batches
- Check/diff: simulate execution and see changes
ansible-playbook -i inventory.ini site.yml --check --diff
ansible-playbook -i inventory.ini site.yml --tags "install,config"
ansible-playbook -i inventory.ini site.yml -l node1
What about serial ?
Goal: Demonstrate that Ansible can execute tasks on multiple hosts in parallel or process hosts sequentially, one at a time.
Create a file named default.yml and
- name: Demonstrate parallel execution
hosts: all
gather_facts: false
tasks:
- name: Display start message
ansible.builtin.debug:
msg: "START on {{ inventory_hostname }}"
- name: Wait for 5 seconds
ansible.builtin.wait_for:
timeout: 5
- name: Display end message
ansible.builtin.debug:
msg: "END on {{ inventory_hostname }}"
Launch it, look at the resultand create another file : serial.yml
- name: Demonstrate sequential execution
hosts: all
gather_facts: false
serial: 1
tasks:
- name: Display start message
ansible.builtin.debug:
msg: "START on {{ inventory_hostname }}"
- name: Wait for 5 seconds
ansible.builtin.wait_for:
timeout: 5
- name: Display end message
ansible.builtin.debug:
msg: "END on {{ inventory_hostname }}"
Launch this new playbook, and compare the result
For more on gather-facts, please look at this BONUS
Integration with cloud providers
Ansible can:
- Provision resources (VMs, networks) using cloud collections/modules.
- Then configure the provisioned machines.
Common pattern: “provisioning” (cloud) + “configuration” (OS/application). Keeping the two separate makes life considerably easier.
Using Ansible in Windows environments
- Connection is typically via WinRM (rather than SSH, although SSH is also possible).
- Dedicated Windows modules (services, features, registry, etc.).
- Pay attention to prerequisites and WinRM configuration (security, certificates).
Practical exercise: controlled deployment
- Revisit
site.yml(Chapter 3) and add tags to the tasks:install,config,service. - Run only
install. - Run only
configwith--diff. - Try
serial: 1to deploy to one node at a time.
Minimal example
- name: Web baseline + deployment
hosts: web
become: true
serial: 1
vars:
index_msg: "Hello from Ansible (Chapter 5)"
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
tags: [install]
- name: Deploy the home page
ansible.builtin.copy:
dest: /var/www/html/index.html
content: "<h1>{{ index_msg }}</h1>\n"
owner: root
group: root
mode: "0644"
notify: restart nginx
tags: [config]
- name: Start and enable nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
tags: [service]
handlers:
- name: restart nginx
ansible.builtin.service:
name: nginx
state: restarted
How to use
ansible-playbook -i inventory.ini site.yml --check --diff
ansible-playbook -i inventory.ini site.yml --tags install (run installation only)
ansible-playbook -i inventory.ini site.yml --tags config --diff (execute only config with diff)
ansible-playbook -i inventory.ini site.yml --tags service (run the service tasks only)
ansible-playbook -i inventory.ini site.yml -l node1 (target one node only)
QCM
1)
--check is used to:
- A. Run faster
- B. Simulate without applying changes (as far as possible)
- C. Create an inventory
2) Tags are used to:
- A. Run a subset of tasks
- B. Encrypt variables
- C. Replace YAML
3) For Windows, Ansible often uses:
- A. WinRM
- B. SSH only
- C. ICMP
Answer key
1) B · 2) A · 3) A