Ansible playbook - update and reboot machine

Posted on

Hi,

I decided to create scheduled job based on Ansible playbook which will automatically:
1. Clean DNF cache
2. Install missing errata's
3. Reboot server if needed.

Below my simple script:

  • hosts: all
    gather_facts: True
    tasks:

    • name: Clean cache dnf
      ansible.builtin.command:
      cmd: dnf clean all
      ignore_errors: yes

    • name: Install missing erratas
      ansible.builtin.dnf:
      name: '*'
      state: latest

    • name: Check for pending reboot
      ansible.builtin.command:
      cmd: needs-restarting -r
      register: reboot_required
      failed_when: reboot_required.rc not in [0, 1]

    • name: Reboot server if needed
      ansible.builtin.reboot:
      reboot_timeout: 600
      when: reboot_required.rc == 1

And now when I push that script with "Schelude remote job" I getting two issues:
1. Run first task (Clean DNF cache) then run second task (Install missing errata's) but... not installing missing erratas, never proceed third task (Reboot if needed).

  1. It also repeat tasks execution few times.

Console dump as below.
[WARNING]: Callback disabled by environment. Disabling the Foreman callback plugin.

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [server_name.domain.org]

TASK [Clean cache dnf] *********************************************************
changed: [server_name.domain.org]

TASK [Install missing erratas] *************************************************
Exit status: 0

[WARNING]: Callback disabled by environment. Disabling the Foreman callback plugin.

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [server_name.domain.org]

TASK [Clean cache dnf] *********************************************************
changed: [server_name.domain.org]

TASK [Install missing erratas] *************************************************
Exit status: 0

TASK [Install missing erratas] *************************************************
Timeout for execution passed, stopping the job
Exit status: 0

If you met with similar issue - let me know how you fix that. Really appreciate for any help.

Responses