Commandes ad-hoc 18 min de lecture

Syntaxe et modules de base

Syntaxe des commandes ad-hoc

Les commandes ad-hoc permettent d'executer une tache rapide sans ecrire de playbook.

# Syntaxe generale
ansible <pattern> -m <module> -a "<arguments>" -i <inventaire>

# Exemples
# Ping tous les hotes
ansible all -m ping

# Ping un groupe specifique
ansible webservers -m ping

# Ping un hote specifique
ansible web1.example.com -m ping

Module ping

Le module ping verifie la connectivite et la presence de Python sur l'hote cible.

ansible all -m ping
# Resultat attendu :
# web1.example.com | SUCCESS => {
#     "changed": false,
#     "ping": "pong"
# }

Module shell et command

# Module command (par defaut, sans shell)
ansible all -m command -a "uptime"
ansible all -a "df -h"    # command est le module par defaut

# Module shell (avec interpretation shell)
ansible all -m shell -a "cat /etc/os-release | head -2"
ansible all -m shell -a "ps aux | grep nginx | wc -l"

Module copy

# Copier un fichier vers les hotes
ansible webservers -m copy -a "src=./index.html dest=/var/www/html/index.html owner=www-data mode=0644"

# Creer un fichier avec du contenu
ansible all -m copy -a "content='Hello Ansible' dest=/tmp/test.txt"
Difference command vs shell : Le module command n'interprete pas les pipes et redirections. Utilisez shell si vous avez besoin de ces fonctionnalites.