Ansible Playbook |In this article we learn Ansible Playbook Example and create ansible playbook hosts .In previous
article we have seen how to execute an ansible task using Ad-hoc command.We
also knew that Ad-Hoc command is very useful to execute simple task like reboot the server, changing permission of
files and so on.But when we want to execute multitask at a time.Then ansible
playbook takes a vital role to execute multitask at a time to configure and
deployement on the remote machine.
Ansible
playbook is written by YAML language which is very simple language other than
XML and JSON format.It is easy to understand for human being.
Creation of ansible playbook
Before creating
ansible playbook we need to update the inventory file first which is vey necessary
to gathering the details of the remote
machine on which ansible tasks will be executed.If you want to change default inventory location ,you can
specify the location of the inventory file using ‘-i’ switch.
After that we need to create ansible playbook hosts .In
this tutorial you have used one host named host01.put its
hostname in an inventory file call our inventory myhosts.
echo
“host01 ansible_ssh_user=root” >> myhosts
How to run ansible playbook
To demonstrate ansible playbook example first
we have create one file called yum.yaml in which ansible tasks will be written
using YAML language.
To
create blank file using the below command
touch yum.yaml
Now we
write code to execute the task.This is single task code.
---
- hosts: host01
tasks:
- name: ensure
latest sysstat is installed
yum:
name: sysstat
state: latest
To run the
playbook use ansible-playbook command
Ansible-playbook –i myhosts
yum.yaml
Ansible Playbook Example |
Ansible should
return “changed=1”indicating that the change has been occurred
·
‘---‘ : start of the YAML file
·
hosts: To un the tasks on which hosts.
·
Name: The task name.
·
Yum: Specify the module name
·
Name:
argument of the specified module.
If want to
uninstall the package use state=absent.
If this article is helpful to know about Ansible Playbook Example please share this article.
If this article is helpful to know about Ansible Playbook Example please share this article.
Nice Blog!1 It is very helpful and very informative and I really learned a lot from it.
ReplyDeleteDevOps Training
DevOps Classroom Training in Hyderabad