Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Ansible configuration from command line

Tags:

ansible

Ansible has a pretty long list of configuration parameters including different settings for ssh. Assuming I have some /etc/ansible/ansible.cfg and also other ansible.cfg files in current folder, how do I see the resulting configuration?

I am speaking about something like:

ansible show-config
ansible-playbook show config
like image 782
Ross Ivantsiv Avatar asked Dec 20 '22 00:12

Ross Ivantsiv


2 Answers

Run 'ansible --version'.

$ ansible --version
ansible 2.0.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides

$ touch ~/.ansible.cfg
ansible 2.0.1.0
config file = /home/jdoe/.ansible.cfg
configured module search path = Default w/o overrides
like image 52
user6168328 Avatar answered Jan 09 '23 12:01

user6168328


In Ansible 2+ you can easily find this out by running ansible --version.

In Ansible 1.X you had to just follow the precedence in the search path for the config iteratively to find out which was being used. The Ansible docs state that Ansible pulls config in the following order of preference:

  1. ANSIBLE_CONFIG (an environment variable)
  2. ansible.cfg (in the current directory)
  3. .ansible.cfg (in the home directory)
  4. /etc/ansible/ansible.cfg

And prior 1.5 the order was:

  1. ansible.cfg (in the current directory)
  2. ANSIBLE_CONFIG (an environment variable)
  3. .ansible.cfg (in the home directory)
  4. /etc/ansible/ansible.cfg

It also states that it doesn't merge these at all so you don't have to consider what happens if you have an option in /etc/ansible/ansible.cfg but not set in any of the preferential files.

I don't know of a way of asking Ansible directly to show that config it will use if you run Ansible from that directory but you can see it by using this simple bash script:

#!/bin/bash

if ! [[ -z $ANSIBLE_CONFIG ]]; then
  echo $ANSIBLE_CONFIG
  exit 0
elif [[ -f ansible.cfg ]]; then
  cat ansible.cfg
  exit 0
elif [[ -f ~/.ansible.cfg ]]; then
  cat ~/.ansible.cfg
  exit 0
elif [[ -f /etc/ansible/ansible.cfg ]]; then
  cat /etc/ansible/ansible.cfg
  exit 0
else
  echo "No Ansible config found! Using default settings"
  exit 1
fi

I put this in /usr/local/bin/ (making it executable) and named it get_ansible_cfg.sh.

To show it in action we need to set up all of the 4 places Ansible gets config from and then remove them one by one. The values I set up as a test can be seen here:

vagrant@vagrant-ubuntu-trusty-64:~$ echo $ANSIBLE_CONFIG
Set in env variables

vagrant@vagrant-ubuntu-trusty-64:~$ sudo find / -name "*ansible.cfg"
/etc/ansible/ansible.cfg
/home/vagrant/.ansible.cfg
/home/vagrant/inventory/ansible.cfg

vagrant@vagrant-ubuntu-trusty-64:~$ cat /etc/ansible/ansible.cfg
Config in /etc/ansible/ansible.cfg
vagrant@vagrant-ubuntu-trusty-64:~/inventory$ cat ~/.ansible.cfg
Set in ~/.ansible.cfg
vagrant@vagrant-ubuntu-trusty-64:~/inventory$ pwd
/home/vagrant/inventory
vagrant@vagrant-ubuntu-trusty-64:~/inventory$ cat ansible.cfg
Set in local directory ansible.cfg

With all of these set and run we get:

vagrant@vagrant-ubuntu-trusty-64:~/inventory$ get_ansible_cfg.sh
Set in env variables

Which returns our ANSIBLE_CONFIG test value.

Unsetting this and running again gives us:

vagrant@vagrant-ubuntu-trusty-64:~/inventory$ unset ANSIBLE_CONFIG
vagrant@vagrant-ubuntu-trusty-64:~/inventory$ get_ansible_cfg.sh
Set in local directory ansible.cfg

Which is the ansible.cfg in the directory that the shell is currently at .

Removing this file then gives us:

vagrant@vagrant-ubuntu-trusty-64:~/inventory$ rm ansible.cfg
vagrant@vagrant-ubuntu-trusty-64:~/inventory$ get_ansible_cfg.sh
Set in ~/.ansible.cfg

Which is the test value set in the .ansible.cfg file in the current user's home directory.

Finally, removing this gives us:

vagrant@vagrant-ubuntu-trusty-64:~/inventory$ get_ansible_cfg.sh
Config in /etc/ansible/ansible.cfg

And if there's no config in any of these locations we get:

vagrant@vagrant-ubuntu-trusty-64:~/inventory$ sudo rm /etc/ansible/ansible.cfg
vagrant@vagrant-ubuntu-trusty-64:~/inventory$ get_ansible_cfg.sh
No Ansible config found! Using default settings
like image 28
ydaetskcoR Avatar answered Jan 09 '23 11:01

ydaetskcoR