Ansible: Automate Everything from First Playbook to Production · Foundations: Installation, Inventory and Ad-hoc Commands
Install Ansible and Configure Your Control Node
Install Ansible via pip, verify the version, understand the control-node / managed-node split, and create an ansible.cfg that points at your inventory.
What you'll build
A working Ansible control node: Ansible installed, version verified, and a minimal ansible.cfg that sets the inventory path and disables host-key checking for lab use.
The idea
Ansible follows a push-based, agentless model. You run commands from a single machine called the control node; it connects to managed nodes over SSH (Linux) or WinRM (Windows) and executes tasks there. Managed nodes need nothing installed except Python 3 — no agent, no daemon, no open port beyond SSH.
The control node needs:
- Python 3.10+ (the
ansiblepackage is pure Python). - SSH client (
ssh) for connecting to managed nodes. - ansible.cfg in the project root (or
~/.ansible.cfg) to set defaults.
The recommended install method is pip install ansible inside a virtual environment. Distribution packages (apt install ansible) work but are often several versions behind.
ansible.cfg cascades: Ansible checks ANSIBLE_CONFIG env var → ./ansible.cfg (project root) → ~/.ansible.cfg → /etc/ansible/ansible.cfg. A per-project ansible.cfg keeps settings portable and versioned.
The code
pip install --quiet ansible
ansible --version | head -6
Output
Run this on your computer — see “Get Started” for setup.
Create a minimal project with an ansible.cfg and a local inventory:
mkdir -p ansible-lab && cd ansible-lab
cat > ansible.cfg << 'EOF'
[defaults]
inventory = inventory.ini
host_key_checking = False
retry_files_enabled = False
stdout_callback = yaml
[ssh_connection]
pipelining = True
EOF
cat > inventory.ini << 'EOF'
[local]
localhost ansible_connection=local
EOF
ansible --version | grep "config file"
ansible all --list-hosts
Output
Run this on your computer — see “Get Started” for setup.
Key settings in ansible.cfg:
| Setting | Purpose |
|---|---|
inventory |
Default inventory file path |
host_key_checking = False |
Skip SSH fingerprint prompts (labs only — enable in production) |
retry_files_enabled = False |
Don't create .retry files after failures |
stdout_callback = yaml |
Human-readable task output instead of JSON |
pipelining = True |
Reduce SSH round trips by running multiple commands per connection |
Common mistakes
- Installing with
sudo pip install. This pollutes the system Python. Use a virtual environment orpipx install ansible. - Leaving
host_key_checking = Falsein production. An attacker who intercepts SSH connections can inject commands. Usessh-keyscanto populateknown_hostsbefore the first run. - Forgetting Python on managed nodes. Ansible modules execute Python on the remote host. If a minimal server image has no Python, the
rawmodule oransible.builtin.packagewithgather_facts: falsecan bootstrap it. - Using an outdated distribution package.
apt install ansibleon Ubuntu 22.04 installs Ansible 2.10; the current version is 2.17+. Features likeansible.builtinFQCNs and improved error messages require recent versions.
Key points
- Ansible is agentless: control node pushes over SSH, managed nodes need only Python 3.
- Install with
pip install ansiblein a virtualenv for the latest version. ansible.cfgin the project root sets defaults; it cascades from env var → project → home → system.- Disable
host_key_checkingonly in lab environments. pipelining = Truemeasurably speeds up playbook runs.
Further reading: Installing Ansible · Ansible configuration
Practice questions
What does the control node need that managed nodes do not?
Answer
The control node needs Ansible installed (the Python package). Managed nodes only need Python 3 and an SSH server — no Ansible package, no agent, no daemon.
Why is pipelining = True recommended?
Answer
With pipelining enabled, Ansible sends the module code and executes it in one SSH session instead of copying a temporary file first. This reduces SSH round trips and speeds up playbook runs, especially on high-latency connections.
In what order does Ansible look for ansible.cfg?
Answer
ANSIBLE_CONFIG environment variable → ./ansible.cfg (current directory) → ~/.ansible.cfg (home directory) → /etc/ansible/ansible.cfg (system). The first one found wins.
Open in the app to mark lessons complete, take notes and track your progress.