Hello, everyone In this Article we are going to learn How we Can Use Ansible Playbook in a very simple and easy step-by-step approach.
Ansible Playbook Tutorial
Ansible Modules
When we are working with Playbook in ansible that will be either in .json or .yml format
There are 2000+ modules in ansible it is not possible to explain each module soo we are making one article we will teach in this article that how we can work with different modules and how we can implement the modules in the daily life activities.
After reading this article we are sure that you have a very good understanding of the modules that are there in ansible and you can work with any module in ansible
There is no need to learn the functionality of every module we will work with the modules according to our need.
1 How to get a list of modules that are available – When we run the ansible-doc command with -l option then we will get the list of all modules that are available in the ansible.
# ansible-doc -l
2 How to count the total number of modules – If we want to know the total number of modules available then we can add wc -l command with ansible-doc when we run this command we can see that the total number of modules are there in the ansible currently we have 3400 total modules available in the ansible.
# ansible-doc -l | wc -l
3 How to get documentation for a specific module = If we want to know the documentation for the specific module then we can specify the module name with the ansible-doc command. This will give the Examples/ Description etc. information for the module
# ansible-doc copy
Now we are learning How we can use the Modules using the ad-hoc command in Ansible
In this we are taking the example for the copy module Step by step I will teach how we can use the modules for the ad-hoc command
# ansible-doc copy = Run this command to get the full documentation for the copy module in ansible with the examples
First this will tell the brief introduction what is use of the copy module in ansible.
Then this will tells that what ate the Options available for the module
After that this will tell us that all the detailed examples of how we can use the copy module in the ansible .
After reading the Usage for the copy module and option come to the examples section in the doc
In the examples section we can see that there are large number of examples are there in my case just to teach I’m taking the Copy file with owner and permissions example
Here we can see that there are different options are there now we are going to learn how we can use this options in the ansible ad-hoc command
Now First let us see that where is the host file is located in the host file we added the IP address for the Target machine where we want to run the modules in ansible
# ansible –version = With this command we can see there where is the Configuration file is located
Our configuration file is located at /etc/ansible/ansible.cfg . Now when we see this configuration file we will come to know that here is our host file is located
After viewing this file using # cat /etc/ansible/ansible.cfg file we come to know that our host file is located at /etc/ansible/hosts
after doing # cat /etc/ansible/hosts command we can see that our target IP machine is added here
We are working with the Worker-1-Node Machine
When we write Playbook there are 3 formats
1 – Single Line Format –
# vi SingleLineFormat.yml – We are writing the Module in the Single Line Format here the name can be of anything but the extension must be in .yml / .yaml format
Where the copy and yum are the modules name here we can get the information about these examples by executing # ansible-doc copy & # ansible-doc yum command.
In the hosts section we are defining in which host we are executing the Playbook and in the task section, we are defining what modules we need to execute with the options that require to run those modules.
How to execute PlayBook – We can execute the Playbook using the below command. here we are defining the Playbook name with the command
# ansible-playbook SingleLineFormat.yml
As we can see that our Playbook is successfully run this is the Example for the SingleLineFormat Playbook. Whatever tasks that we defined in the Playbook is Executed on our Worker Node successfully
2 – Dictionary format playbook
# vi DictionaryFormat.yml = We are writing the Module in the DictionaryFormat here the name can be of anything but the extension must be in .yml / .yaml format
In the hosts section we are defining in which host we are executing the Playbook and in the task section, we are defining what modules we need to execute with the options that require to run those modules.
Please note that the indentation of line must be in the above format
Where the copy and yum are the modules name here we can get the information about these examples by executing # ansible-doc copy & # ansible-doc yum command
How to execute PlayBook – We can execute the Playbook using the below command. here we are defining the Playbook name with the command.
# ansible-playbook DictionaryFormat.yml
As we can see that our Playbook is successfully run this is the Example for the Dictionary Playbook. Whatever tasks that we defined in the Playbook is Executed on our Worker Node successfully
3 – Multi Line format playbook
# vi MultiLineFormat.yml = We are writing the Module in the MultiLineFormat here the name can be of anything but the extension must be in .yml / .yaml format
In the hosts section we are defining in which host we are executing the Playbook and in the task section, we are defining what modules we need to execute with the options that require to run those modules.
Where the copy and yum are the modules name here we can get the information about these examples by executing # ansible-doc copy & # ansible-doc yum command
How to execute PlayBook – We can execute the Playbook using the below command. here we are defining the Playbook name with the command.
# ansible-playbook MultiLineFormat.yml
As we can see that our Playbook is successfully run this is the Example for the MultiLineFormat Playbook. Whatever tasks that we defined in the Playbook is Executed on our Worker Node successfully.
Conclusion
If you want to learn Ansible in the most simple language then please click on this link
If you want to learn Linux in the most simple language then please click on this link
This is the complete knowledge of How we can use ansible Playbook if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on the Linux post.