Se utilizó el módulo replace para reemplazar un texto en el archivo /etc/default/grub y el módulo update-grup para actualizar los cambios.
Generar un archivo disable_ipv6.yml con el siguiente código:
---
- hosts: all
remote_user: admin
become: yes
tasks:
- name: 'Check if grub is present'
stat:
path=/etc/default/grub
register: grub_file
- name: 'Disable IPv6 - GRUB_CMD_LINE_LINUX'
replace:
path: /etc/default/grub
regexp: '^GRUB_CMDLINE_LINUX="((:?(?!ipv6\.disable=1).)*?)"$'
replace: 'GRUB_CMDLINE_LINUX="\1 ipv6.disable=1"'
when: grub_file.stat.exists
register: updateGrub
- name: 'Disable IPv6 - GRUB_CMDLINE_LINUX_DEFAULT'
replace:
path: /etc/default/grub
regexp: '^GRUB_CMDLINE_LINUX_DEFAULT="((:?(?!ipv6\.disable=1).)*?)"$'
replace: 'GRUB_CMDLINE_LINUX_DEFAULT="\1 ipv6.disable=1"'
register: updateGrub
when: grub_file.stat.exists
- name: 'update-grub'
shell: update-grub
when: updateGrub is defined
El script se ejecuta de la siguiente manera:
pablo@ansible:~$ ansible-playbook disable_ipv6.yml -K
SUDO password:
PLAY [192.168.0.100] *******************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [192.168.0.100]
TASK [Check if grub is present] ******************************************************************************************************************************************
ok: [192.168.0.100]
TASK [Disable IPv6 - GRUB_CMD_LINE_LINUX] **********************************************************************************************************************************************
changed: [192.168.0.100]
TASK [Disable IPv6 - GRUB_CMDLINE_LINUX_DEFAULT] **************************************************************************************************************************************
changed: [192.168.0.100]
TASK [update-grub] *******************************************************************************************************************************************************
changed: [192.168.0.100]
PLAY RECAP ***************************************************************************************************************************************************************
192.168.0.100 : ok=5 changed=3 unreachable=0 failed=0
pablo@ansible:~$