In this lesson, you’ll learn how Ansible configuration files work, how they control the tool’s default behavior, and the various methods available to override their default settings. Understanding these files is essential for efficiently managing your Ansible environment and tailoring it to different playbook scenarios. When you install Ansible, a default configuration file is created atDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
/etc/ansible/ansible.cfg. This file is divided into several sections that manage various aspects of Ansible’s behavior. Common sections include [defaults], [inventory], [privilege_escalation], [paramiko_connect], [ssh], and [colors]. Each section comes with options set to default values. Below is an example structure of the configuration file:
[defaults] section, you can specify key settings such as the inventory file location, log file, module paths, roles directory, and plugins directory. It also allows configuration of options like fact gathering behavior, SSH connection timeouts, and the number of concurrent host forks. Consider the following example:
Ansible processes configuration files in a specific order of precedence. Settings defined in higher precedence files override those in lower precedence ones.
Customizing Configuration for Different Playbooks
By default, Ansible uses the configuration file at/etc/ansible/ansible.cfg, regardless of the playbook’s location. However, if you have multiple playbooks for different purposes—such as web, database, or network configurations—you might need unique settings for each context.

- For web playbooks, you might disable fact gathering.
- For database playbooks, you may enable fact gathering while disabling colored output.
- For network playbooks, you could extend the SSH timeout from the default 10 seconds to 20 seconds.
/opt/ansible-web.cfg for web playbooks) and instruct Ansible to use it by setting the ANSIBLE_CONFIG environment variable. Before running your playbook, use the following command:
/opt/ansible-web.cfg instead of the default configuration file. The order of precedence for configuration files is as follows:
- The configuration file specified by the
ANSIBLE_CONFIGenvironment variable. - The
ansible.cfgfile in the current directory. - The
.ansible.cfgfile located in the user’s home directory. - The default configuration file at
/etc/ansible/ansible.cfg.
Overriding Specific Parameters with Environment Variables
Sometimes you may only need to adjust a single parameter without copying the entire configuration file. Ansible allows you to override configuration parameters by setting environment variables. For most options, convert the parameter name to uppercase and prefix it withANSIBLE_. For example, to override the gathering parameter (originally set as follows):
-
For one-time execution:
-
For the current shell session:
- To make persistent changes, consider creating a local configuration file in your playbook directory and version-controlling it with your repository.
ansible-config command:
-
List all configuration options with their default values:
-
View the currently active configuration file:
-
Dump the full configuration (with sources):