Note: This post presumes that you have completed the steps in the previous posts, starting with Ansible Lab – Getting Started
Sample Files
Did you know that when you installed Ansible it provided inventory and configuration file templates for you? Let’s take a look. On the Ansible server navigate to /etc/ansible and pull up a directory listing
cd /etc/ansible ls -lah
Item | Explanation |
---|---|
/etc/ansible/ansible.cfg | A template configuration file. By default all content is commented out |
/etc/ansible/hosts | A template inventory file. By default all content is commented out |
/etc/ansible/roles | An empty folder for storing created roles in |
I won’t go through the contents of the ansible.config or hosts files as they are nicely documented inline ( editor: and online by Ansible as well ). However let’s create a simple configuration file to get us going. I am going to rename the ansible.config file and create my own version of it.
mv /etc/ansible/ansible.config /etc/ansible/ansible.config.original touch /etc/ansible/ansible.config
I have edited my new ansible.config file (using nano) and inserted the following.
As you can see I have added an inventory entry in, pointing to the inventory file we created earlier. Now I won’t have to specify the -i <inventory_file> each time I use the ansible cli. I have specified the root user as the user to use for sudo operations, for visibility, as well as setting a timeout value for SSH connections.
I can test my new configuration file with the following command
As you can see I run the first command specifying the inventory file location and this worked fine. I then re-run the command again failing to specify the inventory file. Again the command worked. Ansible successfully read the inventory file path from the ansible.config file which we modified.
Note: Even though we have the inventory file specified in the ansible.config file, we can override this on the command line. Why? We may have another inventory file we want to use for testing