Een praktische handleiding om Ansible te installeren, inventory-bestanden te organiseren, uw eerste YAML-playbook te schrijven en servertaken te orkestreren.
🔗 Related: Ansible Basis Spiekbrief · Ansible Roles Spiekbrief · Ansible Playbook Voorbeelden
Installatie
# macOS (Homebrew)
brew install ansible
# Ubuntu / Debian
sudo apt update && sudo apt install -y ansible
# Python Pip (Universal)
pip install ansible
Zie: Officiële Ansible-installatiedocumentatie: Ansible Installation Docs
Projectopzet
mkdir -p ~/ansible-setup
cd ~/ansible-setup
Maak een speciale werkmap aan voor uw inventory, playbooks en variabelendefinities.
Configuratiebestanden
Inventory-bestand
hosts
[webservers]
192.168.0.10
192.168.0.11
[databases]
192.168.0.20
[local]
localhost ansible_connection=local
Inventories definiëren doelknooppunten in logische groepen. U kunt localhost targeten met ansible_connection=local om automatisering lokaal te testen zonder SSH.
Zie: Handleiding voor het opbouwen van een Ansible-inventory: Ansible How to build your inventory
Playbook-definitie
playbook.yml
---
- name: Basic Server Provisioning
hosts: webservers
become: true
tasks:
- name: Update apt package cache
apt:
update_cache: yes
cache_valid_time: 3600
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
- name: Start Nginx and enable on boot
service:
name: nginx
state: started
enabled: yes
- name: Create hello world banner
shell: echo "Managed by Ansible" > /tmp/motd.txt
Uitvoering
Valideren en uitvoeren
Controleer de connectiviteit via ad-hoc ping:
ansible -i hosts all -m ping
Voer het volledige playbook uit op uw inventory:
ansible-playbook -i hosts playbook.yml
Proefdraaien (Check-modus)
Bekijk mogelijke wijzigingen vooraf zonder ze op externe knooppunten toe te passen:
ansible-playbook -i hosts playbook.yml --check --diff