Ansible Inventory|Create Inventory File for Normal User

Hello, everyone In this Article we are going to learn How to make Inventory File the Normal User in ansible in a very simple and easy step-by-step approach.

Yellow Fishing Boat Blog Banner 48

Ansible Inventory

The need of Making the Inventory file as a normal users are as follow

For example we have 3 servers

1 – Web Server – These are 100 servers

2 – Database server – These are 150 servers

3 – App server – These are 300 server

Now we have 3 users as follow

User -1 – Harry – The Harry has the responsibility to manage the Web server daily they need to check the uptime of the server / Do some updates daily

User-2 – Natasha- The Natasha has the responsibility to manage the Database daily they need to check the uptime of the server / Do some updates daily

User-3 – Sarah – The Sarah has the responsibility to manage the App server daily they need to check the uptime of the server / Do some updates daily

Now all 3 users have difficulty to manage the servers individually soo they decide to do ansible automation in order to do their tasks on the servers

Soo they go to their manager and tell them that they need to do automation for their task soothe users need the Ansible server and the manager approves it

Now when the users will do the task and they both are working with the root users then it is impossible to find what user is doing what task as every log is generated by the root user

Now we need to make the inventory files for different users so that we get to know what the user is doing which task.

Now let us make the 3 users on the server and assign the password

  • Adding the user harry and assigning the password for the harry user
Screenshot 1 19
  • Adding the user natasha and assigning the password for the natasha user
Screenshot 2 18
  • Adding the user sarah and assigning the password for the sarah user
Screenshot 3 17

Case -1 – For harry User

Now login as harry user and make the custom inventory file for the harry user soo that he can add the entry for their manage node here

Screenshot 6 17
Screenshot 5 18

Now login as harry user and when we execute the command we can see that the host entry is successfully done for the harry user.

$ ansible Web-Server --list-hosts -i custom_inventory_harry/harry_inventory_file
Screenshot 7 15

Now we don’t want to give the path again and again in this command

Soo to do this we need to modify the /etc/ansible/ansible.cfg file for the harry user and we have to define the path for the custom inventory file for the harry user

But by default normal user doesn’t have the right to edit this file we need to give right for the normal user to edit this configuration file

When we see the permission on this file we can see that the normal user cannot able to edit the file they can only read the file

Screenshot 8 15

We have defined the ACL on this file for the harry user now he is able to edit the file

Screenshot 9 15

Now the harry user can edit the /etc/ansible/ansible.cfg file and can add the path for the custom Inventory file here file is

Screenshot 10 14

Now when we execute the # ansible Web-Server –list-hosts command without writing the path for the inventory file then it works

Screenshot 11 16

Case -2- For natasha user

Now login as natasha user and make the custom inventory file for the natasha user soo that she can add the entry for their manage node here

Screenshot 12 15
Screenshot 13 13

Now login as a Natasha user and when we execute the command we can see that the host entry is successfully done for the Natasha user.

$ ansible App-Server --list-hosts -i custom_inventory_natasha/natasha_inventory_file
Screenshot 14 10

Now we don’t want to give the path again and again in this command

Soo to do this we need to modify the /etc/ansible/ansible.cfg file for the Natasha user and we have to define the path for the custom inventory file for the Natasha user

But by default normal user don’t have right to edit this file we need to give right for the normal user to edit this configuration file

When we see the permission on this file we can see that the normal user cannot able to edit the file they can only read the file

We have defined the ACL on this file for the natasha user now she is able to edit the file

Screenshot 15 10

Now the Natasha user can edit the /etc/ansible/ansible.cfg file and can add the path for the custom Inventory file here file is

Screenshot 16 8

Now when we execute the # ansible App-Server –list-hosts command without writing the path for the inventory file then it works

Now again when the user harry wants to do the work he needs to modify the default configuration file with its own custom location because currently the file is modified according to Natasha. This becomes a problem. To solve this problem we need to do the following steps

We have to remove the permissions from the user harry and natasha from the main configuration file now the user harry and natasha has default permission on the configuratiopn file

Screenshot 17 9

Now again we are defining the values of /etc/ansible/ansible.cfg file to default value

Screenshot 18 7

Now we are logged in as harry user and we can copy the /etc/ansible/ansible.cfg file to the custom_inventory directory

Screenshot 19 7

Now we can see that the ansible.cfg file is copied inside the custom_inventory directory

Now harry user has to set the environment variable in order to read the ansible.cfg file that is copied to the harry custom_inventory directory .

$ export ANSIBLE_CONFIG=/home/harry/custom_inventory_harry/ansible.cfg
Screenshot 20 6

Now when we run # ansible –version command then this will reading the customized inventory file

Screenshot 21 5

Now we can edit the ansible.cfg file what we copied above to our customized location where we have put the inventory file

Screenshot 23 3
Screenshot 24 4

Now the inventory file for the harry user will read from the customized location that is defined above

Now when we run # ansible WebServer –list-hosts command this will read the file from above location

Screenshot 25 4

If harry will do logged out then logging again then that variable that we defined is temporary after logged out this will remove to make the permanent change we have to add the variable value in the .bashrc file. There is a hidden file in the user home directory called .bashrc

Screenshot 27 2
Screenshot 26 4

Similarly we can do same steps for the Natasha user

Please note that when we define the location from the environment variable and in the home location and in the main configuration file then ansible always give priority to the location that is defined in the variable. If we unset the environment variable then it will read the home location file

Conclusion

This is the complete knowledge of how we create inventory files for the Normal user in ansible 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 DevOps Articles

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.

Leave a Comment