Nach der Installation von Ansible beginnen Sie typischerweise mit Ad-hoc-Befehlen, um die Konnektivität zu testen und sich mit der CLI vertraut zu machen. Diese Befehle sind besonders nützlich für schnelle administrative Aufgaben und Troubleshooting.
# Einfacher Ping-Test aller Hosts
ansible all -m ping
# Mit spezifischem Inventar und Benutzer
ansible -i inventory/hosts -u ansible all -m ping
# Bei SSH-Passwort-Authentifizierung
ansible -i inventory/hosts -u ansible --ask-pass all -m ping
# Mit Sudo-Berechtigung für privilegierte Befehle
ansible -i inventory/hosts -u ansible --ask-become-pass all -m ping# Systeminfo abfragen
ansible all -m shell -a "uptime" -u ansible
# Pakete verwalten (Debian/Ubuntu)
ansible webservers -m apt -a "name=nginx state=present" -u ansible --become
# Dateien kopieren
ansible all -m copy -a "src=/tmp/test.txt dest=/tmp/test.txt" -u ansible
# Services verwalten
ansible webservers -m systemd -a "name=nginx state=started enabled=yes" -u ansible --become
# Fakten sammeln
ansible all -m setup -u ansible-m - Spezifiziert das Ansible-Modul-a - Argumente für das Modul-i - Inventar-Datei oder -Verzeichnis-u - SSH-Benutzer--ask-pass - Fordert SSH-Passwort interaktiv an--ask-become-pass - Fordert Sudo-Passwort an--become - Aktiviert Privilege Escalation--check - Dry-run Modus (zeigt nur geplante
Änderungen)-v, -vv, -vvv - Verschiedene Verbose-Level für
DebuggingEin produktionstaugliches Ansible-Projekt folgt etablierten Konventionen und nutzt moderne Features wie Collections. Die empfohlene Struktur orientiert sich an den Ansible Best Practices:
my-ansible-project/
├── ansible.cfg # Projektkonfiguration
├── requirements.yml # External Collections/Roles
├── inventory/
│ ├── hosts # Hauptinventar
│ ├── group_vars/
│ │ ├── all.yml # Globale Variablen
│ │ ├── webservers.yml # Gruppenspezifische Variablen
│ │ └── databases.yml
│ └── host_vars/
│ ├── web1.yml # Host-spezifische Variablen
│ └── db1.yml
├── playbooks/
│ ├── site.yml # Haupt-Playbook
│ ├── webserver.yml # Komponenten-Playbooks
│ └── database.yml
├── roles/
│ ├── common/ # Eigene Rollen
│ │ ├── tasks/main.yml
│ │ ├── handlers/main.yml
│ │ ├── templates/
│ │ ├── files/
│ │ ├── vars/main.yml
│ │ └── defaults/main.yml
│ └── webserver/
├── collections/
│ └── ansible_collections/ # Heruntergeladene Collections
├── files/ # Globale Dateien
├── templates/ # Globale Templates
└── vault/ # Verschlüsselte Secrets (optional)
[defaults]
inventory = inventory/hosts
remote_user = ansible
host_key_checking = False
retry_files_enabled = False
collections_path = ./collections
roles_path = ./roles
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
pipelining = True---
collections:
- name: community.general
version: ">=3.0.0"
- name: ansible.posix
version: ">=1.0.0"
- name: community.crypto
version: ">=1.0.0"
roles:
- name: geerlingguy.nginx
version: "3.1.4"Installation der Dependencies:
ansible-galaxy install -r requirements.yml---
- name: "Webserver Setup"
hosts: webservers
become: true
vars:
nginx_port: 80
tasks:
- name: "Install Nginx"
ansible.builtin.package:
name: nginx
state: present
- name: "Start and enable Nginx"
ansible.builtin.systemd:
name: nginx
state: started
enabled: true
- name: "Deploy index.html"
ansible.builtin.template:
src: index.html.j2
dest: /var/www/html/index.html
owner: www-data
group: www-data
mode: '0644'
notify: restart nginx
handlers:
- name: "restart nginx"
ansible.builtin.systemd:
name: nginx
state: restarted# Syntax-Check
ansible-playbook --syntax-check playbooks/webserver.yml
# Dry-run (Check-Modus)
ansible-playbook -i inventory/hosts -u ansible --check playbooks/webserver.yml
# Produktive Ausführung
ansible-playbook -i inventory/hosts -u ansible playbooks/webserver.yml
# Mit spezifischer Gruppe und Tags
ansible-playbook -i inventory/hosts -u ansible --limit webservers --tags "nginx" playbooks/webserver.yml
# Mit erhöhtem Verbose-Level für Debugging
ansible-playbook -i inventory/hosts -u ansible -vvv playbooks/webserver.ymlInstallation und Verwendung:
# Installation
pip install ansible-lint
# Linting eines Playbooks
ansible-lint playbooks/webserver.yml
# Linting des gesamten Projekts
ansible-lint .# YAML-Syntax einzelner Dateien
ansible-playbook --syntax-check playbooks/webserver.yml
# Inventar validieren
ansible-inventory -i inventory/hosts --list- name: "Using community collection modules"
hosts: all
tasks:
- name: "Manage systemd service with community.general"
community.general.systemd:
name: nginx
state: started# Verschlüsselte Datei erstellen
ansible-vault create vault/secrets.yml
# Verschlüsselte Datei bearbeiten
ansible-vault edit vault/secrets.yml
# Playbook mit Vault ausführen
ansible-playbook --ask-vault-pass playbooks/webserver.yml# inventory/dynamic_aws.yml
plugin: amazon.aws.aws_ec2
regions:
- us-east-1
- eu-central-1
keyed_groups:
- key: tags.Environment
prefix: env
- key: instance_type
prefix: typeSSH-Verbindungsprobleme:
# Detaillierte SSH-Logs
ansible all -m ping -vvv
# SSH-Konfiguration testen
ssh -F ~/.ssh/config ansible@target-hostBerechtigungsprobleme:
# Sudo-Rechte testen
ansible all -m shell -a "whoami" --become
# Ansible-Benutzer-Setup überprüfen
ansible all -m shell -a "sudo -l"Inventar-Debugging:
# Inventar anzeigen
ansible-inventory -i inventory/hosts --list --yaml
# Host-spezifische Variablen anzeigen
ansible-inventory -i inventory/hosts --host web1