Ansible: la commande shell

La commande shell permet d'exécuter des commandes simples mais aussi complexes et également sous forme de scripts.

Documentation Ansible en ligne

Exemples:

$ ansible -m shell -a 'find /var/log/nginx/ -type f -name "access.log*" | xargs stat --printf "%-80n%-15s%-35w\n"' nginx
SERVER2 | CHANGED | rc=0 >>
/var/log/nginx/access.log-20240116                                              7052050        2023-11-15 17:37:33.620096531 +0100
/var/log/nginx/access.log                                                       10191          2024-01-16 09:28:20.137458998 +0100
SERVER1 | CHANGED | rc=0 >>
/var/log/nginx/access.log-20240116                                              2101711        2023-11-15 16:03:11.480149331 +0100
/var/log/nginx/access.log                                                       22278          2024-01-16 09:29:28.959290202 +0100

$ ansible -m shell -a 'cat /etc/passwd | grep root' SERVER1:SERVER2
SERVER1 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
SERVER2 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash

$ ansible -m shell -a 'echo "Hello world!" > /tmp/hello_world ; ls -l /tmp/hello_world ; cat /tmp/hello_world' localhost
localhost | CHANGED | rc=0 >>
-rw-r--r-- 1 user group 13  5 juin  14:20 /tmp/hello_world
Hello world!

Etiquettes: