Goal
Stop repeating -i inventory.ini and connection flags on every command.
Project layout
On the control node:
mkdir -p ~/ansible-project
cd ~/ansible-projectinventory.ini
[web]
prod-managed-node-1 ansible_host=54.179.68.157 app_name=frontend
prod-managed-node-2 ansible_host=54.254.115.55 app_name=backend
[web:vars]
ansible_user=ubuntuansible.cfg
[defaults]
inventory = inventory.ini
host_key_checking = False
remote_user = ubuntu
private_key_file = ~/.ssh/id_ed25519
forks = 10
timeout = 30
log_path = ansible.log| Setting | Purpose |
|---|---|
inventory | Default inventory path |
host_key_checking | Skip first-connect SSH prompts (labs) |
remote_user | Default SSH user |
private_key_file | Control node private key |
forks | Parallel host connections |
timeout | SSH timeout |
log_path | Write Ansible logs |
Verify config is loaded
ansible --version
# look for: config file = .../ansible-project/ansible.cfg
ansible-inventory --list
ansible all -m pingWith ansible.cfg present:
# Before
ansible all -i inventory.ini -u ubuntu -m ping
# After
ansible all -m pingOverride when needed
CLI flags still win for a single run:
ansible all -i other-inventory.ini -m pingSearch order
Ansible looks for config roughly in this order (first found wins):
ANSIBLE_CONFIGenvironment variable./ansible.cfg(current directory)~/.ansible.cfg/etc/ansible/ansible.cfg
Student tasks
- Create
~/ansible-projectwithinventory.ini+ansible.cfg - Confirm
ansible --versionshows your config file - Ping all hosts without
-i - Change
forksand re-check withansible-config dump - Generate a sample config:
ansible-config init --disabled > sample.cfg
Interview reminders
- What is
ansible.cfg? Project/user/system defaults for inventory, SSH, parallelism, and more. - Which config wins? The first found in the search order (or
ANSIBLE_CONFIG). - Best practice? Keep
ansible.cfgin the project root and commit non-secret settings with the repo.