You run ansible-playbook and the behavior differs from that of a colleague running the same playbook. The cause is almost always the same: an ansible.cfg file loaded without your knowing it. This guide explains the load order of configuration files, the key sections ([defaults], [privilege_escalation], [connection] and [ssh_connection]), the essential settings to know for the RHCE 2026, and the command that always answers the question "which value is active?": ansible-config dump --only-changed.
What you will learn
- The load order of
ansible.cfgfiles (ANSIBLE_CONFIGenv >./ansible.cfg>~/.ansible.cfg>/etc/ansible/ansible.cfg). - The main sections of an
ansible.cfgand their role. - The essential settings for the RHCE:
inventory,host_key_checking,forks,gathering,callback_result_format,become,private_key_file,interpreter_python. - How to generate a clean template (
ansible-config init --disabled). - How to diagnose the active configuration (
ansible-config dump --only-changed).
How to configure Ansible
Let us start with good news: Ansible works without any configuration. Install it, run a command, and it runs with its default values. You can check this in an empty directory, the config file line says so plainly.
ansible [core 2.21.5] config file = NoneConfiguring Ansible is therefore not about making it work, but about no longer repeating the same options on every command. It is a comfort that quickly becomes a necessity.
The problem the file solves
Take a project with its inventory in inventory/hosts.yml. Without configuration, Ansible does not know where to look for it, and tells you so:
$ ansible all -m ansible.builtin.ping[WARNING]: No inventory was parsed, only implicit localhost is available[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'You therefore have to point to the inventory by hand, on every call, with -i inventory/hosts.yml. Add the SSH user with -u and the private key with --private-key, and the command becomes long to type and easy to get wrong.
A two-line ansible.cfg file is enough to remove this repetition.
[defaults]inventory = ./inventory/hosts.ymlThe same command then works as is, and ansible --version confirms that the file is taken into account.
$ ansible --version | head -2ansible [core 2.21.5] config file = /home/bob/projets/mon-projet/ansible.cfg
$ ansible all -m ansible.builtin.pinglocalhost | SUCCESS => { ... }The shape of the file
ansible.cfg follows the INI format, the one used by old Windows configuration files, not YAML. The syntax comes down to two rules: sections are written between square brackets, like [defaults], and each setting takes the form key = value. Comments start with # or ;.
Do not try to write this file from memory or to copy it from a forum. Ansible can generate a complete, commented template, and the "Generate a clean template" section below shows how.
Where to put the file
In the vast majority of cases, at the root of your project, next to your playbooks, and versioned with them. This guarantees that the whole team works with the same settings, and that a playbook behaves the same on your workstation and in continuous integration.
Three other locations exist, and Ansible examines them in a precise order. That is the subject of the next section, and the source of a common bad surprise with this file.
The load order
Ansible looks for its configuration file in this exact order, and stops at the first one found. This is crucial: a skipped file is never merged with another, it is all of one or all of the other.
| Priority | Source | When to use it |
|---|---|---|
| 1 | ANSIBLE_CONFIG=/path/ansible.cfg environment variable | CI/CD pipeline, scripts that want to force a config |
| 2 | ./ansible.cfg in the current directory | Per-project config, the standard case |
| 3 | ~/.ansible.cfg | Personal config valid for all your projects |
| 4 | /etc/ansible/ansible.cfg | System config installed by a distribution package |
The classic trap: you have a personal ~/.ansible.cfg that defines inventory = ~/.ansible/hosts. You cd into a project that has its own ./ansible.cfg. The project file wins and your personal inventory is ignored completely. There is no section-by-section merge. If a value is not in the project file, the Ansible default value applies, not the one from your ~/.ansible.cfg.
Checking the active config: ansible-config dump
When a behavior surprises you, the first command to type is ansible-config dump --only-changed. It displays all the settings changed compared to the Ansible defaults, with the source of each value.
ANSIBLE_PIPELINING(/home/bob/Projets/lab-ansible/ansible.cfg) = TrueCACHE_PLUGIN(/home/bob/Projets/lab-ansible/ansible.cfg) = jsonfileCACHE_PLUGIN_CONNECTION(/home/bob/Projets/lab-ansible/ansible.cfg) = ./.ansible_factsCACHE_PLUGIN_TIMEOUT(/home/bob/Projets/lab-ansible/ansible.cfg) = 7200CALLBACKS_ENABLED(/home/bob/Projets/lab-ansible/ansible.cfg) = ['ansible.posix.profile_tasks', 'ansible.posix.timer']CONFIG_FILE() = /home/bob/Projets/lab-ansible/ansible.cfgDEFAULT_BECOME(/home/bob/Projets/lab-ansible/ansible.cfg) = TrueDEFAULT_BECOME_ASK_PASS(/home/bob/Projets/lab-ansible/ansible.cfg) = FalseDEFAULT_BECOME_METHOD(/home/bob/Projets/lab-ansible/ansible.cfg) = sudoDEFAULT_FORKS(/home/bob/Projets/lab-ansible/ansible.cfg) = 10DEFAULT_GATHERING(/home/bob/Projets/lab-ansible/ansible.cfg) = smartDEFAULT_HOST_LIST(/home/bob/Projets/lab-ansible/ansible.cfg) = ['/home/bob/Projets/lab-ansible/inventory/hosts.yml']DEFAULT_PRIVATE_KEY_FILE(/home/bob/Projets/lab-ansible/ansible.cfg) = /home/bob/Projets/lab-ansible/ssh/id_ed25519DEFAULT_REMOTE_USER(/home/bob/Projets/lab-ansible/ansible.cfg) = ansibleHOST_KEY_CHECKING(/home/bob/Projets/lab-ansible/ansible.cfg) = FalseINTERPRETER_PYTHON(/home/bob/Projets/lab-ansible/ansible.cfg) = /usr/bin/python3.12RETRY_FILES_ENABLED(/home/bob/Projets/lab-ansible/ansible.cfg) = False
GALAXY_SERVERS:This output comes from the lab's ansible.cfg, shown further down in "Essential settings by section". The callback_result_format = yaml line does not appear in it: that option belongs to the ansible.builtin.default callback, not to the engine configuration, and ansible-config dump --only-changed only lists the latter. Callback options are read with ansible-config list -t callback.
Three things to read in this output. CONFIG_FILE() shows the ansible.cfg file that was detected, so you know immediately which one was loaded. The source in parentheses says for each setting whether it comes from the file (path) or from an environment variable (env:). Only the changed settings are listed, so the Ansible defaults do not clutter the output.
For the full dump (useful at least once to explore what exists): ansible-config dump. You get all the settings of the Ansible config, with their effective value and their source.
The sections of an ansible.cfg
An ansible.cfg contains sections in INI format. Here are the sections of the engine configuration, captured with ansible-config init --disabled | grep '^\[':
[defaults][privilege_escalation][persistent_connection][connection][colors][selinux][diff][galaxy][inventory][netconf_connection][jinja2][tags]In practice, three sections are enough to get started: [defaults], [privilege_escalation] and [connection], completed when needed by [ssh_connection]. The others serve specific needs: color customization, NETCONF plugins for network devices, SELinux settings.
A remark on this list, which surprises on first reading: the [ssh_connection] section is not in it, even though you will often write it right after [defaults]. The reason lies in the origin of the settings. The sections listed above come from the engine configuration, whereas [ssh_connection] belongs to the SSH connection plugin, documented separately. It is perfectly valid, and carries most of the SSH settings: on ansible-core 2.21.5, the ssh plugin exposes 21 options there, against only 2 in [connection]. To view them, the command is ansible-config list -t connection.
The essential settings per section
Here is the lab's ansible.cfg (which you find in all the projects of the course), with a line-by-line explanation of the key settings. This is the base to know for the RHCE.
[defaults]inventory = ./inventory/hosts.ymlhost_key_checking = Falseretry_files_enabled = Falseforks = 10callback_result_format = yamlcallbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timergathering = smartfact_caching = jsonfilefact_caching_connection = ./.ansible_factsfact_caching_timeout = 7200remote_user = ansibleprivate_key_file = ./ssh/id_ed25519interpreter_python = /usr/bin/python3.12
[connection]pipelining = True
[privilege_escalation]become = Truebecome_method = sudobecome_ask_pass = FalseSection [defaults], general settings
inventory designates the inventory file or directory to use by default. Without this setting, you have to pass -i hosts.yml on every command. With it, ansible all -m ping finds the hosts directly.
host_key_checking = False disables verification of the server's SSH host key fingerprint. It is convenient on a lab where VMs are reprovisioned often (the fingerprint changes), but dangerous in production where you want to detect a MITM. For labs it is fine; for production, keep it at True and manage ~/.ssh/known_hosts properly.
forks = 10 sets the number of hosts processed in parallel. The Ansible default is 5; on a lab of 4 VMs, 10 is more than enough. On a fleet of 100 servers, raise it to 50 to shorten the run time.
gathering = smart controls fact collection. Three values: implicit (collect on every play, the default), explicit (only if gather_facts: true is explicit), smart (collect if not already cached). The smart mode combined with fact_caching avoids re-collecting facts on every run, which noticeably shortens playbooks that you rerun often.
fact_caching = jsonfile enables caching of the collected facts. Combined with fact_caching_connection (cache path) and fact_caching_timeout (validity period, here 7200 s = 2 h), Ansible avoids re-collecting each host's facts on every run. The gain grows with the number of servers, so measure it on your own fleet with profile_tasks.
callback_result_format = yaml changes how Ansible displays its output: the default callback prints inline JSON that is hard to read, and this option reformats it as structured multi-line YAML. Recommended for any dev workstation. It replaces the old stdout_callback = yaml: the community.general.yaml plugin was removed in community.general 12.0.0, in favor of the result_format option of the ansible.builtin.default callback (since ansible-core 2.13). In the ansible.cfg file, the exact key is callback_result_format and the only accepted values are yaml or json.
callbacks_enabled enables additional callbacks. ansible.posix.profile_tasks adds a duration recap per task at the end of the play (useful to identify what slows things down). ansible.posix.timer displays the total time. See the section on callbacks (in French).
remote_user = ansible is the default SSH user. It saves you from writing it in every playbook or inventory.
private_key_file designates the SSH private key to use. Essential when you have several key pairs and Ansible's is not in ~/.ssh/id_rsa.
interpreter_python = /usr/bin/python3.12 forces the Python interpreter on the managed nodes. Without this setting, Ansible tries to auto-detect it, which can fall back to python3.6 on heterogeneous distributions. Target it explicitly when your managed nodes are uniform.
Section [privilege_escalation], privilege escalation
become = True enables privilege escalation (sudo) by default for all the tasks of the play. You can then disable it locally with become: false on a task that does not need root. Conversely, become = False (the Ansible default) forces you to write become: true everywhere you want sudo.
become_method = sudo defines the escalation tool. Alternatives: su, doas, pbrun, runas (Windows). On RHEL/AlmaLinux/Ubuntu, sudo is universal. The become_user setting designates the target user of the escalation, root by default.
become_ask_pass = False suits the case where the user is configured with NOPASSWD: ALL on the managed node (the lab's case). If your targets require a sudo password, set it to True and Ansible will ask for it at launch.
Section [connection], settings common to connections
pipelining = True enables SSH pipelining: Ansible sends the Python module directly to SSH's standard input instead of first copying it to a temporary file on the server. This removes several network round trips per task. The gain depends on the number of tasks and on latency, so measure it with profile_tasks. Pipelining is disabled by default because it requires requiretty to be absent from the sudo configuration of the managed nodes, which is the default case on AlmaLinux 10.
The same option can be read from several sections. For pipelining, the [defaults], [connection] and [ssh_connection] sections are accepted by the SSH plugin (verified on ansible-core 2.21.5). The global ANSIBLE_PIPELINING setting, the one shown by ansible-config dump --only-changed, only reads [defaults] and [connection]. This guide uses [connection], because it is the section where ansible-config init --disabled places pipelining: a single form to remember, which applies to all connection plugins.
Section [ssh_connection], transport settings
This section controls the SSH connection plugin. You will typically put this in it:
[ssh_connection]ssh_args = -o ControlMaster=auto -o ControlPersist=60scontrol_path = ~/.ssh/cm-%%r@%%h:%%pssh_args imposes additional SSH options (useful for proxy jump, host aliases, multiplexing). control_path defines the location of the SSH multiplexing socket. On most labs, the defaults are fine, and you only have to enable pipelining in [connection].
Generate a clean template
To start a new Ansible project with an up-to-date, commented ansible.cfg, run:
ansible-config init --disabled > ansible.cfgThe command generates a file with all possible settings, each one commented out (prefixed with ;) and documented:
[defaults]# (boolean) This option forces color mode even when running without a TTY or the "nocolor" setting is True.;force_color=False
# (path) The default root path for Ansible config files on the controller.;home=~/.ansibleYou then uncomment only the settings you need; the vast majority stay at the Ansible defaults. This is the clean way to start a project, rather than copy-pasting an ansible.cfg found on StackOverflow.
Equivalent environment variables
Each ansible.cfg setting has its equivalent environment variable, prefixed with ANSIBLE_. Handy for one-off overrides in CI or on the command line.
| cfg setting | Env variable |
|---|---|
inventory | ANSIBLE_INVENTORY |
host_key_checking | ANSIBLE_HOST_KEY_CHECKING |
forks | ANSIBLE_FORKS |
callback_result_format | ANSIBLE_CALLBACK_RESULT_FORMAT |
private_key_file | ANSIBLE_PRIVATE_KEY_FILE |
become | ANSIBLE_BECOME |
gathering | ANSIBLE_GATHERING |
Example: ANSIBLE_FORKS=50 ansible-playbook site.yml temporarily raises the parallelism without touching the file, just like the --forks 50 command-line option. The precedence is: command line > env variables > ansible.cfg. This makes env variables ideal for CI/CD pipelines.
Frequent pitfalls
Three surprises come up regularly with this file. The first two make a configuration that is actually present not get applied, which is confusing until you know the rule.
- Only one
ansible.cfgis loaded, never two. A file placed in./roles/nginx/ansible.cfgis ignored when you runansible-playbookfrom the project root. Per-role or per-subdirectory configuration does not exist in Ansible. - A world-writable directory makes the file be ignored. What counts is the permissions of the current directory, not those of the file: an
ansible.cfgin0666is loaded without complaint, while the same file in a0777directory is discarded. Ansible says so explicitly,Ansible is being run in a world writable directory, ignoring it as an ansible.cfg source, andansible --versionthen showsconfig file = None. The protection targets the injection of a malicious configuration by another user of the machine. - Booleans accept several spellings.
True,true,yesand1all mean true,False,false,noand0all mean false: verified on ansible-core 2.21.5, wherehost_key_checking = noandhost_key_checking = falseproduce the sameHOST_KEY_CHECKING = False. Pick one form and stick to it, consistency makes review easier. Do not confuse this with the YAML of your playbooks, where the convention is lowercasetrueandfalse.
Key takeaways
- Ansible looks for
ansible.cfgin this order:ANSIBLE_CONFIG>./ansible.cfg>~/.ansible.cfg>/etc/ansible/ansible.cfg. The first one found wins, no merge. ansible-config dump --only-changedis your diagnostic reflex, it shows which setting is active and where it comes from.- Three sections are enough to get started:
[defaults],[privilege_escalation]and[connection](wherepipeliningis set), with[ssh_connection]for the SSH options. - The essential settings for the RHCE:
inventory,host_key_checking,forks,gathering,callback_result_format,become,private_key_file,interpreter_python. - To start a new project:
ansible-config init --disabled > ansible.cfg. Uncomment only the necessary settings. - Each setting has its equivalent env variable
ANSIBLE_*, which takes priority over the file, useful in CI/CD.
Put it into practice
Reading the load order is not enough to make it stick, you have to see one file win over another. This lab has you write a project ansible.cfg, confirm the values that are actually active with ansible-config dump, then override them with an ANSIBLE_* environment variable to watch the precedence live. You finish by enabling an output callback, which changes the display without touching the playbook.