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-docMODULE- Module docs and examples.
ansible-galaxycollection|roleinstall- Install collections and roles.
ansible-vaultcreate|edit|encrypt|view- Encrypt secret files.
ansible-configdump--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
Niets past bij dit filter.
Voorbeelden 11
$ als gewone gebruiker# als root (of met sudo)
1Install and write an inventory
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"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 }4Dry run first, then apply
ansible-playbook -i inventory.ini web.yml --check --diff ansible-playbook -i inventory.ini web.yml5Run on one host and some tags only
ansible-playbook -i inventory.ini site.yml -l web1.lab.local -t nginx6Keep 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_pass7Harden 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 }8Collect facts
ansible web1.lab.local -i inventory.ini -m setup -a "filter=ansible_distribution*"9Install collections and roles
cat requirements.yml collections: - name: ansible.posix - name: community.general ansible-galaxy collection install -r requirements.yml10Patch 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:11Find the docs of a module
ansible-doc ansible.builtin.user | less
Niets past bij dit filter.
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.
--checkcannot 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.iniandremote_userin a projectansible.cfg. - Run
ansible-lintin CI on every playbook change.
Exitcodes
0 | Success. |
1 | Error. |
2 | One or more hosts failed. |
4 | One or more hosts unreachable. |