Dec 26, 2024 · Linux & Servers / Intermediate · ~3 MIN READ
Use Ansible to Configure Multiple Linux Servers
Move from manually configured servers to repeatable infrastructure using inventories, playbooks, and roles.
Who This Is For
Intermediate.
Think of it like a recipe card instead of cooking from memory every time. Write the steps once, and every kitchen that follows the card turns out the same dish.
What You’ll Build
One playbook applies the same baseline configuration to two Linux servers.
Prerequisites
- A control node (your workstation or a dedicated host)
- At least two managed Linux hosts with SSH access
Why Configuration Drift Happens
Manual setup works fine for one server. By the third or fourth, small inconsistencies creep in silently, Ansible exists to stop that.
Core Concepts
- Control node, where you run Ansible from
- Inventory, the list of managed hosts, grouped
- Playbooks, the automation itself
- Roles, reusable, organized playbook building blocks
Example Inventory
$ all:
$ children:
$ docker_hosts:
$ hosts:
$ docker01:
$ docker02:
$ monitoring:
$ hosts:
$ monitor01:
Set Up SSH Keys
$ ssh-copy-id user@docker01
Write Your First Playbook
$ - hosts: docker_hosts
$ become: true
$ tasks:
$ - name: Update apt cache
$ apt: update_cache=yes
$ - name: Install Docker
$ apt: name=docker.io state=present
Idempotence
A well-written playbook can run repeatedly with no unintended effect, re-running it should only change what actually needs changing.
Secrets: Use Ansible Vault
$ ansible-vault encrypt secrets.yml
Never commit plaintext passwords or keys to a repository, even a private one.
Roles for Reuse
- Base Linux baseline
- Docker host role
- Monitoring agent role
- Backup client role
Test on One Host First
Always run a new or changed playbook against a single test host with --limit before applying it fleet-wide.
Security & Backup Notes
- Store playbooks and roles in Gitea for version history, and always Vault-encrypt anything containing credentials
Troubleshooting
- SSH access issue, confirm the control node’s key is actually authorized on every managed host
- Python missing on target, Ansible needs Python on the managed host; some minimal images don’t include it by default
- Host key mismatch, a reinstalled host will have a new SSH host key; update
known_hosts - Wrong inventory group, double-check which group a playbook targets before running it
- Playbook changes more than intended, always test with
--check(dry run) before applying for real
Lab Finish Line
One playbook applies the same baseline configuration to two Linux servers.