Ansible

 

////////////////////////////

https://linuxdady.com/multitask-playbook-in-ansible/

////////////////////////////

How to write a multitask playbook in ansible

The biggest beauty of ansible playbook is we can perform multiple and complex task easily. So, use of multitask playbook become very useful when we want to perform multiple task at a time that’s why in this tutorial we are going to learn how to write multitask playbook in ansible.

Multitask playbook in Ansible

Let understand meaning of multitask with scenario suppose we have three groups backup, web, dev. In backup group suppose we take our daily routine backup and in web and dev group suppose we do package and build update on daily basis. So, in this scenario we have two option we can write three playbook and run one by one on every group that will consume our time as well our resources and another one is we can write multitask playbook.

In which we will define all task separately according to group like in backup group we will perform backup task and dev group we will do deploy and so on that will save our time means no need to run multiple playbook. We can perform our task by executing a singly playbook. Let take a demo how to write multitask playbook in ansible. First make inventory file with group accordingly as shown below.

[Click & Read:–  Ansible playbook example with explanation ]

[Click & Read:–  Ansible ad hoc method in simple way for beginners]

After making inventory file we are going to write ansible playbook with name multitask.yml.

multitask playbook in ansible

In this playbook we have written different task for different group as show in above fig. Now we are going to execute this playbook. First we will run in dry mode to check every thing is ok or not then we will run finally on all group as define in inventory file. 

Conclusion

In this tutorial we have explained how to write multitask playbook in ansible and what are the benefit of using multitask playbook If anyone have query write me in comment box. I will try my best to resolve that query.

Interview question related to ansible playbook

  1. How many tasks we can define in a playbook?

Ans. We can define N number of tasks but be sure once with ansible control node configuration.

  1. What is fact in ansible?

Ans. Please write answer in comment box.

////////////////////////////////////////////////////////

Scenario based ansible playbook example

Ansible playbook example will help to understand the use case of playbook. But before doing example of ansible playbook we should understand the basic difference between ad hoc and playbook. Which one we should prefer between playbook and ad hoc method and why?

Playbook versus Ad hoc method

So, let’s be familiar with playbook and ad hoc method and differentiate it first. 

ansible playbook example

Ansible playbook Format

From above diagram I think we are well known about ad hoc and playbook method. We can  use anyone depends on our requirement. But for repetitive task playbook is more preferable method because to type command every time become hectic task in ad hoc method. So, before writing playbook we should try to understand its format means how to write playbook and its basic parameter. I have written a very simple playbook to explain all the parameters of playbook.

hosts parameter have the list of host name or group name where we want to perform our task. For better understanding suppose we have web group with multiple machine then this parameter will look into all machines in web group only.

This parameter is used to display anything in fly mode while our playbook is running. Suppose we have three tasks in our playbook, and we want to print a message when our first task become complete then this parameter come in scenario. We can ignore this parameter depend on choice it’s not mandatory.  

tasks parameter is used to define action that you want to perform on your host or host group. Suppose we want to install yum server that means we are installing our required package by yum module. This is a type of task. We can define N number of tasks depend on our requirement.

Ansible playbook example

Now we are going to take an example of ansible playbook. We will write our playbook and then learn how to run that playbook in change mode and dry mode.

Example 1

In this ansible playbook example we will create a very basic and simple playbook. By this playbook we will create a file demo.txt in /tmp directory on our all machine which belongs to web group then we will copy it into /mnt directory with example.txt name.

We created our first playbook now there are two way to execute this playbook.

  1. Dry Mode (Check Mode)
  2. Change Mode

Dry Mode

In ansible playbook dry mode is also called check mode. For better understanding about dry mode let’s take an example. Suppose I have write a playbook and I am not sure how to react this playbook on managed node or you can say I am in doubt this playbook will run properly or not on manage nodes. So, in my mind a question raised I should  execute this playbook on managed node, but my playbook changes should not impact on mange node means dry mode is a way by which we can execute playbook in fly mode without doing any changes on manage nodes. 

We can also use -C option to execute the playbook in dry mode.

Change Mode

Change mode means suppose we have write a playbook and we are sure about that playbook action then we will run that playbook on change mode. For better understanding you can say if you will run playbook in change mode then it will make the changes on our manage node. Wrong or write action does not matter it will perform action that will be written in playbook.

Ansible playbook example 2

Suppose we want to install postfix package on all machines in web group as well we want to enable and restart the service of postfix. 

When write a playbook be careful about indentation of yml language because if we will do indentation wrong then while executing playbook it will show syntax error.

ansible playbook example with scenario

[Click & Read:–  Ansible basic modules that should be learn must]

[Click & Read:–  Ansible ad hoc method in simple way for beginners]

Ansible playbook example 3

Now lets move a little bit next and take a scenario suppose I want to install webserver on 1000 host and I want to configure it on port 80 as well add port into firewall service.

Conclusion

In this tutorial we have tried to cover ansible playbook example and playbook basic format that will help to make easy to understand concept of ansible playbook. But still anyone have queries write me in comment box. I will try my best to resolve them.

Ansible scenario based interview question. 

  1. What are the difference between dry mode and change mode ?
  2. Interviewer always ask scenario base interview question on playbook. Like in my last interview interviewer asked Deepak does you have any idea about how to write playbook. Then I told him yes. Then he told me his requirement.
  3. He has 1000 server and he want to add user with name MAX on all server.
  4. After adding user on all server there is a file demo.txt give its ownership to MAX and add any content into this file but before adding content take backup of current file.       
  5. Second question interviewer asked me Deepak we cannot open ssh port then should I prefer ansible or not and why?
  6. The next interview create a scenario related to server. He want to install ftp package over 1000 server but ssh port is not allowed to open in his company is it possible to install ftp package if yes, how and if not then why?

ANS. Please write your answer in comment box.

////////////////////////////////////////////////////////

Understand basic modules in ansible

What are the use cases of modules in ansible and why you cannot say I am perfect in ansible without knowledge of modules? How many modules should I know to say I am perfect in ansible. All  these stuffs we are going to discuss in this tutorial.  So, before moving next all these stuffs should be clear because ansible modules play very important role in life of devops engineers.

Which modules in ansible should I learn?

There are tons of modules in ansible and no need to learn all these modules because suppose we are using ansible only for OS level then why we should learn switch, router, firewall modules. So now my question is after how many modules can I say yes I can work very well on ansible.

If I know 15-20 modules then can I say yes, I am perfect in ansible. There are lots of question that’s raising in my mind. But to be perfect in ansible is depend on you infra not on count of modules. Suppose in you infra you are using only 15 modules then you can say our infra requirement is this and we are using this module for this task.

Prerequisite for lab

Control Node IP :– linuxdady.com (172.31.86.202)
Manage Node IP :– 172.17.0.2, 172.17.0.3, 172.17.0.4
Ansible should be installed on Control node.
Control node and Mange node should be password less.

[Click & Read:–  How to install ansible step by step very easy]

[Click & Read:–  Ansible ad hoc method in simple way for beginners]

Now we are going to create a simple inventory file to perform lab. Kindly have a look of below static inventory file and change it accordingly.

Remember one thing when you will install package of ansible all default modules will install automatically. Suppose if we need any particular module then we can write our custom module. Here we are going to discuss basic modules in ansible that we are using in our daily routine. Let’s do basic modules of ansible.

Module 1: Command modules in ansible

The command module have the capability to execute command on manage or targeted nodes. We can run all our OS level commands parallelly from control node to multiple or single manage node as required. Suppose we want to create a directory on all manage node then simply we can create with the help of mkdir command no need to go on individually machine. Just we will execute this command from control node and command will executed on all managed nodes automatically. Syntax of command module.

modules in ansible

-k :- Use -k option if your machine is not password less.
-u :- Option is use for username from that user we are going to perform action on manage node

Example 1

Now we are going to push changes on single machine you can push changes on group or over the group just change IP address with your group name in below commands.

example of ansible module

Suppose we are not well aware about any module in ansible then we can read docs related to that module. Ansible is providing their official docs related to modules. Suppose we want to read command module then use below ansible syntax.

ansible modules docs

So, we can take help of ansible docs to explore any module.

Module 2: raw module

raw module is also used to execute command on targeted node. But the workflow of raw module is little bit different from command module means raw module is not depend on python without python we can execute our commands simply on targeted node by establishing ssh connection. But in case of command module python should be installed on targeted nodes. Raw module also have one more beauty we can also run multiple command at a time. Let’s understand it by example.

modules in ansible syntax

Example 1

As shown in below example. We have executed three commands at a time on web group means we can execute multiple command at a time according to our requirement.

ansible modules command

Module 3: Shell module

Shell module is identical to command module. But with the help of shell command we can run highly complex command with special shell operator like < > | ; & $HOME etc. But command module is more secure as compare to shell modules. So, give preference to command module while execute command. Shell module is mainly used to execute script means suppose you want to execute a script on all manage node then copy it and use shell module to execute that scrips.

shell modules in ansible

Command VS raw VS shell modules in ansible with difference

Let’s have a difference between command, raw and shell modules. Because these modules create lots of confusion.

ansible module comparison

Module 4: Copy module

Ansible copy module is used to copy files and directory from the local machine to remote/managed node. We can manage lot of task with copy modules like copy, backup, permission, ownership, group ownership, can insert content into file etc. This is versatile and easy to use modules.

Example 1

For example, suppose we want to copy a file from remote server to all destination hosts.

This command copy test.sh file on all machine which is list in web group. Login to all machine and check our file has been copied or not properly.

As shown in below diagram file has been copied as required. Let’s have a look of below diagram.

modules in ansible

Example 2

Suppose we have edit some content into file and we want to copy this file at manage node. Suppose file is already existed at remote destination. Now we are going to copy this and then we will take backup of existing file.

Example 3

Suppose we want to write content in a file at all mange node at a time. Then use content option.

Similarly, we can change file permission, ownership and group ownership according to our requirement.

If you want to know more use ansible official docs. There will be detail knowledge with option and example. Use below command for copy module docs.

Module 5: fetch module

Fetch module works like copy module but in reverse direction means copy module use to copy from local host to managed node. But fetch module copy file from manage node to local host. It will copy all file at a time from all manage node and it will manage their backup by their hostname. But mind it if file will already exit it will overwrite existing file.

Copy VS Fetch module

copy vs fetch module

Module 6: File module

With the help of file module, we can perform task related to mkdir, touch, chown, chgrp, rm, soft link, hard link, Selinux policy etc. If we are well aware about file modules in ansible then there is no need to use above given commands. Syntax of file module.

file modules in ansible

Let check demo directory has been created or not.

Suppose we want to delete demo directory then use absent parameter.

Let create a file with file module.

Create a soft link with file module.

Now check its working or not.

list copied file

Now let create hard link.

Module 7: Package module

Package module is used to install, remove and to update package into Linux based OS like RedHat, Centos, Ubuntu, Debian etc. Syntax of package module.

package modules in ansible

Module 8: Yum module

Yum module is identical to package module but little bit simple to use then package module. BY this module we can installs, upgrade, downgrades, removes, and lists packages and groups with the `yum’ package manager. Syntax of yum module.

Module 9: Service module

Service module in ansible is used to manage the state of your service means you can start, stop, restart, reload, enable, disable your service according to your requirement.

For more visit ansible official docs.

Module 10: Lineinfile module

Lineinfile module is mainly used to edit the content of file means by this module we can comment and uncomment specific line, we can replace, delete any line based on regexp. We can add or delete line at our desire location into the file. For better understanding let’s have a look of syntax and its example.

In above command we have checked this is added a user linuxdady with sudo permission into /etc/sudoers file. Suppose now we want to delete this user entry form /etc/sudoers file then.

Suppose we want to add content starting of the file then use below command.

Now suppose you want to delete line with the help of regular expiration. Then.

Module 11: Replace module

Replace modules in ansible is mainly used to change character or string into the file. Like suppose I have a file demo.sh. From this file I want to change character ‘ansible’ to ‘puppet’. Let’s do it.

replace file content

Module 12: URI module

If you want to understand URI modules in ansible then simple you can compare it with curl command. This can perform all task of curl command. For windows use win_uri module. Syntax and example of uri module.

We can also pass username and password in this module if required. For more visit ansible official docs.

Module 13: GET_URL module

You can compare this module with wget command in Linux means we can perform all operation of wget command with this module like downloading from the internet, passing credential etc. Let’s understand more with example suppose I want to download a package to all web group machine.

The major disadvantage of get_url module is it will not download the file again if file already exist into directory even content differ. If we want to download, we have to use force=yes.

Module 14: Stat module

The state module is mainly used to find the details of file or directory like creation detail, modification detail, owner, permission, group, checksum, inode number etc. This module is an alternate of stat command in Linux. For windows we can use win_stat module.

If your test.sh file will exist in /tmp folder then it will show the complete details of test.sh file otherwise will show the exist status = false. This module have lot of use case for more visit at its official docs.

Module 15: Setup modules in ansible

Setup module is mainly used for fact gathering in ansible means we can get system information by this module hostname, IP address, filesystems, OS releases, Users, Network parameters, CPU, memory, disk space and many more. We can store fact value in a variable and we can call it into playbook if required

This command will display the full details of 172.17.0.2 host. We can filter out on the behalf of facts.

Suppose we want to check IP address in web group then use below filter.

Module 16: User module

User modules in ansible have very important role. Because with the help of this module we can create, delete, modify or we can add group, groups, home directory of user according to our requirement. Simple you can compare it with useradd command in Linux.

Module 17: Group modules in ansible

Group module is used to manage the group in Linux based OS. For better understanding you can compare it with groupadd command in Linux mean we can add, delete, modify group according to our requirement.

Suppose we want to delete group from all web machine.

Module 18: Archive

Archive modules in ansible use to compress our data in Linux based OS. For better understanding  we can compare it with tar command. In below example we are going to archive /etc directory in bz2 comparison under temp directory.

Conclusion – Basic modules in ansible

In ansible there is tons of modules to read all module is not intelligence. Just be aware with basic modules of ansible. If we will become hands on over basic module then easily, we can use rest of module. So, In this tutorial I have tried to explain basic modules in ansible. If anyone have query write me in comment box. I will try my best to resolve.

Interview question related to modules in ansible

  1. How to copy test.sh file form manage node /opt directory to manage node /mnt directory?
  1. Suppose I have a file of 5GB over the internet and I want to copy it on all manage node . Then what will be more preferable copy module or wget module and why?

Ans.  Copy module because wget module will download package on all machine that will consume our bandwidth and download speed will be slow then copy which consume time.

  1. Which one module is to get fact in ansible?

Ans. Setup modules

4. Explain the difference between setup and stat module?

https://linuxdady.com/basic-modules-in-ansible/

https://linuxdady.com/ansible-playbook-example/

https://linuxdady.com/multitask-playbook-in-ansible/

https://linuxdady.com/ansible-ad-hoc-commands-with-example/

////////////////////////////////////////////////////////

Ansible ad hoc commands with example

In this tutorial we are going to learn ansible ad hoc commands and how to push configuration by ad hoc method. Ad hoc method will be helpful for you when you are not well aware about ansible modules. You can execute commands on all required managed host parallelly by this method.

Ansible ad hoc commands

To perform lab of ansible ad hoc commands we are using four managed node and we will create a static inventory file. On the behalf of this static inventory file we will perform our lab.
Control Node IP :– linuxdady.com (172.31.86.202)
Manage Node IP :– 172.17.0.2, 172.17.0.3, 172.17.0.4
Now we are going to create a simple inventory file to perform lab. Kindly have a look of below inventory file and change it accordingly.

[Click & Read:–  How to install ansible step by step very easy]

[Click & Read:–  Static and dynamic inventory file explanation for beginners]

In above static inventory file, we have created web and dev group as well a db group which is group over the group. Still our manage node are not password less. So, first we are going to create username and password for manage node. After doing practice with password then we will work with key based authentication. I have created user “centos” and set password “redhat”. You can change it accordingly. Now first have a look of ad hoc syntax.

Example 1 Check all hosts uptime in [web] group

-k :- Option is required to prompt password for manage node.

ansible ad hoc commands

In above example we have checked uptime of manage node from centos user. Suppose you are login from centos, and you want to check uptime from another user at manage node. Mind it user should be exist of manage node. Then it will work otherwise it will show you the error. So, I am trying with root user. Use any command from below according to your comfort.

-u:- option will use to pass username.
-k:- Option will use to prompt password for root user.

How to disable host level verification from ansible level

In above example you will find every time while making connection with manage node asking for yes or no option. To avoid this, disable host level verification from ansible level. So, open you ansible.cfg file and uncomment the below entry. After that ansible will not prompt yes or no option while making connection with managed host.

ansible host verification

Example 2 Use sudo to perform any task

Suppose you are normal user and you want to perform any root level task. Then you need root level permission otherwise you will got permission error. So, use -b option.

-b :– This option help to become sudo

Make managed node password less

To type password for every machine manually is not a best practice. Because for 2-3 machine this is ok but suppose you have hundred servers, or you want to automate any task then this become problematic task. So, best practice is we should make all manage node password less. For this purpose, we have to create a ssh key and then we have to copy it on all manage node.

ansible ssh key

Our key has been created by above command. Now copy public key to all managed node by using below command. Change IP address and user name accordingly. 

Our key has been successfully added. Now check once it becomes password less or not from your end. Execute below command to check its working without password or not.

We are properly able to check uptime from centos user. Let’s check user id of centos user once.

ansible ad hoc commands linuxdady

How to set -k and -u option as a default

Suppose you are not interest to type -k and -u option while making connection with manage node then we can also make all these option as a default. Open your ansible.cfg file and go to line 15 and make below entry.

Similarly, if you want to perform all action with sudo then again open you file and make below entry under line 340 and uncomment the below line.

Now check our changes are working or not. Note it down we are running command form centos user and we will see who is performing action on manage node. Let’s check the id.

ansible ad hoc command example

As we have check it is showing id of root user or root user means our changing has been reflected and sudo parameter is working file.

Conclusion

In this tutorial we have taken basic overview of ansible ad hoc commands and how to perform action on group. Ad hoc method is very useful when you are lacking in modules and playbook part. You can directly push your changes with the help of ad hoc method. So, write me in comment box if anyone have query. I will try my best to resolve that query.

Interview on ansible ad hoc commands

  1. Suppose python is not installed on client machine which one module will you prefer in raw or command ?

Ans. raw module will be preferable because command module is written in core python. ( if I am wrong you can write me in comment box)

  1. Why ad hoc method?
  2. What is configuration file of ansible?
  3. What does -k option in ad hoc ?
  4. It is possible to make connection without key with manage host.

Ans. Yes, we can establish connection with the help of password. But to type password for every machine for every time become hectic.  

////////////////////////////////////////////////////////


////////////////////////////////////////////////////////


////////////////////////////////////////////////////////


////////////////////////////////////////////////////////


////////////////////////////////////////////////////////


////////////////////////////////////////////////////////


////////////////////////////////////////////////////////


////////////////////////////////////////////////////////


////////////////////////////////////////////////////////


////////////////////////////////////////////////////////

Comments