This article explains you all about managing ansbile configuration files. Before playing with Ansible automation, We should have knowledge about how to manage ansible configuration files where it is located, what is the purpose of it.
In the previous posts, we have explained the below topics. Refer those links to understand this topic from basics.
1. What is Ansible, How Ansible works, Ansible Introduction, Ansible Basic Tutorials
2. Ansible Inventory Introduction - Ansible Beginner Tutorials
3. Ansible Ad hoc Commands - Ansible Tutorial for Beginners
4. Managing Ansible Configuration Files Explained with Examples
5. Understanding Ansible Playbook - Write your First Playbook
6. Ansible Roles Explained with Examples - Ansible Tutorials
7. How to use Ansible Vault to Protect Ansible Playbooks
Also Watch this "ANSIBLE CONFIGURATION MANAGEMENT" Tutorial video free on our YouTube Channel.
Default Ansible Configuration file is /etc/ansible/ansible.cfg
[root@node1 ~]# vi /etc/ansible/ansible.cfg
# config file for ansible -- https://ansible.com/
# ===============================================
# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first
[defaults]
# some basic default values...
#inventory = /etc/ansible/hosts
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#forks = 5
#poll_interval = 15
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = False
.
.
.
#..... more......
This ansible configuration file has many parameters like inventory file location, library location, modules and etc,.
All these default values are commented. If you wanted to change the default behavior of any parameter, just uncomment and change the value.
Before making any changes in the config file, Run a ansible ad-hoc command to get the uptime of the clients.
[root@node1 ~]# ansible all -m shell -a uptime
node2 | SUCCESS | rc=0 >>
21:21:25 up 24 min, 2 users, load average: 0.00, 0.01, 0.05
We have got the uptime output of hosts only for node2 which was mentioned in the default inventory file ie /etc/ansible/hosts.
Lets do a change in ansible.cfg file to find the inventory file in other location rather than the default inventory file /etc/ansible/hosts.
[root@node1 ~]# cat /root/myhosts
node1
node2
Where we have specified two clients node1 and node2..
Edit the ansible.cfg file.
#inventory = /etc/ansible/hosts
Uncomment the inventory paratmer as below and save the file.
inventory = /root/myhosts
Rerun the ansible-ad hoc command again.
[root@node1 ~]# ansible all -m shell -a uptime
node1 | SUCCESS | rc=0 >>
00:48:37 up 3 min, 2 users, load average: 0.10, 0.13, 0.06
node2 | SUCCESS | rc=0 >>
00:48:34 up 3:24, 1 user, load average: 0.00, 0.01, 0.05
This time We have got the uptime for two nodes (node1 and node2) which means ansible has used the inventory file /root/myhosts.
Default configuration of ansible is more sufficient for us. But On some cases, we may needed to change the configuration file to suit for our need.
And also you have different options to use these parameter as an argument to override these config file values while running the ansible command directly.
Lets bypass the argument to use the different inventory file /root/databasehosts rather than changing the configuration file.
[root@node1 ~]# cat /root/databasehosts
node1
node2
node3
[root@node1 ~]# ansible all -i /root/databasehosts -m shell -a uptime
node1 | SUCCESS | rc=0 >>
00:48:37 up 3 min, 2 users, load average: 0.10, 0.13, 0.06
node2 | SUCCESS | rc=0 >>
00:48:34 up 3:24, 1 user, load average: 0.00, 0.01, 0.05
node3 | SUCCESS | rc=0 >>
00:48:39 up 3 min, 1 users, load average: 0.12, 0.23, 0.04
The above output shows uptime of three hosts mentioned in the new inventory hosts file /root/databasehosts using "-i" option to specify the inventory hosts to not use the ansible.cfg configuration file. similarly you can use other options to pypass the argument of any parameters.
Multiple Ansible configuration files
Ansible has the flexibility to have multiple configuration file. So changing the single configuration file for every time is not required depend on the environment.Ansible will find the configuration file as below whichever it finds first.
1. Environment variable ANSIBLE_CONFIG set or not, (example: ANSIBLE_CONFIG=/root/ansible.cfg)
2. Find ansible.cfg in the current working directory if Environment variable ANSIBLE_CONFIG not set. Hence you can create dedicated directory for different environments.
3. Find ansible.cfg in the home directory if Environment variable ANSIBLE_CONFIG not set and no ansible.cfg file in the current working directory.
4. Find /etc/ansible/ansible.cfg.
Why do we use multiple Ansible configuration file?
For example, we may have database servers, application servers, web servers separately. Each will have different users, different host inventory file.So when you perform some automation using ansible only for database hosted servers, you may change the ansible.cfg accordingly. Again if you want to do some automation on application server, again you would change the same config file.
This is quite challenging and confusing one. Hence we have the ansible configuration flexibility.
How do we find which ansible configuration file has been used?
Use -v option along with ansible command.[root@node1 ansible]# ansible all -m shell -a uptime -v
Using /etc/ansible/ansible.cfg as config file
node1 | SUCCESS | rc=0 >>
00:48:37 up 3 min, 2 users, load average: 0.10, 0.13, 0.06
That's all about the basics of Ansible Configuration Management. Going forward, we will play more with ansible tool with some test cases.
If you are interested in learning, Request you to go through the below recommended tutorial.
DevOps Full Course Tutorial for Beginners - DevOps Free Training Online
Docker Full Course Tutorial for Beginners - Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners - Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners - Ansible Free Training Online
Openstack Full Course Tutorial for Beginners - Openstack Free Training Online
Keep practicing and have fun. Leave your comments if any.
Stay connected with us on social networking sites, Thank you.
0 Comments