Lesson 2.2: Managing ansible configuration files


Creating Ansible Configuration file

# Create a file with predefined name ansible.cfg [devops@serverA playbooks]$ pwd /home/devops/playbooks [devops@serverA playbooks]$ vi ansible.cfg # The file path can be relative or absolute path # The configuration file does not need lie in the same dir as inventory file, it could be anywhere. But the standard practice is shown below. [devops@serverA playbooks]$ cat ansible.cfg [defaults] inventory=/home/devops/playbooks/inventory

Querying Inventory

[devops@serverA playbooks]$ ansible devservers --list-hosts hosts (2): 192.168.208.102 192.168.208.103 [devops@serverA playbooks]$ ansible testservers --list-hosts hosts (1): 192.168.208.104 [devops@serverA playbooks]$ ansible testweb --list-hosts hosts (3): 192.168.208.104 192.168.208.102 192.168.208.103 # Listing individual hosts [devops@serverA playbooks]$ ansible 192.168.208.102,192.168.208.103 --list-hosts hosts (2): 192.168.208.102 192.168.208.103 # Listing all [devops@serverA playbooks]$ ansible all --list-hosts hosts (3): 192.168.208.104 192.168.208.102 192.168.208.103 [devops@serverA playbooks]$ ansible *.example.com --list-hosts hosts (1): sanjeeb.example.com # Take action in those who has common host in different groups [devops@serverA playbooks]$ ansible 'testservers:&devservers' --list-hosts hosts (1): sanjeeb.example.com # Present in testservers but not in devservers [devops@serverA playbooks]$ ansible 'testservers:!devservers' --list-hosts hosts (1): 192.168.208.104

Ansible Configuration Files Priority

Highest to Lowest Priority

    1. $ANSIBLE_CONFIG
    1. ./ansible.cfg
    1. ~/.ansible.cfg
    1. /etc/ansible/ansible.cfg

Checking the Ansible Configuration files

[devops@serverA playbooks]$ ansible --version ansible [core 2.14.17] config file = /home/devops/playbooks/ansible.cfg configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.9/site-packages/ansible ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections executable location = /usr/bin/ansible python version = 3.9.18 (main, Sep 7 2023, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] (/usr/bin/python3) jinja version = 3.1.2 libyaml = True
All systems normal

© 2025 2023 Sanjeeb KC. All rights reserved.