Ansible playbook is very useful feature to write all command into a yml script file with sequence and sent to the remote system to execute. It is best solution for the big task and complex environment, due to one-time write a playbook we can execute it on multiple time as per the requirement, playbook can make complex task very easy to execute and repetition work. Also we can execute it on multiple system simultaneously.
- Playbook push configuration Ansible what to execute.
- Playbook contains a list of task
- Playbooks contain the steps which user want to execute on a remote systems.
- Playbooks are run sequentially.
- All playbooks written in yaml format
- Playbook define set of play with activates to run on hosts.
- A task is a single action perform on a host
How to write playbook:
We can create playbook with any name, not necessary to create with any specific file name and extension. But best is that create play book with .yml extension. If we create file any other extension then we can’t find difference between normal file and yml file as well as it can’t support auto spacing whenever we type the playbook code, so recommended that create playbook with yml extension to easy of write.
The file starts with: –
What is yaml:
YAML is a human understandable written code, which is used to store in file (script) and transmit over a network to install, configuration applications and much more on remote system .yml is a one type script that store locally and transmit on remote to execute as per requirement. YAML mean Yet Another Markup Language very easy to write a playbook and understand.
YAML is very sensitive:
Whenever we write playbook be cautions with space in a script, it also not allow tab key to write coding, so as a new user it feel complex for the few program but after that it become familiar, after write any task need to start another task start with exact space like previous task it shown in following playbook examples.
Basically Playbooks consists of three sections
- Host declaration
- Variable declaration (optional)
- Action / Tasks Declaration
1. Host declaration –
In host file declare the address or name of available client/server system where we want to run and execute ad-hoc command and playbook..
2. Variable declaration (optional) –
It is optional, we see some examples how to declare a variable below.
3. Action / Tasks Declaration –
We can use different type parameters as per the requirement of execute on client, we can use notify, actions, name as per the condition specify on the playbook.
Write a Sample Playbook:
– name: play1
– name: Ping all hosts
Verify the playbook syntax errors:
Before run program we can check the syntax error in playbook, to check syntax error in playbook we can run following command. It program is accurate syntax then it print only the name of program otherwise it show error msg in red colours:
$ansible-playbook test.yml –syntax
$ansible-playbook <playbook name> -i <inventory file>
$ansible-playbook test.yml -i /etc/ansible/hosts
Ansible Playbook Terminologies:
The machine where Ansible is installed. It responsible for manage all client system listed in inventory file.
A list of host that is managed by the control node
A Playbook a file where we can type the code in yml format which executed on client systems.
In inventory file have list of all client/server system which we want manage though the ansible automation.
Task is an action which we want to execute on the client/server listed on inventory file.
Module is simple code or program which perform specific action on client and gather information
Roles are ways to automatically load some variable files, tasks, and handlers based on known file structure.
One play book can have multiple play, each play have implementation of specific automation start to end of script.
How to understand Ansible Output:
The ansible playbook after run displayed output in 4 colours, each colour have specific meaning
ok – If that is correct.
changed – it show update are completed on remote system successfully as per the assigning in task.
FAILED – Red indicate task is failure to execute on target system as per assigning on playbook.
It comes with multiple parameters
Copy file from local to Remote system
– name: Play1
– name : copy file local to remote
src : /home/info
dest : /mnt/info
owner : sachin
group : imbgrp
mode : ‘0644’
Change file permission
– name: File permission
owner : sachin
– name: create directory with perm
– name: remove file or directory
– name: remove directory
file: path: /etc/india
Change selinux mode permanently
– name: change selinux mode from file
Install package, start and enable service
– name: install Apache server
– name: enable and start Apache server
Start and enable firewall service:
– name: firewalld enabled and running
Add service into a firewall:
– name: open firewall port
Create Tar archive
– name: Compress Directory contents
Add user account
– name: Add a simple user called harsh
Add Group Account:
– name: Add a group called developer
Add User into a group
– name: Add a user john and add them to
a group developer
Delete user account:
– name: Remove janedoe
Remove Group Account
– name: Remove developer group
SevenMentor Pvt Ltd.