Ansible Playbook:
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
For Free, Demo classes Call: 7798058777
Registration Link: Click Here!
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.
.yml
.yaml
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.
For Free, Demo classes Call: 7798058777
Registration Link: Click Here!
Write a Sample Playbook:
vim test.yml
– name: play1
hosts: all
remote_user: root
tasks:
– name: Ping all hosts
ping:
:wq
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
Playbook Execute:
Syntax:
$ansible-playbook <playbook name> -i <inventory file>
Eg
$ansible-playbook test.yml -i /etc/ansible/hosts
Ansible Playbook Terminologies:
Control Node:
The machine where Ansible is installed. It responsible for manage all client system listed in inventory file.
Managed Nodes:
A list of host that is managed by the control node
Playbook:
A Playbook a file where we can type the code in yml format which executed on client systems.
Inventory:
In inventory file have list of all client/server system which we want manage though the ansible automation.
Task:
Task is an action which we want to execute on the client/server listed on inventory file.
Module:
Module is simple code or program which perform specific action on client and gather information
Role:
Roles are ways to automatically load some variable files, tasks, and handlers based on known file structure.
Play:
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
Green:
ok – If that is correct.
Yellow:
changed – it show update are completed on remote system successfully as per the assigning in task.
Red:
FAILED – Red indicate task is failure to execute on target system as per assigning on playbook.
White:
It comes with multiple parameters
For Free, Demo classes Call: 7798058777
Registration Link: Click Here!
Copy file from local to Remote system
– name: Play1
hosts: web
remote_user: root
tasks:
– name : copy file local to remote
copy:
src : /home/info
dest : /mnt/info
owner : sachin
group : imbgrp
mode : ‘0644’
Change file permission
–name: Play1
hosts: web
remote_user: root
tasks:
– name: File permission
file:
path: /home/pune
owner : sachin
group: imbgrp
mode: ‘0644’
Create Directory
–name: Play1
hosts: web
remote_user: root
tasks:
– name: create directory with perm
file:
path: /home/database
state: directory
Delete file
–name: Play1
hosts: web
remote_user: root
tasks:
– name: remove file or directory
file:
path: /home/doc.txt
state: absent
– name: remove directory
file: path: /etc/india
State: absent
Change selinux mode permanently
–name: Play1
hosts: web
remote_user: root
tasks:
– name: change selinux mode from file
lineinfile:
path: /etc/selinux/config
regexp: ‘^SELINUX=‘
line: SELINUX=disabled
Install package, start and enable service
-name: Play1
hosts: web
remote_user: root
tasks:
– name: install Apache server
yum:
name: httpd
state: latest
– name: enable and start Apache server
service:
name: httpd
enabled: yes
state: started
Start and enable firewall service:
–name: Play1
hosts: web
remote_user: root
tasks:
– name: firewalld enabled and running
service:
name: firewalld
enabled: true
state: started
Add service into a firewall:
–name: Play1
hosts: web
remote_user: root
tasks:
– name: open firewall port
firewalld:
service: http
immediate: true
permanent: true
state: enabled
Create Tar archive
–name: Play1
hosts: web
remote_user: root
tasks:
– name: Compress Directory contents
archive:
path: /usr/sbin
dest: /mnt/backup.tar
format: tar
Add user account
–name: Play1
hosts: web
remote_user: root
tasks:
– name: Add a simple user called harsh
user:
name: harsh
comment: harshad
Add Group Account:
–name: Play1
hosts: web
remote_user: root
tasks:
– name: Add a group called developer
group:name: developer
state: present
Add User into a group
–name: Play1
hosts: web
remote_user: root
tasks:
– name: Add a user john and add them to
a group developer
user:
name: john
groups: developer
append: yes
Delete user account:
–name: Play1
hosts: web
remote_user: root
tasks:
– name: Remove janedoe
user:
name: janedoe
state: absent
remove: yes
Remove Group Account
–name: Play1
hosts: web
remote_user: root
tasks:
– name: Remove developer group
group:
-name: developer
state: absent
Author:
Abhijeet Dahatonde
SevenMentor Pvt Ltd.