This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
docpublic:systemes:ansible_init [2022/08/19 12:03] adminjp [ansible module user] |
docpublic:systemes:ansible_init [2022/08/21 08:49] (current) adminjp [ansible module ssh] |
||
---|---|---|---|
Line 1178: | Line 1178: | ||
jehan@node3:/ | jehan@node3:/ | ||
uid=1041(joe) gid=1041(joe) groups=1041(joe), | uid=1041(joe) gid=1041(joe) groups=1041(joe), | ||
+ | </ | ||
+ | |||
+ | === afficher les details === | ||
+ | |||
+ | pour voir les details systems de ce qui a été fait on ajoute un register de notre user joe avec un debug sur cette variable : | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | --- | ||
+ | - name: J_Playbook_User | ||
+ | hosts: node3 | ||
+ | become: yes | ||
+ | tasks: | ||
+ | - name: create_user_joe | ||
+ | user: | ||
+ | name: joe | ||
+ | state: present | ||
+ | uid: 1041 | ||
+ | groups: sudo | ||
+ | password: "{{ ' | ||
+ | register: __user_joe | ||
+ | - name: debug_user | ||
+ | debug: | ||
+ | var: __user_joe | ||
+ | </ | ||
+ | |||
+ | |||
+ | === resultat === | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | BECOME password: | ||
+ | ... | ||
+ | TASK [create_user_joe] ****************************************************************************************** | ||
+ | Friday 19 August 2022 16:13:18 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | TASK [debug_user] *********************************************************************************************** | ||
+ | Friday 19 August 2022 16:13:19 +0200 (0: | ||
+ | ok: [node3] => { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | PLAY RECAP ****************************************************************************************************** | ||
+ | node3 : ok=3 changed=1 | ||
+ | |||
+ | Friday 19 August 2022 16:13:19 +0200 (0: | ||
+ | =============================================================================== | ||
+ | Gathering Facts ------------------------------------------------------------------------------------------ 1.53s | ||
+ | create_user_joe ------------------------------------------------------------------------------------------ 0.54s | ||
+ | debug_user ----------------------------------------------------------------------------------------------- 0.04s | ||
+ | </ | ||
+ | |||
+ | ===== ansible stat register ===== | ||
+ | |||
+ | si on souhaite afficher des info sur nos actions, on peut utiliser le module stat sur un fichier par exemple, mais l' | ||
+ | |||
+ | === playbook === | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | --- | ||
+ | - name: J_Playbook_Stat_Reg | ||
+ | hosts: node3 | ||
+ | become: yes | ||
+ | tasks: | ||
+ | - name: create_file | ||
+ | file: | ||
+ | path: "/ | ||
+ | state: touch | ||
+ | owner: root | ||
+ | group: ans | ||
+ | mode: 0755 | ||
+ | - name: stat_file | ||
+ | stat: | ||
+ | path: "/ | ||
+ | register: __stat_file2 | ||
+ | - name: display | ||
+ | debug: | ||
+ | var: __stat_file2 | ||
+ | </ | ||
+ | |||
+ | === resultat === | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | BECOME password: | ||
+ | |||
+ | PLAY [J_Playbook_Stat_Reg] ************************************************************************************** | ||
+ | |||
+ | TASK [Gathering Facts] ****************************************************************************************** | ||
+ | Saturday 20 August 2022 10:03:05 +0200 (0: | ||
+ | ok: [node3] | ||
+ | |||
+ | TASK [create_file] ********************************************************************************************** | ||
+ | Saturday 20 August 2022 10:03:07 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | TASK [stat_file] ************************************************************************************************ | ||
+ | Saturday 20 August 2022 10:03:07 +0200 (0: | ||
+ | ok: [node3] | ||
+ | |||
+ | TASK [display] ************************************************************************************************** | ||
+ | Saturday 20 August 2022 10:03:07 +0200 (0: | ||
+ | ok: [node3] => { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | PLAY RECAP ****************************************************************************************************** | ||
+ | node3 : ok=4 changed=1 | ||
+ | |||
+ | Saturday 20 August 2022 10:03:07 +0200 (0: | ||
+ | =============================================================================== | ||
+ | Gathering Facts ------------------------------------------------------------------------------------------ 1.24s | ||
+ | stat_file ------------------------------------------------------------------------------------------------ 0.28s | ||
+ | create_file ---------------------------------------------------------------------------------------------- 0.28s | ||
+ | display -------------------------------------------------------------------------------------------------- 0.04s | ||
+ | </ | ||
+ | |||
+ | === stat specifique === | ||
+ | |||
+ | plus specifiquement si on veux filtrer uniquement sur le retour de stat d' | ||
+ | |||
+ | < | ||
+ | - name: display | ||
+ | debug: | ||
+ | msg: " | ||
+ | </ | ||
+ | |||
+ | === resultat === | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | TASK [display] ************************************************************************************************** | ||
+ | Saturday 20 August 2022 10:18:19 +0200 (0: | ||
+ | ok: [node3] => { | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== condition when ==== | ||
+ | |||
+ | avec cette condition d' | ||
+ | |||
+ | < | ||
+ | - name: creation conditionnelle du subDir | ||
+ | file: | ||
+ | path: / | ||
+ | state: directory | ||
+ | when: __stat_file2.stat.exists == True | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | TASK [display] ************************************************************************************************** | ||
+ | Saturday 20 August 2022 10:27:42 +0200 (0: | ||
+ | ok: [node3] => { | ||
+ | " | ||
+ | } | ||
+ | |||
+ | TASK [creation conditionnelle du subDir] ************************************************************************ | ||
+ | Saturday 20 August 2022 10:27:42 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | PLAY RECAP ****************************************************************************************************** | ||
+ | node3 : ok=5 changed=2 | ||
+ | </ | ||
+ | |||
+ | ==== ansible boucle ==== | ||
+ | |||
+ | * https:// | ||
+ | |||
+ | la plus classique avec with_items qui est une liste a base de dictionnaire | ||
+ | |||
+ | === playbook === | ||
+ | |||
+ | creation de 3 repertoires | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | --- | ||
+ | - name: J_Playbook_File_Dir | ||
+ | hosts: node3 | ||
+ | become: yes | ||
+ | tasks: | ||
+ | - name: create_x_dir | ||
+ | file: | ||
+ | path: "/ | ||
+ | state: directory | ||
+ | recurse: yes | ||
+ | owner: root | ||
+ | with_items: | ||
+ | - ansdirA | ||
+ | - ansdirB | ||
+ | - ansdirC | ||
+ | </ | ||
+ | |||
+ | === execution === | ||
+ | |||
+ | < | ||
+ | TASK [create_x_dir] ********************************************************************************************* | ||
+ | Saturday 20 August 2022 10:45:11 +0200 (0: | ||
+ | changed: [node3] => (item=ansdirA) | ||
+ | changed: [node3] => (item=ansdirB) | ||
+ | changed: [node3] => (item=ansdirC) | ||
+ | </ | ||
+ | |||
+ | === dictonnaire de valeur === | ||
+ | |||
+ | on peut aussi utiliser les items sous forme de dictionnaire de valeur | ||
+ | |||
+ | < | ||
+ | tasks: | ||
+ | - name: create_x_dir | ||
+ | file: | ||
+ | path: "/ | ||
+ | state: directory | ||
+ | recurse: yes | ||
+ | owner: root | ||
+ | with_items: | ||
+ | - { dir: ansdirA, fichier: " | ||
+ | - { dir: ansdirB, fichier: " | ||
+ | - { dir: ansdirC, fichier: " | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | TASK [create_x_dir] ********************************************************************************************* | ||
+ | Saturday 20 August 2022 10:52:27 +0200 (0: | ||
+ | changed: [node3] => (item={' | ||
+ | changed: [node3] => (item={' | ||
+ | changed: [node3] => (item={' | ||
+ | |||
+ | PLAY RECAP ****************************************************************************************************** | ||
+ | node3 : ok=2 changed=1 | ||
+ | </ | ||
+ | |||
+ | ==== dictionnaire dans group_vars ==== | ||
+ | |||
+ | d'un point de vue organisation, | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | mydict: | ||
+ | - { dir: ansdirA, fichier: " | ||
+ | - { dir: ansdirB, fichier: " | ||
+ | - { dir: ansdirC, fichier: " | ||
+ | </ | ||
+ | |||
+ | avec dans le playbook un appel a ce dictionnaire | ||
+ | |||
+ | < | ||
+ | with_items: | ||
+ | {{ mydict }} | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== ansible module apt ==== | ||
+ | |||
+ | * https:// | ||
+ | |||
+ | installer un paquet , plein d' | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | --- | ||
+ | - name: J_Playbook_Apt | ||
+ | hosts: node3 | ||
+ | become: yes | ||
+ | tasks: | ||
+ | - name: gestion_apt | ||
+ | apt: | ||
+ | name: tree | ||
+ | state: latest | ||
+ | update_cache: | ||
+ | cache_valid_time: | ||
+ | </ | ||
+ | |||
+ | le //state: present// est moins risqué en terme d' | ||
+ | |||
+ | === execution === | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | ... | ||
+ | TASK [gestion_apt] ********************************************************************************************** | ||
+ | Saturday 20 August 2022 11:29:43 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | PLAY RECAP ****************************************************************************************************** | ||
+ | node3 : ok=2 changed=1 | ||
+ | |||
+ | Saturday 20 August 2022 11:29:58 +0200 (0: | ||
+ | =============================================================================== | ||
+ | gestion_apt --------------------------------------------------------------------------------------------- 15.74s | ||
+ | Gathering Facts ------------------------------------------------------------------------------------------ 1.39s | ||
+ | </ | ||
+ | |||
+ | |||
+ | === supression === | ||
+ | |||
+ | supression totale | ||
+ | |||
+ | < | ||
+ | - name: gestion_apt | ||
+ | apt: | ||
+ | name: tree | ||
+ | state: absent | ||
+ | purge: yes | ||
+ | autoremove: yes | ||
+ | </ | ||
+ | |||
+ | ==== ansible module reboot ==== | ||
+ | |||
+ | * https:// | ||
+ | |||
+ | on demande un reboot sur la base de la presence d'un fichier | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | --- | ||
+ | - name: J_Playbook_File_Reboot | ||
+ | hosts: node3 | ||
+ | become: yes | ||
+ | tasks: | ||
+ | - name: create_fileR | ||
+ | file: | ||
+ | path: "/ | ||
+ | state: touch | ||
+ | - name: stat_fileR | ||
+ | stat: | ||
+ | path: "/ | ||
+ | register: __stat_fileR | ||
+ | |||
+ | - name: reboot_node | ||
+ | reboot: | ||
+ | msg: " | ||
+ | connect_timeout: | ||
+ | reboot_timeout: | ||
+ | pre_reboot_delay: | ||
+ | post_reboot_delay: | ||
+ | test_command: | ||
+ | when: __stat_fileR.stat.exists | ||
+ | |||
+ | - name: reboot_ok | ||
+ | file: | ||
+ | path: "/ | ||
+ | state: touch | ||
+ | </ | ||
+ | |||
+ | === execution === | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | BECOME password: | ||
+ | |||
+ | PLAY [J_Playbook_File_Reboot] *********************************************************************** | ||
+ | |||
+ | TASK [Gathering Facts] ****************************************************************************** | ||
+ | Saturday 20 August 2022 11:54:11 +0200 (0: | ||
+ | ok: [node3] | ||
+ | |||
+ | TASK [create_fileR] ********************************************************************************* | ||
+ | Saturday 20 August 2022 11:54:12 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | TASK [stat_fileR] *********************************************************************************** | ||
+ | Saturday 20 August 2022 11:54:12 +0200 (0: | ||
+ | ok: [node3] | ||
+ | |||
+ | TASK [reboot_node] ********************************************************************************** | ||
+ | Saturday 20 August 2022 11:54:12 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | TASK [reboot_ok] ************************************************************************************ | ||
+ | Saturday 20 August 2022 11:55:25 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | PLAY RECAP ****************************************************************************************** | ||
+ | node3 : ok=5 changed=3 | ||
+ | |||
+ | Saturday 20 August 2022 11:55:26 +0200 (0: | ||
+ | =============================================================================== | ||
+ | reboot_node --------------------------------------------------------------------------------- 73.09s | ||
+ | reboot_ok ------------------------------------------------------------------------------------ 0.86s | ||
+ | Gathering Facts ------------------------------------------------------------------------------ 0.86s | ||
+ | stat_fileR ----------------------------------------------------------------------------------- 0.30s | ||
+ | create_fileR --------------------------------------------------------------------------------- 0.29s | ||
+ | </ | ||
+ | |||
+ | ==== ansible module ssh ==== | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | genérer une clée ssh et la deployer | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | --- | ||
+ | - name: J_Playbook_sshKey | ||
+ | hosts: node3 | ||
+ | become: yes | ||
+ | tasks: | ||
+ | - name: create_sshKey | ||
+ | openssh_keypair: | ||
+ | path: "/ | ||
+ | type: rsa | ||
+ | size: 2048 | ||
+ | state: present | ||
+ | force: no | ||
+ | #delegate a localhost pour jouer ça sur notre server-node | ||
+ | delegate_to: | ||
+ | #le faire tourner une seule fois , meme si +sieurs hosts | ||
+ | run_once: yes | ||
+ | </ | ||
+ | |||
+ | === execution === | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | BECOME password: | ||
+ | |||
+ | PLAY [J_Playbook_sshKey] **************************************************************************** | ||
+ | |||
+ | TASK [Gathering Facts] ****************************************************************************** | ||
+ | Saturday 20 August 2022 20:45:52 +0200 (0: | ||
+ | ok: [node3] | ||
+ | |||
+ | TASK [create_sshKey] ******************************************************************************** | ||
+ | Saturday 20 August 2022 20:45:53 +0200 (0: | ||
+ | changed: [node3 -> localhost] | ||
+ | |||
+ | PLAY RECAP ****************************************************************************************** | ||
+ | node3 : ok=2 changed=1 | ||
+ | |||
+ | Saturday 20 August 2022 20:45:53 +0200 (0: | ||
+ | =============================================================================== | ||
+ | Gathering Facts ------------------------------------------------------------------------------ 0.86s | ||
+ | create_sshKey -------------------------------------------------------------------------------- 0.21s | ||
+ | |||
+ | ans@disi-dellat: | ||
+ | -rw-r--r-- 1 root root 382 août 20 20:45 / | ||
+ | -rw------- 1 root root 1799 août 20 20:45 / | ||
+ | </ | ||
+ | |||
+ | ==== deploy ssh-key ==== | ||
+ | |||
+ | apres generation locale de la clé (pas besoin d' | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | --- | ||
+ | - name: J_Playbook_sshKey | ||
+ | hosts: node3 | ||
+ | become: yes | ||
+ | tasks: | ||
+ | - name: create_sshKey | ||
+ | openssh_keypair: | ||
+ | path: "/ | ||
+ | type: rsa | ||
+ | size: 2048 | ||
+ | state: present | ||
+ | force: no | ||
+ | #delegate a localhost pour jouer ça sur notre server-node | ||
+ | delegate_to: | ||
+ | #le faire tourner une seule fois , meme si +sieurs hosts | ||
+ | run_once: yes | ||
+ | |||
+ | - name: create_user_adma | ||
+ | user: | ||
+ | name: adma | ||
+ | shell: /bin/bash | ||
+ | groups: sudo | ||
+ | append: yes | ||
+ | password: "{{ ' | ||
+ | become: yes | ||
+ | |||
+ | - name: add_adma_sudoers | ||
+ | copy: | ||
+ | dest: "/ | ||
+ | content: "adma ALL=(ALL) NOPASSWD: ALL" | ||
+ | become: yes | ||
+ | |||
+ | - name: deploy_sshKey | ||
+ | authorized_key: | ||
+ | user: adma | ||
+ | key: "{{ lookup(' | ||
+ | state: present | ||
+ | become: yes | ||
+ | </ | ||
+ | |||
+ | === execution === | ||
+ | |||
+ | < | ||
+ | ans@disi-dellat: | ||
+ | BECOME password: | ||
+ | |||
+ | PLAY [J_Playbook_sshKey] **************************************************************************** | ||
+ | |||
+ | TASK [Gathering Facts] ****************************************************************************** | ||
+ | Sunday 21 August 2022 10:47:05 +0200 (0: | ||
+ | ok: [node3] | ||
+ | |||
+ | TASK [create_sshKey] ******************************************************************************** | ||
+ | Sunday 21 August 2022 10:47:06 +0200 (0: | ||
+ | ok: [node3 -> localhost] | ||
+ | |||
+ | TASK [create_user_adma] ***************************************************************************** | ||
+ | Sunday 21 August 2022 10:47:06 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | TASK [add_adma_sudoers] ***************************************************************************** | ||
+ | Sunday 21 August 2022 10:47:07 +0200 (0: | ||
+ | ok: [node3] | ||
+ | |||
+ | TASK [deploy_sshKey] ******************************************************************************** | ||
+ | Sunday 21 August 2022 10:47:07 +0200 (0: | ||
+ | changed: [node3] | ||
+ | |||
+ | PLAY RECAP ****************************************************************************************** | ||
+ | node3 : ok=5 changed=2 | ||
+ | |||
+ | Sunday 21 August 2022 10:47:08 +0200 (0: | ||
+ | =============================================================================== | ||
+ | Gathering Facts ------------------------------------------------------------------------------ 1.25s | ||
+ | deploy_sshKey -------------------------------------------------------------------------------- 0.66s | ||
+ | add_adma_sudoers ----------------------------------------------------------------------------- 0.62s | ||
+ | create_user_adma ----------------------------------------------------------------------------- 0.50s | ||
+ | create_sshKey -------------------------------------------------------------------------------- 0.19s | ||
</ | </ |