Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

ansible

Configure many Linux (and Windows) servers over SSH with YAML playbooks — no agent on the servers.

Niveau
DevSecOps-service
Groep
Infrastructuur als code en cloud
Root nodig
Voor wijzigingen
Pakket
ansible-core

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

Wat het doet

Ansible connects with SSH, copies small modules, runs them and removes them. You list servers in an inventory, and describe the wanted state in playbooks (tasks that call modules: dnf, copy, template, service, user, firewalld …). Modules are idempotent: run a playbook twice and the second run changes nothing. Roles package tasks, templates and defaults; collections bring more modules (for example ansible.posix, community.general, azure.azcollection).

Terraform builds the servers; Ansible configures what is inside. Ansible is the main tool of the RHCE exam (EX294). On RHEL install ansible-core.

Zo schrijf je het

ansible PATTERN -m MODULE [-a ARGS] [-i INVENTORY]
ansible-playbook [-i INVENTORY] PLAYBOOK.yml [--check] [--diff]

Belangrijkste delen

Tools

ansible
Run one module on hosts (ad hoc).
ansible-playbook
Run a playbook.
ansible-inventory --graph
Show the inventory.
ansible-doc MODULE
Module docs and examples.
ansible-galaxy collection|role install
Install collections and roles.
ansible-vault create|edit|encrypt|view
Encrypt secret files.
ansible-config dump --only-changed
Show your config changes.
ansible-lint
Check playbooks for bad practice.
ansible-navigator
Run playbooks in execution environments (containers).

Opties 15

Meest gebruikt

ansible-playbook flags

-i FILE
Inventory file or folder.
-l PATTERN
Limit to some hosts.
--check / -C
Dry run, change nothing.
--diff / -D
Show file changes.
-b, --become
Use sudo.
-K
Ask for the sudo password.
-u USER
Remote user.
-t TAGS / --skip-tags TAGS
Run only / skip tagged tasks.
-e 'k=v' / -e @file.yml
Extra variables.
--ask-vault-pass / --vault-password-file FILE
Vault password.
--syntax-check
Only check the syntax.
--list-tasks / --list-hosts
Show what would run.
--start-at-task NAME
Start in the middle.
-f N
Parallel forks (default 5).
-v … -vvvv
More output.

Voorbeelden 11

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

  1. 1Install and write an inventory

    dnf install -y ansible-core
    cat inventory.ini
    [web]
    web1.lab.local
    web2.lab.local
    [db]
    db1.lab.local ansible_user=admin
    [lab:children]
    web
    db
  2. 2Test the connection and run ad hoc commands

    ansible all -i inventory.ini -m ping
    ansible web -i inventory.ini -b -m dnf -a "name=nginx state=present"
    ansible all -i inventory.ini -m command -a "uptime"
  3. 3A playbook for web servers

    cat web.yml
    - name: Web servers
      hosts: web
      become: true
      tasks:
        - name: Install nginx
          ansible.builtin.dnf: { name: nginx, state: present }
        - name: Site config
          ansible.builtin.template: { src: site.conf.j2, dest: /etc/nginx/conf.d/site.conf, mode: "0644" }
          notify: Reload nginx
        - name: Open HTTP and HTTPS
          ansible.posix.firewalld: { service: "{{ item }}", permanent: true, immediate: true, state: enabled }
          loop: [http, https]
        - name: Start nginx
          ansible.builtin.service: { name: nginx, state: started, enabled: true }
      handlers:
        - name: Reload nginx
          ansible.builtin.service: { name: nginx, state: reloaded }
  4. 4Dry run first, then apply

    ansible-playbook -i inventory.ini web.yml --check --diff
    ansible-playbook -i inventory.ini web.yml
  5. 5Run on one host and some tags only

    ansible-playbook -i inventory.ini site.yml -l web1.lab.local -t nginx
  6. 6Keep secrets in a vault

    ansible-vault create group_vars/db/vault.yml
    ansible-vault view group_vars/db/vault.yml
    ansible-playbook -i inventory.ini db.yml --vault-password-file ~/.vault_pass
  7. 7Harden SSH on all servers

    cat ssh-hardening.yml
    - hosts: all
      become: true
      tasks:
        - ansible.builtin.lineinfile:
            path: /etc/ssh/sshd_config.d/50-hardening.conf
            create: true
            line: "{{ item }}"
            mode: "0600"
          loop: ["PermitRootLogin no", "PasswordAuthentication no", "MaxAuthTries 3"]
          notify: Restart sshd
      handlers:
        - name: Restart sshd
          ansible.builtin.service: { name: sshd, state: restarted }
  8. 8Collect facts

    ansible web1.lab.local -i inventory.ini -m setup -a "filter=ansible_distribution*"
  9. 9Install collections and roles

    cat requirements.yml
    collections:
      - name: ansible.posix
      - name: community.general
    ansible-galaxy collection install -r requirements.yml
  10. 10Patch all servers one by one

    cat patch.yml
    - hosts: web
      become: true
      serial: 1
      tasks:
        - ansible.builtin.dnf: { name: "*", state: latest, security: true }
        - ansible.builtin.reboot:
  11. 11Find the docs of a module

    ansible-doc ansible.builtin.user | less

Let op

  • Use modules, not shell/command; shell tasks are not idempotent and show "changed" every time.
  • Never put passwords in plain YAML — use Ansible Vault or a secret manager lookup.
  • --check cannot predict tasks that depend on earlier changes.

Bestanden

ansible.cfg
Settings (searched in ./, ~/.ansible.cfg, /etc/ansible/).
inventory.ini / inventory.yml
Hosts and groups.
group_vars/ host_vars/
Variables per group / host.
roles/
Roles (tasks, handlers, templates, defaults).

Tips om te onthouden

  • Put inventory = ./inventory.ini and remote_user in a project ansible.cfg.
  • Run ansible-lint in CI on every playbook change.

Exitcodes

0Success.
1Error.
2One or more hosts failed.
4One or more hosts unreachable.