Ajouter une condition when os family au playbook ansible |
Article original Publié le : 02 octobre 2020 Mise a jour le : – |
Utiliser l’argument when os family pour jouer les instructions du playbook selon l’os
Ce playbook avait pour but de stopper le service ossec et de désactiver son démarrage automatique,
l’occasion d’utiliser cette condition et aussi d’utiliser la possibilité de passer par un bastion pour atteindre les machines.
Le playbook se trouve sur github
Structure du dossier
1 2 3 4 5 6 |
. ├── inventories │ ├── bastion │ └── hosts ├── playbook_ossec.yml └── README.md |
Fichier bastion contient l’instruction pour le rebond, changer la valeur hostbastion et le port
1 |
$ vim inventories/bastion |
1 |
$ ansible_ssh_common_args: "-o ForwardAgent=yes -o ProxyCommand='ssh -o StrictHostKeyChecking=no -W %h:%p -q <strong>hostbastion</strong> <strong>-p2222</strong>'" |
Fichier hosts contient l’inventaire des machines
1 |
$ vim inventories/hosts |
1 2 3 4 |
[all] host1 host2 host3 |
Fichier playbook_ossec.yml
|
Fichier README.md
Sur un environnement sans bastion exécuter la commande
1 |
$ ansible-playbook -i inventories/hosts playbook_ossec.yml -K |
Sur un environnement avec bastion exécuter la commande
1 |
$ ansible-playbook -i inventories/hosts playbook_ossec.yml -e '@inventories/bastion' -K |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span style="font-size: 8pt;">PLAY [all] ************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************* <span style="color: #00ff00;">ok: [192.168.3.225] ok: [192.168.3.224]</span> TASK [désactivation du service centos 6] ******************************************************************************************************* <span style="color: #ffff00;">changed: [192.168.3.224] => (item=chronyd) changed: [192.168.3.225] => (item=chronyd)</span> TASK [arret du service centos 6] *************************************************************************************************************** <span style="color: #00ffff;">skipping: [192.168.3.225]</span> <span style="color: #ffff00;">changed: [192.168.3.224] </span> TASK [désactivation et arret du service centos 7] ********************************************************************************************** <span style="color: #00ff00;">ok: [192.168.3.224]</span> <span style="color: #ffff00;">changed: [192.168.3.225]</span> PLAY RECAP ************************************************************************************************************************************* <span style="color: #ffff00;">192.168.3.224 </span> : <span style="color: #00ff00;">ok=4</span> <span style="color: #ffff00;">changed=2</span> unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 <span style="color: #ffff00;">192.168.3.225 </span> : <span style="color: #00ff00;">ok=3</span> <span style="color: #ffff00;">changed=2</span> unreachable=0 failed=0 <span style="color: #00ffff;">skipped=1</span> rescued=0 ignored=0</span> |