The Ansible configuration file defines global properties that are specific to our setup. The following are ways in which you can specify the Ansible configuration file, and the first method overrides the next – the settings are not merged, so keep that in mind:
• By setting an environment variable, ANSIBLE_CONFIG, pointing to the Ansible configuration file
• By creating an ansible.cfg file in the current directory
• By creating an ansible.cfg file in the home directory of the current user
• By creating an ansible.cfg file in the /etc/ansible directory
Tip
If you manage multiple applications, with each application in its Git repositories, having a local ansible.cfg file in every repository will help keep the applications decentralized. It will also enable GitOps and make Git the single source of truth.
So, if you check the ansible.cfg file in the current directory, you will see the following:
[defaults]
inventory = ./hosts
host_key_checking = False
Now, to check whether our inventory file is correct, let’s list our inventory by using the following command:
$ ansible-inventory –list -y
all:
children:
dbservers:
hosts:
db:
ansible_host: db
ansible_python_interpreter: /usr/bin/python3
ungrouped: {}
webservers:
hosts:
web:
ansible_host: web
ansible_python_interpreter: /usr/bin/python3
We see that there are two groups—dbservers containing db and webservers containing web, each using python3 as the ansible_python_interpreter.
If we want to see all the hosts, we can use the following command:
$ ansible –list-hosts all
hosts (2):
web
db
If we want to list all hosts that have the webservers role, we can use the following command:
$ ansible –list-hosts webservers
hosts (1):
web
Now, let’s check whether Ansible can connect to these servers by using the following command:
$ ansible all -m ping
web | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
db | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
And, as we can observe, we get a successful response for both servers. So, we’re all set up and can start defining the configuration. Ansible offers tasks and modules to provide CM. Let’s look at these in the next section.
Leave a Reply