Sélectionner une langue
With a fresh installation of Ansible, like every other software, it ships with a default configuration file. This is the brain and the heart of Ansible, the file that governs the behavior of all interactions performed by the control node. In Ansible’s case that default configuration file is (ansible.cfg) located in /etc/ansible/ansible.cfg.
Please pay close attention to the line that starts with "# nearly... " This line details the order of precedence of Ansible, which we will talk about down below, I just wanted to point that out.
As you can see, the default Ansible configuration file is very large and divided into ten different sections. Each section denoted within the square brackets gives you an idea about this massive configuration file. I will choose a couple of the default parameters to demonstrate how they affect Ansible’s behavior. I highly recommend that you browse through the default configuration file to see the different settings that are available to you. In most cases, to have basic Ansible configuration file going, you can get away with just having the [defaults] and [privilege_escalation] settings, as you will see below.
Ansible is so flexible, and it chooses its configuration file from one of several possible locations on the control node. One use case where this might be useful would be managing a web server and a database server. You might need to gather facts from one host and not on the other one. Having an ansible.cfg in the current project working directory can facilitate this behavior. If we’re going to be working with multiple configuration files, it is important to understand the order of precedence on how it chooses its configuration file; we’ll go through them below.
By default Ansible reads its configuration file in /etc/ansible/ansible.cfg , however this behavior can be altered. The recommended practice is either to have an ansible.cfg in your current project working directory or to set it as an environment variable. One way to determine which configuration file ansible is using is to use the $ansible --version command, you can also run your ansible commands with the -v option.
When it comes to the order of precedence, the ANSIBLE_CONFIG environment variable has the highest precedence. If this environment variable is in your current shell, it will override all other configuration files. Here is one reason you might want to use the environment variable: let’s say you have multiple projects and you want all of them to use one specific configuration file, besides the default one located in /etc/ansible. Setting the environment variable would be a good way to solve this problem.
The second location on the food chain is to have ansible.cfg in your current working directory. if Ansible doesn’t find a configuration file in the current working directory, it will then look in for an .ansible.cfg file in the user’s home directory, if there isn’t one there either, it will finally grab the /etc/ansible/ansible.cfg.
The ansible-config utility
Use the ansible-config utility to view, list, or dump the various different settings available for Ansible. Running the $ansible-config view utility will print in your standard output your current ansible.cfg content, as you can see, this below outcome is the exact same as the earlier $cat ansible.cfg command above.
Remember how we say earlier if an environment variable is defined, it has the highest precedence? Let’s demonstrate this workflow and use $ansible-config dump to dump all of Ansible current confirmation.
In the above example, we set the ANSIBLE_COLOR environment variable to true, and we demonstrated the use of ansible-config dump to dump out Ansible’s current configuration, and then we ran the $ansible-dump command again. As you can see, no colors are being displayed in standard out. Using environment variables is a quick way to tweak non-persistent settings.
Lastly, we can use $ansible-config list command to see a list of all different parameters, their default value, and the different values you can set.
When it comes to managing Ansible configuration, that’s pretty much all the meats and potatoes. Please reference the official documentation page https://docs.ansible.com/ansible/latest/installation_guide/intro_configuration.html for more detailed information. I hope you’ve found this quick read useful.
Connect with Red Hat Services
Learn more about Red Hat Consulting
Learn more about Red Hat Training
Join the Red Hat Learning Community
Learn more about Red Hat Certification
Subscribe to the Training Newsletter
Follow Red Hat Services on Twitter
Follow Red Hat Open Innovation Labs on Twitter
Like Red Hat Services on Facebook
Watch Red Hat Training videos on YouTube
Understand the value of Red Hat Certified Professionals