Today I have covered,
- What is Playbooks?
- How to use Playbook using YAML?
- Hands-on on Playbook.
What is the Ansible Playbook?
Ansible playbooks run
multiple tasks
,assign roles
, anddefine configurations
,deployment steps
, andvariables
. If you’re using multiple servers, Ansible playbooks organize the steps between theassembled machines
orservers
and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.It allows you to describe the desired state of your
systems
andapplications
in a declarative manner, and Ansible takes care of executing thenecessary tasks
to achieve that state.Ansible playbooks are written in YAML. YAML is a
human-readable data serialization language
. It is commonly used for configuration files and in applications where data is beingstored
ortransmitted
.YAML stands for YAML Ain't Markup Language. It is a human-readable data serialization language and is commonly used for configuration files, but it can be used in many applications where data is being stored or transmitted.
Visit my previous blog to learn about YAML
Task-01
Write an Ansible playbook to create a file on a different server
- Write an ansible playbook to create a new user.
-
name: Create a User
hosts: all
become: yes
tasks:
- name: Create a User
user:
name: rohit
- To run the above playbook use the following command:
ansible-playbook user.yml
- Output of the above playbook:
Write an ansible playbook to create a file on a different server.
name: Display of Date in Playbook
hosts: all
server: yes
tasks:
- name: Display of Date
command: date
- name: Greeting Message
command: echo "Hello World"
# Similarly we can create a file using file module
- name: Create a File
file:
path: /home/rohit/ansible.txt
state: touch
- To run the above playbook use the following command:
ansible-playbook date_play.yml
- Output of the above playbook:
Write an Ansible playbook to install docker on a group of servers.
-
name: Playbook for Installing Docker
hosts: all
become: yes
tasks:
- name: Install Docker
apt:
name: docker.io
state: latest
- name: start and enable docker
service:
name: docker
state: started
enabled: yes
- To run the above playbook use the following command:
ansible-playbook docker_install.yml
- Output of the above playbook:
- Check Node_1:
- Check Node_2:
Task-02
Ansible playbooks:
An Ansible playbook is a
structured
andhuman-readable document
written in YAML (YAML Ain't Markup Language) that defines aset of tasks
and configurations to be performed on remote servers. Playbooks are the heart of Ansible automation and allow you to define the desired state of your systems and applications.YAML Syntax:
Playbooks
are written in YAML format, which is ahuman-readable markup language
. YAML uses indentation and colons to structuredata hierarchically
. Indentation is crucial in YAML to define thenesting levels of data
.Play: A
playbook
starts with alist of plays
. Each play targets a specific group of hosts and defines a set of tasks to be executed on those hosts.name: A user-friendly name for the play.
hosts: The group(s) of hosts this play will target. Host groups are defined in the inventory file.
become: Indicates whether privilege escalation is required to perform tasks (using sudo).
tasks: List of tasks to be executed in this play.
For Example;
---
- name: My First Playbook
hosts: web_servers
become: yes
tasks:
# Tasks will be defined here
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
name: A description of the task.
module: The Ansible module to use for the task (e.g., apt, yum, file, copy, etc.).
module_arguments: Arguments specific to the module being used.