Ansible configuration files how to use and troubleshoot

Ansible can use multiple files and environment variables to configure its behavior, in this article we will see how we can use them and how…

Ansible configuration files how to use and troubleshoot
Photo by insung yoon on Unsplash

Ansible can use multiple files and environment variables to configure its behavior, in this article we will see how we can use them and how we can troubleshoot configuration issues.

Configuration files priority order

  1. Environment variables: ANSIBLE_CONFIG environment variable has always the greatest priority, this means that if we set
export ANSIBLE_CONFIG=/path/to/your/ansible.cfg

Then ansible configuration will be read from /path/to/your/ansible.cfg

2. Local ansible.cfg: Second in priority if ANSIBLE_CONFIG is not set is local directory ansible.cfg, if you run ansible from a directory that contains ansible.cfg this config file will be used.

3. Home directory .ansible.cfg : if nor environment ANSIBLE_CONFIG nor local ansible.cfg exists then ansible will search in your current user home directory for .ansible.cfg

4. /etc/ansible/ansible.cfg : Lastly this is the if nor environment variable, nor local directory configuration file exists and nor current user configuration file this file will be used.

Configuration Parameters priority

One cool thing with the configuration files is that they dont need to contain all the parameters but only those you want to overwrite from the previous files or the default values.

Configuration Parameters Override

You can override priority by using environment variables, for example if you want to set the “gathering” option to “explicit” you can create an environment variable ANSIBLE_GATHERING=explicit

List configuration and troubleshoot

To view the effective configuration in a friendly format you can use the following command, the output is not the default options but a tailored to my needs config

ansible-config view 
 
[defaults] 
# Control SSH settings 
transport = ssh 
ssh_args = -o ControlMaster=auto -o ControlPersist=3600s -o Compression=yes -o ConnectTimeout=120 
timeout = 120 
pipelining = True 
 
# Limit facts gathering 
gathering = smart 
fact_caching = jsonfile 
fact_caching_connection = ~/.ansible/facts_cache 
 
# Increase task concurrency 
forks = 20

Also especially useful for troubleshooting configuration is the following command, it does not print the config in a friendly format but it lists all the configured options plus default ansible values

ansible-config dump

For example to find out which effective configuration file is used you can do the following

ansible-config dump | grep -i config 
CONFIG_FILE() = /Users/kpatronas/.ansible.cfg

Now we know which configuration file is used we can list all the configured parameters from this file

ansible-config dump | grep -i "/Users/kpatronas/.ansible.cfg" 
ANSIBLE_PIPELINING(/Users/kpatronas/.ansible.cfg) = True 
CACHE_PLUGIN(/Users/kpatronas/.ansible.cfg) = jsonfile 
CACHE_PLUGIN_CONNECTION(/Users/kpatronas/.ansible.cfg) = ~/.ansible/facts_cache 
CONFIG_FILE() = /Users/kpatronas/.ansible.cfg 
DEFAULT_FORKS(/Users/kpatronas/.ansible.cfg) = 20 
DEFAULT_GATHERING(/Users/kpatronas/.ansible.cfg) = smart 
DEFAULT_TIMEOUT(/Users/kpatronas/.ansible.cfg) = 120 
DEFAULT_TRANSPORT(/Users/kpatronas/.ansible.cfg) = ssh

Conclusion

Ansible is a powerful tool with many many options, knowing how those options take place it can help us to utilize its full potential and make it easier to troubleshoot! i hope you enjoyed this article!