3.2. Script - Actualizar un Servidor de Linux/Debian mediante Ansible

To update a server (or server group) add this to a yml script file.

 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
     ---
     - hosts: linuxservers proxmoxservers
     remote_user: ansible
     become: yes
     tasks:
             - name: 'update'
             apt:
                     update_cache: yes
             - name: 'Install aptitude'
             apt:
                     name: aptitude
                     update_cache: yes
     #       register: OUT
     #    - name: Debug OUT
     #     debug: msg={{ OUT.stdout_lines}}
             - name: 'upgrade'
             apt:
                     name: "*"
                     state: latest
     #       register: OUT
     #    - name: Debug OUT
     #     debug: msg={{ OUT.stdout_lines}}
             - name: 'dist-upgrade'
             apt:
                     upgrade: dist
     #       register: OUT
     #    - name: Debug OUT
     #     debug: msg={{ OUT.stdout_lines}}
             - name: 'autoremove'
             apt:
                     autoremove: yes
     #       register: OUT
     #    - name: Debug OUT
     #     debug: msg={{ OUT.stdout_lines}}
             - name: 'autoclean'
             apt:
                     autoclean: yes
                     upgrade: dist
     #       register: OUT
     #    - name: Debug OUT
     #     debug: msg={{ OUT.stdout_lines}}
             - name: 'check if reboot needed'
             stat: path=/var/run/reboot-required
             register: reboot_required_file
             - name: Rebooting machine
             shell: sleep 2 && shutdown -r now "Ansible updates triggered"
             async: 1
             poll: 0
             ignore_errors: true
             when: reboot_required_file.stat.exists
             - name: Waiting for the machine to come back
             local_action: wait_for host={{ ansible_host }} state=started port=22 delay=30
             become: no
             when: reboot_required_file.stat.exists

Y ejecútelo en la consola

$ ansible-playbook act.yml -K

Listo!