Ansible Lab Setup – Mastering Sample Files, Configuration Files, and Multiple Test Nodes (Part 3)

Ansible Lab: Customizing the Ansible Configuration File

In our previous Ansible lab posts, we covered the basics of installing Ansible and creating inventory files for our machines. Today, we’ll dive deeper into customizing the Ansible configuration file to make our automation tasks more efficient.

By default, when you install Ansible, it provides inventory and configuration file templates for you. Let’s take a look at the contents of the ansible.config file located in /etc/ansible on the Ansible server. We won’t go through the contents of the ansible.config or hosts files as they are well-documented inline and online by Ansible. Instead, we’ll create a simple configuration file to get us started.

Renaming the Ansible Configuration File

We can rename the ansible.config file to create our own version of it. For this example, let’s call our new configuration file ansible-custom.config. Using nano, we’ll edit the newly created file and insert the following:

Inventory Entry

—————-

As you can see, we’ve added an inventory entry pointing to the inventory file we created earlier. This way, we won’t have to specify the -i each time we use the Ansible CLI.

Adding Additional Configuration Options

We’ve also specified the root user as the user to use for sudo operations, which provides better visibility and sets a timeout value for SSH connections. Here’s the edited ansible-custom.config file:

Testing the Ansible Configuration File

To test our new configuration file, we can run the following command:

“`css

ansible-playbook -i /path/to/inventory/file

“`

As you can see, we specified the inventory file location, and the command worked as expected. We then re-ran the command without specifying the inventory file, and Ansible successfully read the inventory file path from the ansible-custom.config file we modified.

Overriding the Inventory File on the Command Line

Why do we need to specify the inventory file path in the configuration file if we can override it on the command line? The answer is simple: flexibility. We may have another inventory file we want to use for testing or specific tasks, and by specifying it on the command line, we can quickly switch between different inventory files without modifying the configuration file each time.

Conclusion

———-

In this post, we’ve explored how to customize the Ansible configuration file to make our automation tasks more efficient. By creating a simple configuration file and specifying an inventory entry, we can avoid having to specify the -i each time we use the Ansible CLI. Additionally, we learned how to override the inventory file on the command line, providing us with flexibility in our automation tasks.

As always, thanks for reading, and we’ll see you in the next post!

Leave a Reply