Write a playbook that gathers disk usage from all servers and writes a single report on the control node.
IntermediateHands-onWrite-an-Ansible-playbook tasks
Answer
- hosts: all
gather_facts: true
tasks:
- name: Build report on control node
ansible.builtin.template:
src: disk_report.j2
dest: ./disk_report.txt
delegate_to: localhost
run_once: true
{% for h in ansible_play_hosts %}
{% for m in hostvars[h].ansible_facts.mounts if m.mount == '/' %}
{{ h }}: {{ ((1 - m.size_available / m.size_total) * 100) | round(1) }}% used on /
{% endfor %}
{% endfor %}
Key trick → run_once plus delegate_to: localhost runs one task for the whole play, and hostvars[h] reads facts gathered from every other host.
More on Write-an-Ansible-playbook tasks
Write a playbook that installs and starts Nginx on Ubuntu web servers.BeginnerWrite an inventory with groups, a group of groups, and per-group variables.BeginnerWrite a playbook that deploys a config file from a Jinja2 template and restarts the service only if it changed.BeginnerWrite a role structure for a Node.js app and show what goes in each folder.IntermediateWrite a task that creates multiple users with SSH keys from a list.BeginnerWrite a rolling deploy playbook: 2 servers at a time, removed from the load balancer during the update.Advanced