Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 6Ansible · IaC and automation

Install and set up Ansible

Ansible configures many servers from one place over SSH: install packages, copy config files, start services, create users — the same way every time. It needs no agent on the servers. This guide installs Ansible on a control machine (RHEL or Ubuntu), builds a clean project with ansible.cfg and an inventory, runs a first playbook, and keeps passwords safe with Ansible Vault.

  • 12 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

6.1

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.

ItemValue
Packagesansible-core (the engine + ansible.builtin) or ansible (engine + many collections)
Commandsansible, ansible-playbook, ansible-inventory, ansible-vault, ansible-galaxy, ansible-doc
Config file./ansible.cfg (in the project — best), ~/.ansible.cfg, /etc/ansible/ansible.cfg
Inventoryinventory.ini or inventory.yml in the project
Collections~/.ansible/collections/
ConnectionSSH, port 22, as a user with sudo on the managed nodes

6.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install ansible-core
ansible --version
ansible [core 2.16.14]

For the full ansible package (with community collections) on Rocky/Alma, use EPEL: dnf -y install epel-release && dnf -y install ansible.

6.3

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 --version

Op 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.

6.4

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-ok

If 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.

6.5

First configuration: a clean project

Keep each Ansible project in its own Git folder with its own ansible.cfg.

~/infra/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
~/infra/inventory.ini
[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
SettingWhy
inventoryNo -i on every command.
forks = 2020 servers at the same time (default 5).
become = TrueTasks run with sudo.
pipelining = TrueFewer SSH steps, much faster runs.
host_key_checkingLeave 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"}

6.6

Your first playbook

Install Nginx on the web servers, put a page in place and keep the service running:

~/infra/web.yml
- 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: true
ansible-playbook web.yml --check --diff
ansible-playbook web.yml
ansible-playbook web.yml

Onthoud

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.

6.7

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-pass

Write 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.

6.8

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.yml

Real 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.

6.9

Make it safe

Security checklist:

  • Secrets only in Ansible Vault (or HashiCorp Vault), never in plain YAML in Git.
  • no_log: true on tasks with passwords or tokens.
  • SSH keys, host key checking on, a dedicated Ansible user with only the sudo it needs.
  • Always --check --diff first on production; use --limit for a small first batch.
  • Pin collection versions in collections/requirements.yml.
  • Check playbooks with ansible-lint in CI.

Commando's in deze les

6.10

Update and remove

dnf -y upgrade ansible-core
apt-get install --only-upgrade -y ansible
pipx upgrade ansible
dnf -y remove ansible-core

Commando's in deze les

6.11

Fix common problems

ProblemCauseFix
UNREACHABLE! … Permission denied (publickey)SSH key or user is wrong.ssh admin@HOST by hand; fix remote_user or copy the key.
Missing sudo passwordsudo 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 sourceThe project folder is writable by everyone.chmod o-w ~/infra.
No python interpreters foundThe server has no Python (minimal images).Install python3, or set ansible_python_interpreter.
Host key verification failedThe server is new or was rebuilt.ssh-keyscan HOST >> ~/.ssh/known_hosts after you check the key.
couldn't resolve module/actionThe collection is missing.ansible-galaxy collection install community.general.
Variables not usedWrong folder name or precedence.ansible-inventory --host web1 shows the final variables.

6.12

Next steps

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.

Alle commando's in dit hoofdstuk