Article original publié le : 04 juin 2021 Mise a jour le : |
Quelques exemples pour tester les modules, copier un fichier, installer un composant, jouer une commande ou encore redémarrer un service.
Une première approche avant de les utiliser sous forme de task dans les playbooks
Mon nom de machine est vmcentos8
Module PING : le plus simple
1 |
$ ansible -i "vmcentos8," all -m ping |
1 2 3 4 5 6 7 |
vmcentos8 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" } |
/!\ Si on ne set pas les privilege_escalation on ajoute alors manuellement les arguments
-b: active l’escalade de privilège -K: élévation des droits sudo -a: module argument -m: module name -u: spécifier un utilisateur |
Module COPY : Copier un fichier vers une machine distante
1 |
$ ansible -i "vmcentos8," all -m copy -a "src=/etc/hosts dest=/root/hosts.save" -bK |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
BECOME password: vmcentos8 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "5e064ea8b7e3bf6ec9d0ec62b0bb5ae44ae8b13c", "dest": "/root/hosts.save", "gid": 0, "group": "root", "md5sum": "43994ee03d5a4df7a89ccfc20d059a20", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 212, "src": "/home/vagrant/.ansible/tmp/ansible-tmp-1622795115.98-8888-24271014112957/source", "state": "file", "uid": 0 } |
Module FETCH : Récupérer un fichier d’une machine distante
1 |
$ ansible -i "vmcentos8," all -m fetch -a "src=/var/log/secure dest=~/root flat=yes" -bK |
1 2 3 4 5 6 7 8 9 |
BECOME password: vmcentos8 | CHANGED => { "changed": true, "checksum": "eb02ea3b9f9af2dd1ad65a5d8ee2e376390314dc", "dest": "/home/vagrant/root", "md5sum": "50bcaae73a521100b6a3c9dea64c1992", "remote_checksum": "eb02ea3b9f9af2dd1ad65a5d8ee2e376390314dc", "remote_md5sum": null } |
Module COMMAND : Lancer une commande système
1 |
$ ansible -i "vmcentos8," all -m command -a "df -h" |
1 2 3 4 5 6 7 8 |
vmcentos8 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on devtmpfs 467M 0 467M 0% /dev tmpfs 485M 0 485M 0% /dev/shm tmpfs 485M 13M 473M 3% /run tmpfs 485M 0 485M 0% /sys/fs/cgroup /dev/sda1 10G 3.6G 6.5G 36% / tmpfs 97M 0 97M 0% /run/user/1000 |
Module PACKAGE MANAGEMENT : Pour les gestionnaires de packages comme yum ou apt, dans l’exemple installation du paquet httpd
1 |
$ ansible -i "vmcentos8," all -m yum -a "name=httpd state=latest" -bK |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
BECOME password: vmcentos8 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "msg": "", "rc": 0, "results": [ "Installed: apr-util-openssl-1.6.1-6.el8.x86_64", "Installed: mailcap-2.1.48-3.el8.noarch", "Installed: centos-logos-httpd-85.5-1.el8.noarch", "Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64", "Installed: httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64", "Installed: httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch", "Installed: apr-1.6.3-11.el8.x86_64", "Installed: httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64", "Installed: apr-util-1.6.1-6.el8.x86_64", "Installed: apr-util-bdb-1.6.1-6.el8.x86_64" ] } |
Module SERVICE : Dans l’exemple pour relancer le service httpd
1 |
$ ansible -i "vmcentos8," all -m service -a "name=httpd state=restarted" -bK |
1 2 3 4 5 6 7 8 9 10 11 |
BECOME password: vmcentos8 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "name": "httpd", "state": "started", ... } } |
Module SHELL : Lancer une instruction
1 |
$ ansible -i "vmcentos8," all -m shell -a "ps faux | grep httpd" |
1 2 3 4 5 6 7 8 |
vmcentos8 | CHANGED | rc=0 >> vagrant 37360 0.0 0.2 9772 2664 pts/1 S+ 10:34 0:00 \_ /bin/sh -c ps faux | grep httpd vagrant 37362 0.0 0.1 9184 1088 pts/1 S+ 10:34 0:00 \_ grep httpd root 36064 0.0 1.1 275920 11076 ? Ss 10:20 0:00 /usr/sbin/httpd -DFOREGROUND apache 36065 0.0 0.8 289792 8236 ? S 10:20 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 36066 0.0 1.2 1347592 11964 ? Sl 10:20 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 36067 0.0 1.2 1347592 11964 ? Sl 10:20 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 36068 0.0 1.4 1478720 13956 ? Sl 10:20 0:00 \_ /usr/sbin/httpd -DFOREGROUND |
Module SETUP : Utile pour les gather_fact, sans argument, c’est trop verbeux du coup il faut utiliser des filtres
1 |
$ ansible -i "vmcentos8," all -m setup |
Pour récupérer la partie devices par exemple
1 |
$ ansible -i "vmcentos8," all -m setup -a "filter=ansible_devices*" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
vmcentos8 | SUCCESS => { "ansible_facts": { "ansible_devices": { "sda": { "holders": [], "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)", "links": { "ids": [ "ata-VBOX_HARDDISK_VB6f0b5750-51c527e7", "scsi-0ATA_VBOX_HARDDISK_VB6f0b5750-51c527e7", "scsi-1ATA_VBOX_HARDDISK_VB6f0b5750-51c527e7", "scsi-SATA_VBOX_HARDDISK_VB6f0b5750-51c527e7" ], "labels": [], "masters": [], "uuids": [] }, "model": "VBOX HARDDISK", "partitions": { "sda1": { "holders": [], "links": { "ids": [ "ata-VBOX_HARDDISK_VB6f0b5750-51c527e7-part1", "scsi-0ATA_VBOX_HARDDISK_VB6f0b5750-51c527e7-part1", "scsi-1ATA_VBOX_HARDDISK_VB6f0b5750-51c527e7-part1", "scsi-SATA_VBOX_HARDDISK_VB6f0b5750-51c527e7-part1" ], "labels": [], "masters": [], "uuids": [ "a62c5b49-755e-41b0-9d36-de3d95e17232" ] }, "sectors": "20969472", "sectorsize": 512, "size": "10.00 GB", "start": "2048", "uuid": "a62c5b49-755e-41b0-9d36-de3d95e17232" } }, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "mq-deadline", "sectors": "20971520", "sectorsize": "512", "size": "10.00 GB", "support_discard": "0", "vendor": "ATA", "virtual": 1 } }, "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false } |
Ou pour récupérer toutes les adresses ipv4 de la machine
1 |
$ ansible -i "vmcentos8," all -m setup -a "filter=ansible_all_ipv4_addresses*" |
1 2 3 4 5 6 7 8 9 10 |
vmcentos8 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "10.0.2.15", "192.168.170.3" ], "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false } |