ansible.cfg & Configuration
Almost every Ansible behaviour can be changed: the default inventory, forks, the remote user, SSH options, how output looks, where roles are found. Settings come from four places. For each setting, the strongest one that sets it wins:
- the command line, for the settings that have a flag (
-f 20for forks,-ufor the remote user); - an
ANSIBLE_…environment variable (e.g.ANSIBLE_FORKS=20); - one
ansible.cfgfile, the first found of:$ANSIBLE_CONFIG,./ansible.cfg(the current folder),~/.ansible.cfg,/etc/ansible/ansible.cfg; - the built-in default.
Only one config file is read: settings are not merged from several files.
Note: Ansible ignores
./ansible.cfgwhen the current folder is world-writable, because anyone could plant one there. You get a warning instead. That matters in shared folders and in some CI setups that mount the checkout with mode777.
A typical project file
Commit an ansible.cfg at the root of your Ansible repository, so everyone who runs from there gets the same behaviour:
# ansible.cfg
[defaults]
inventory = inventories/staging
roles_path = roles
collections_path = collections
interpreter_python = auto_silent
forks = 20
timeout = 30
stdout_callback = ansible.builtin.default
callback_result_format = yaml
retry_files_enabled = false
[ssh_connection]
pipelining = true
ssh_args = -o ControlMaster=auto -o ControlPersist=60s| Setting | Why |
|---|---|
inventory |
no -i needed; point it at staging so production needs an explicit -i |
roles_path, collections_path |
roles and collections vendored in the repository are found first |
forks |
5 is low for a fleet; raise it while watching the control node's CPU |
callback_result_format = yaml |
task results printed as YAML instead of JSON, easier to read |
pipelining |
sends modules over the existing SSH session instead of copying files: much faster; needs requiretty off in sudoers (the default today) |
ControlPersist |
reuses SSH connections between tasks |
Asking Ansible what it uses
ansible-config answers "why is this setting what it is?":
mkdir proj && cd proj
cat > ansible.cfg <<'INI'
[defaults]
inventory = hosts.ini
forks = 20
interpreter_python = auto_silent
INI
ansible-config dump --only-changed
echo "=== an environment variable beats the file"
ANSIBLE_FORKS=7 ansible-config dump --only-changed | grep FORKSANSIBLE_FORCE_COLOR(env: ANSIBLE_FORCE_COLOR) = False
ANSIBLE_HOME(env: ANSIBLE_HOME) = /home/learner/.ansible
ANSIBLE_NOCOLOR(env: NO_COLOR) = True
CONFIG_FILE() = /home/learner/proj/ansible.cfg
DEFAULT_FORKS(/home/learner/proj/ansible.cfg) = 20
DEFAULT_HOST_LIST(/home/learner/proj/ansible.cfg) = ['/home/learner/proj/hosts.ini']
INTERPRETER_PYTHON(/home/learner/proj/ansible.cfg) = auto_silent
INVENTORY_UNPARSED_WARNING(env: ANSIBLE_INVENTORY_UNPARSED_WARNING) = False
LOCALHOST_WARNING(env: ANSIBLE_LOCALHOST_WARNING) = False
GALAXY_SERVERS:
=== an environment variable beats the file
DEFAULT_FORKS(env: ANSIBLE_FORKS) = 7
Each line shows the setting's internal name, where the value came from and the value. ansible-config list documents every setting with its environment variable and ini key, and ansible-config init --disabled > ansible.cfg writes a fully commented template.
The config file in effect also appears in ansible --version:
mkdir proj && cd proj
printf '[defaults]\nforks = 20\n' > ansible.cfg
ansible --version | grep 'config file'
cd ..
ansible --version | grep 'config file' config file = /home/learner/proj/ansible.cfg
config file = None
The world-writable folder trap
mkdir shared && chmod 777 shared && cd shared
printf '[defaults]\nforks = 20\n' > ansible.cfg
ansible-config dump --only-changed 2>&1 | head -3[WARNING]: Ansible is being run in a world writable directory (/home/learner/shared), ignoring it as an ansible.cfg source. For more information see https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
ANSIBLE_FORCE_COLOR(env: ANSIBLE_FORCE_COLOR) = False
ANSIBLE_HOME(env: ANSIBLE_HOME) = /home/learner/.ansible
The fix is never chmod 777 on a project folder; if your CI must, point ANSIBLE_CONFIG at the file explicitly.
Useful environment variables
| Variable | Use |
|---|---|
ANSIBLE_CONFIG |
use this config file |
ANSIBLE_NOCOLOR=1 |
plain output for logs (this course's sandbox sets it) |
ANSIBLE_STDOUT_CALLBACK |
change the output format for one run, e.g. json for machine-readable output |
ANSIBLE_HOST_KEY_CHECKING=False |
skip SSH host key checks: only for throwaway test VMs |
ANSIBLE_VAULT_PASSWORD_FILE |
where the Vault password comes from |
Test yourself
You have ./ansible.cfg and ~/.ansible.cfg, and ANSIBLE_CONFIG is unset. Which is used?
- Both, merged
./ansible.cfg~/.ansible.cfg/etc/ansible/ansible.cfg
ansible.cfg sets forks = 20 and you export ANSIBLE_FORKS=7. What is used?
- 20
- 7
- 5
- 27
Why would Ansible ignore ./ansible.cfg?
- The folder is world-writable
- The file is not executable
- It lacks a
[defaults]section - It is in Git