Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
You install Ansible only on the control node (your laptop, a bastion or a CI runner). The managed nodes need only SSH and Python. A playbook (YAML) lists the tasks; the inventory lists the servers.
| Item | Value |
|---|---|
| Packages | ansible-core (the engine + ansible.builtin) or ansible (engine + many collections) |
| Commands | ansible, ansible-playbook, ansible-inventory, ansible-vault, ansible-galaxy, ansible-doc |
| Config file | ./ansible.cfg (in the project — best), ~/.ansible.cfg, /etc/ansible/ansible.cfg |
| Inventory | inventory.ini or inventory.yml in the project |
| Collections | ~/.ansible/collections/ |
| Connection | SSH, port 22, as a user with sudo on the managed nodes |
Install on RHEL, Rocky, Alma and Fedora
For the full ansible package (with community collections) on Rocky/Alma, use EPEL: dnf -y install epel-release && dnf -y install ansible.
Install on Ubuntu and Debian
The Ansible PPA has newer versions than Ubuntu itself:
apt-get update
apt-get install -y software-properties-common
add-apt-repository --yes --update ppa:ansible/ansible
apt-get install -y ansible
ansible --versionOp het werk
On any Linux (and in CI) you can also install a fixed version with pipx: pipx install --include-deps ansible. Pin the version so every runner is the same.
Prepare the managed servers
On each server: a user for Ansible with your SSH key and sudo.
ssh-copy-id admin@web1
ssh admin@web1 'sudo -n true && echo sudo-ok'
sudo-okIf sudo asks for a password, run playbooks with -K (ask for the become password), or give the user a rule in /etc/sudoers.d/ made with visudo.
First configuration: a clean project
Keep each Ansible project in its own Git folder with its own ansible.cfg.
[defaults]
inventory = inventory.ini
remote_user = admin
forks = 20
stdout_callback = yaml
retry_files_enabled = False
interpreter_python = auto_silent
[privilege_escalation]
become = True
become_method = sudo
[ssh_connection]
pipelining = True[web]
web1 ansible_host=192.0.2.11
web2 ansible_host=192.0.2.12
[db]
db1 ansible_host=192.0.2.21
[prod:children]
web
db| Setting | Why |
|---|---|
inventory | No -i on every command. |
forks = 20 | 20 servers at the same time (default 5). |
become = True | Tasks run with sudo. |
pipelining = True | Fewer SSH steps, much faster runs. |
host_key_checking | Leave it on (default). Turn it off only in throw-away labs. |
Test the connection to all servers:
cd ~/infra
ansible-inventory --graph
ansible all -m ping
web1 | SUCCESS => {"changed": false, "ping": "pong"}
web2 | SUCCESS => {"changed": false, "ping": "pong"}
db1 | SUCCESS => {"changed": false, "ping": "pong"}Your first playbook
Install Nginx on the web servers, put a page in place and keep the service running:
- name: Web servers
hosts: web
tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Home page
ansible.builtin.copy:
content: "Hello from {{ inventory_hostname }}\n"
dest: /usr/share/nginx/html/index.html
mode: "0644"
- name: Nginx running and enabled
ansible.builtin.service:
name: nginx
state: started
enabled: trueansible-playbook web.yml --check --diff
ansible-playbook web.yml
ansible-playbook web.ymlOnthoud
The second run shows changed=0. That is idempotence: Ansible only changes what is not yet right, so you can run a playbook again and again.
Keep secrets with Ansible Vault
ansible-vault create group_vars/db/vault.yml
ansible-vault edit group_vars/db/vault.yml
ansible-playbook db.yml --ask-vault-passWrite vault_db_password: "…" in the encrypted file and use {{ vault_db_password }} in tasks. Add no_log: true to tasks that handle secrets, so they never appear in the output.
Daily use: the commands and the real uses
ansible-playbook site.yml --check --diff
ansible-playbook site.yml --limit web1
ansible-playbook site.yml --tags nginx
ansible web -m shell -a 'uptime'
ansible all -m setup -a 'filter=ansible_distribution*'
ansible-doc ansible.builtin.copy
ansible-galaxy init roles/nginx
ansible-galaxy collection install -r collections/requirements.ymlReal uses at work:
- Same baseline on every server: users, SSH hardening, packages, time, firewall.
- Patch days: update 100 servers in batches (
serial: 10). - Deploy apps and config files; set up Docker or Kubernetes nodes.
- Run from CI (GitLab/GitHub) after a merge, with the vault password from the CI secret store.
Make it safe
Security checklist:
- Secrets only in Ansible Vault (or HashiCorp Vault), never in plain YAML in Git.
no_log: trueon tasks with passwords or tokens.- SSH keys, host key checking on, a dedicated Ansible user with only the
sudoit needs. - Always
--check --difffirst on production; use--limitfor a small first batch. - Pin collection versions in
collections/requirements.yml. - Check playbooks with
ansible-lintin CI.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
UNREACHABLE! … Permission denied (publickey) | SSH key or user is wrong. | ssh admin@HOST by hand; fix remote_user or copy the key. |
Missing sudo password | sudo needs a password. | Run with -K, or a NOPASSWD sudo rule. |
[WARNING]: Ansible is being run in a world writable directory … ignoring it as an ansible.cfg source | The project folder is writable by everyone. | chmod o-w ~/infra. |
No python interpreters found | The server has no Python (minimal images). | Install python3, or set ansible_python_interpreter. |
Host key verification failed | The server is new or was rebuilt. | ssh-keyscan HOST >> ~/.ssh/known_hosts after you check the key. |
couldn't resolve module/action | The collection is missing. | ansible-galaxy collection install community.general. |
| Variables not used | Wrong folder name or precedence. | ansible-inventory --host web1 shows the final variables. |
Next steps
- Quick how-tos: Install a package on many servers, Run one command on many servers, Write a role, Ansible Vault, Facts and conditions.
- Command page: ansible — every option and example.
- Ansible at work: Configure servers over SSH, Ansible — configure servers.
- The SSH side: Install and harden the OpenSSH server.
1Where do you install Ansible: on the control node or on every server?
Only on the control node. The servers need SSH and Python.
2The second run of a playbook shows changed=0. Is that a problem?
No. It shows the playbook is idempotent: everything is already in the right state.