Ansible Playbooks
An Ansible playbook is a YAML file that defines a set of tasks to be executed on managed nodes in a structured, repeatable, and automated…
Ansible Playbooks
An Ansible playbook is a YAML file that defines a set of tasks to be executed on managed nodes in a structured, repeatable, and automated way.
YAML is human readable, structured and easy to write making it suitable for ansible automation playbooks
A playbook = a step-by-step automation script written in YAML that tells Ansible what to do, where to do it, and how to do it.
Compared to Ad-hoc commands, playbooks can handle reusable multiple complex tasks easily without need for running commands
Components of Ansible Playbooks
i. Play: A play is the top level unit in the playbook, defining which hosts to target and the tasks to run
ii. Host: specifies target systems from the inventory
iii. Tasks: List of actions to be executed on managed nodes
Working with Ansible
Section I: User and Group Management
The playbook creates users and group across managed nodes and adds users to assigned groups looping through variables
name: Create users and groups, assign users to group
hosts: servers
become: yes
vars:
group_name: DevOps
users:
- alice
- bob
- charles
group_members:
- alice
- charles
tasks:
- name: Create users
user:
name: "{{ username }}"
state: present
create_home: yes
shell: /bin/bash
loop: "{{ users }}"
loop_control:
loop_var: username
- name: Create group
group:
name: "{{ group_name }}"
state: present
- name: Add selected users to group
user:
name: "{{ member }}"
groups: "{{ group_name }}"
append: yes
loop: "{{ group_members }}"
loop_control:
loop_var: member

Section II: Backup for Control node to Managed node
This playbook automates the process of:
- creating a compressed archive of the /etc directory on the control node
- transferring that backup to all managed nodes
The variable used indicates the source of the file, the destination of the file and the name of the backup archive respectively
name: Backup control node and distribute to managed nodes
hosts: servers
become: yes
vars:
backup_src: /etc # what to back up on control node
backup_dest: /tmp/backups
backup_file: etc_backup.tar.gz
tasks:
- name: Create backup using tar
command: tar -czf /tmp/{{ backup_file}} {{ backup_src }}
delegate_to: localhost
run_once: true
- name: Ensure backup directory exists on managed nodes
file:
path: "{{ backup_dest }}"
state: directory
mode: '0755'
- name: Copy backup to managed nodes
copy:
src: "/tmp/{{ backup_file }}"
dest: "{{ backup_dest }}/{{ backup_file }}"

Section III: Installing Applications
This playbook automates the process of:
- Installing software packages (Git and Tree) on RHEL systems
- Verifying successful installation of each package
- Displaying installed versions for confirmation
- name: Install Git on RHEL systems
hosts: servers
become: yes
tasks:
- name: Install Git package
package:
name: git
state: present
- name: Verify Git Installation
command: git --version
register: git_version
- name: Install Tree on RHEL systems
hosts: servers
become: yes
tasks:
- name: Install Tree package
package:
name: tree
state: present
- name: Verify Tree Installation
command: tree --version
command: which tree
register: tree_version

메타데이터
- post_id
- 7151ac6d87a2
- slug
- ansible-playbooks-7151ac6d87a2
- url
- https://medium.com/@odumosu.opef/ansible-playbooks-7151ac6d87a2
- canonical_url
- https://medium.com/@odumosu.opef/ansible-playbooks-7151ac6d87a2
- author_url
- https://medium.com/@odumosu.opef
- status
- ok
- fetched_at
- 2026-07-10 18:30:51