Ansible Tutorial · Getting Started · Lesson 5 of 20
Ansible Configuration
Control Ansible behaviour with ansible.cfg and environment variables.
Configuration file
Ansible reads its settings from ansible.cfg. It searches in this order (first match wins):
ANSIBLE_CONFIGenvironment variable (a file path).ansible.cfgin the current directory.~/.ansible.cfg(hidden file in home)./etc/ansible/ansible.cfg(global default).
The recommended practice is to keep an ansible.cfg in your project root so settings travel with the code.
Common settings
# ansible.cfg
[defaults]
inventory = ./inventory/hosts.yml
remote_user = deploy
host_key_checking = False
timeout = 30
forks = 20
stdout_callback = yaml
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
Key settings explained
| Setting | Section | Purpose |
|---|---|---|
inventory |
defaults | Default inventory path |
remote_user |
defaults | SSH user for connections |
host_key_checking |
defaults | Skip SSH host key verification (useful in labs) |
forks |
defaults | Number of parallel connections (default 5) |
pipelining |
ssh | Reduces SSH operations; faster execution |
become |
privilege | Use sudo by default |
Environment variables
Every setting can be overridden with an environment variable. The name follows the pattern
ANSIBLE_<SECTION>_<KEY> in uppercase:
export ANSIBLE_HOST_KEY_CHECKING=False
export ANSIBLE_FORKS=30
Inspect configuration
ansible-config dump # show all settings and their sources
ansible-config dump --only-changed # show only non-default settings
ansible-config list # describe every setting
Per-project pattern
For a team project, check ansible.cfg into version control. Ansible reads it from the working directory,
so everyone on the team gets the same behaviour:
my-automation/
├── ansible.cfg
├── inventory/
│ └── hosts.yml
├── playbooks/
│ └── site.yml
└── roles/
Security note: Ansible will not load ansible.cfg from a world-writable directory. Keep file
permissions at 0644 or stricter.
cat > /tmp/ansible.cfg << 'EOF'
[defaults]
inventory = ./inventory/hosts.yml
remote_user = deploy
host_key_checking = False
forks = 20
EOF
ANSIBLE_CONFIG=/tmp/ansible.cfg ansible-config dump --only-changed
Output
DEFAULT_FORKS(/tmp/ansible.cfg) = 20 DEFAULT_HOST_KEY_CHECKING(/tmp/ansible.cfg) = False DEFAULT_REMOTE_USER(/tmp/ansible.cfg) = deploy
Run this on your computer — see “Get Started” for setup.
ansible-config dump | grep -i forks
Output
DEFAULT_FORKS(default) = 5
Run this on your computer — see “Get Started” for setup.
Test yourself
In what order does Ansible search for ansible.cfg?
Global → home → project → env varEnv var → project → home → globalProject → home → global → env varHome → project → env var → global
Show answer
Answer: Env var → project → home → global
ANSIBLE_CONFIG env var is checked first, then ansible.cfg in the current directory, then ~/.ansible.cfg, then /etc/ansible/ansible.cfg.