C:\ansible\ansible.cfg, while on Linux the default file is commonly found at /etc/ansible/ansible.cfg. This file regulates Ansible’s default behavior by splitting settings into several sections. The primary section is usually at the top (commonly named [defaults]), followed by sections for inventory, privilege escalation, SSH connection, colors, and others. Each section contains various options with corresponding values.
For example, a typical configuration file may appear as follows:
- Inventory location
- Log file location
- Module library and roles path
- Behavior controlling fact gathering
- Connection timeout and the number of hosts to target simultaneously when executing playbooks
[inventory] section, options are provided to enable specific inventory plugins.
For more detailed configuration options and explanations, refer to the Ansible Configuration Documentation.
Overriding Configuration Settings
The values used in the default configuration file apply whenever you run playbooks from any location on your control machine. For instance, if you have multiple playbooks—each for web, database, or network tasks—you might require tailored settings for each. Examples include:- Web playbooks: Disable fact gathering.
- Database playbooks: Enable fact gathering but disable colored output.
- Network playbooks: Extend the SSH timeout to 20 seconds.
ansible.cfg in the current directory; if it is found, that file is used. Otherwise, Ansible defaults to /etc/ansible/ansible.cfg.
What if you want to use a configuration file stored in an alternative location (e.g., /opt/ansible-web.cfg) for multiple playbooks? You can specify the location of this configuration file through the ANSIBLE_CONFIG environment variable before running your playbook. For example:
| Priority | Configuration File Source |
|---|---|
| 1 | The file specified by the ANSIBLE_CONFIG environment variable. |
| 2 | The ansible.cfg file in the current directory. |
| 3 | The ansible.cfg file in the user’s home directory. |
| 4 | The default ansible.cfg file in /etc/ansible/. |
Setting Environment Variables
Ansible supports determining configuration parameters based on environment variables. Generally, you can convert the parameter name to uppercase and prefix it withANSIBLE_. In this case, gathering becomes ANSIBLE_GATHERING.
There are various ways to set this environment variable:
-
Inline with the playbook command:
-
Export for the duration of a shell session:
Avoid modifying environment variables without verifying their impact on other playbooks. Always test configuration changes in a controlled environment.
Exploring Configuration Options
To explore the available configuration options and view their corresponding environment variables, you can utilize the following commands:ansible-config dump command can help you. It displays a comprehensive list of all settings that Ansible has picked up, along with their source:
gathering parameter is set to explicit and shows that it was derived from the environment variable ANSIBLE_GATHERING. This feature is particularly useful for troubleshooting configuration issues.
That’s it for this lesson. You can now apply your knowledge by practicing with Ansible configuration files and enhancing your skills. The configuration file is well documented and self-explanatory, allowing you to identify and modify the necessary options. We will be testing some of these configuration skills during future challenges. Happy configuring!