In the previous article Getting started with Ansible – Part 1 of the series “Learning Ansible”, we have seen the installation of Ansible and the set up of web and database servers with CentOS and Ubuntu operating systems. We also saw the creation of a user, giving the user sudo privilege, and enabling password authentication.
So, in this article, we will learn the following.
- Inventory
- Groups in Inventory, Group of groups
- Variables in Inventory
- Execute Inventory file for a host with variables different than group variables.
- Escalate privilege for the user.
- Ansible modules like ping, yum, apt and service.
Inventory, Groups, and Group of groups
Inventory is a text file where we define the host information that we are going to manage through Ansible.
The default path of the inventory file is /etc/ansible/hosts/ but we can also specify a different inventory file using -I <path of inventory file> on the command line.
So in continuation of the previous article Getting started with Ansible – Part 1 where we created our project directory “superman”, we are going to create an inventory file.
Step 1
Create an inventory file using the command
- vim inventory

Step 2
Add the below code in the inventory file.
- websrv01 ansible_host=172.31.4.33 ansible_user=devops ansible_ssh_pass=devops123
- websrv02 ansible_host=172.31.15.84 ansible_user=devops ansible_ssh_pass=devops123
- websrv03 ansible_host=172.31.8.231 ansible_user=devops ansible_ssh_pass=devops123
- dbsrv01 ansible_host=172.31.1.205 ansible_user=devops ansible_ssh_pass=devops123
- [WebServersGroup]
- websrv01
- websrv02
- websrv03
- [DBServerGroup]
- dbsrv01
- [DataCenter_Ncali:children]
- WebServersGroup
- DBServerGroup
Now let’s understand code written in the inventory file,
- Here we can see that we have specified all our hosts; i.e. the IP of 3 web servers and 1 DB server and we have also given the username and password for all of them.
- We can see a group of web servers named WebServersGroup. This would be really helpful if we want to configure something on all web servers then we can simply give this WebServersGroup rather than specifying the IP of each web server host machine.
- We can see a group of DB servers, though as of now we have only 1 db server but it is a good practice to keep similar kind of hosts in a group to later manage them effectively.
- We can also see a group called DataCenter_Ncali: children. This is a group of groups for North California region as all our servers are present in this region so we have given the group name as DataCenter_Ncali: children. This would be helpful in case we have different hosts residing in different region.
- So, if we want to run a configuration then we can club them as per region and can run the configuration by giving the appropriate group name which will contain other subgroups.
- DataCenter_Ncali:children is a master group and underneath specifies the other group names.
Step 3
We have to ping each of the hosts defined in the inventory file using the ping module.
To call any module in Ansible we use -m option.
So, the command will be the following.
- ansible -I inventory -m ping websrv01
Output

Few things to note here,
Now, we are getting this error because whenever we do SSH to any host then for the first time it asks whether we want to connect or not and when we say yes, it saves the fingerprint of that host machine to the local host's file. So, this is the same due to which we are getting the error above.
Step 4
Thus, the same thing is happening with ansible as well and it will also get the prompt for yes or no. So, now we have to make ansible to skip this option.
So open default ansible config file at /etc/ansible/ansible.cfg and uncomment host_key_checking=False.
Now let's try again.
- ansible -I inventory -m ping websrv01










Anil GoyalPosted Jul 1, 2020, 1:00 AM
Ubuntu@Linux-Ansible-Earth:~$ ansible -i inventory -m yum -a "name=httpd state=present" websrv01 --becomeUnhandled error: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/ansible/config/manager.py", line 559, in update_config_data value, origin = self.get_config_value_and_origin(config, configfile) File "/usr/lib/python2.7/dist-packages/ansible/config/manager.py", line 503, in get_config_value_and_origin value = ensure_type(value, defs[config].get('type'), origin=origin) File "/usr/lib/python2.7/dist-packages/ansible/config/manager.py", line 122, in ensure_type makedirs_safe(value, 0o700) File "/usr/lib/python2.7/dist-packages/ansible/utils/path.py", line 95, in makedirs_safe raise AnsibleError("Unable to create local directories(%s): %s" % (to_native(rpath), to_native(e))) AnsibleError: Unable to create local directories(/home/ubuntu/.ansible/tmp): [Errno 13] Permission denied: '/home/ubuntu/.ansible/tmp' Traceback (most recent call last): File "/usr/bin/ansible", line 62, in <module> import ansible.constants as C File "/usr/lib/python2.7/dist-packages/ansible/constants.py", line 174, in <module> config = ConfigManager() File "/usr/lib/python2.7/dist-packages/ansible/config/manager.py", line 291, in __init__ self.update_config_data() File "/usr/lib/python2.7/dist-packages/ansible/config/manager.py", line 571, in update_config_data raise AnsibleError("Invalid settings supplied for %s: %s\n" % (config, to_native(e)), orig_exc=e) ansible.errors.AnsibleError: Invalid settings supplied for DEFAULT_LOCAL_TMP: Unable to create local directories(/home/ubuntu/.ansible/tmp): [Errno 13] Permission denied: '/home/ubuntu/.ansible/tmp'
Anil GoyalPosted Jul 1, 2020, 12:59 AM
It would have been good if you would have included some commands issues. I came across some issues while running ansible -i inventory -m service -a "name=httpd state=started enabled=yes" websrv01 --become. This one need more clarity on how to setup the environments given that you are using 4 VM consisting of centos and ubuntu.