Ansible Series

⚙️ Understanding ansible.cfg and Setting Precedence From a "Top 1%" Perspective: How It Relates to Environment Variables and Command-Line Arguments

Introduction

This article is part of the Top 1% Series Complete Article Guide, the 13th article in the Ansible Series.

The Big Picture

graph TB
    A["File specified via the<br/>ANSIBLE_CONFIG env var"]
    B["ansible.cfg in the<br/>current directory"]
    C["~/.ansible.cfg in the<br/>home directory"]
    D["/etc/ansible/ansible.cfg"]
    E["Ansible's built-in defaults"]
    A -->|"not found -> next"| B
    B -->|"not found -> next"| C
    C -->|"not found -> next"| D
    D -->|"not found -> next"| E

A Thorough, Grounds-Up Explanation

ansible.cfg Gets Loaded From “One of Four Places”

When you run Ansible, it searches for ansible.cfg in the order shown above, and loads only the first one it finds. Even if files exist in multiple locations, they never get merged. In practice, placing ansible.cfg in the same directory as your Playbooks (the current directory) is the most common setup — it lets you switch configuration per project, and lets the whole team share it under git.

Settings Commonly Configured in ansible.cfg

[defaults]
inventory = ./inventory/hosts.yml
remote_user = deploy
host_key_checking = False
retry_files_enabled = False

[privilege_escalation]
become = True
become_method = sudo

What a Pro Sees Here (Top 1% Understanding)

“Same Playbook, Different Result” Usually Comes Down to Setting Precedence

Ansible’s settings aren’t determined by ansible.cfg alone. In practice, they get overridden in this order (the lower items win, meaning they take final priority):

  1. Ansible’s built-in defaults
  2. ansible.cfg
  3. Environment variables (e.g., ANSIBLE_HOST_KEY_CHECKING=False)
  4. Command-line arguments (e.g., ansible-playbook -i other-inventory.yml)

When “only my environment behaves differently” shows up on a team, the first thing to suspect is that somewhere in these four stages, an environment variable in someone’s personal shell, or a command-line argument baked into a personal alias, is overriding the team’s shared ansible.cfg. Even if ansible.cfg is shared via git, an export ANSIBLE_* line sitting in someone’s .bashrc still wins. When investigating, the ansible-config dump --only-changed command lists exactly which settings differ from the defaults, and which source overrode each one.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. Ansible behaves differently across teammates: Run ansible-config dump --only-changed and check whether each differing setting is coming from the default, ansible.cfg, or an environment variable.
  2. The intended inventory file isn’t being used: Check for a missing -i argument, or a conflict between ansible.cfg’s inventory setting and a -i argument baked into a shell alias.
  3. Ansible behaves differently only in CI: Check whether the CI environment has an unexpected ANSIBLE_* environment variable set.

Summary

Takeaways to Apply Today

  1. Keep your project’s ansible.cfg in the current directory and under git, so the whole team shares the same settings.
  2. When something feels like “only my environment is different,” suspect your own shell’s environment variables first.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).