Ansible detect changes in certain files after `yum update` and do a reboot

StackOverflow https://stackoverflow.com/questions/22597424

  •  19-06-2023
  •  | 
  •  

Question

While using ansible-playbook to manage VPS, I have task to run yum update using yum module. The thing is, the VPS are initially CentOS 6.2 and it will upgrade to CentOS 6.5 (which is what I want), and then I want to do a reboot after this because there are some big changes, and I want that happen only after some big chagnes because I don't want to reboot every time there is some unimportant package updates.

In ansible, is it possible to detect such big changes, e.g., /etc/redhat-release will be changed by yum update, and then reboot if the big changes are found.

Thanks.

Était-ce utile?

La solution

Yes you can try something like this:

---
- name: Reboot Server Playbook
  hosts: all
  user: ambot
  sudo: True


  tasks:

    - name: Do an upgrade
      command: yum upgrade -y

    - name: Check what the new version is
      shell:  lsb_release -r | awk '{print $2}'
      register: new_release

    - name: Reboot
      command: /sbin/reboot
      when: ansible_distribution_version != new_release.stdout

The above will reboot the server if the CentOS release has changed. It's also using the ansible_distribution_version from the ansible facts variables populated initially the first time your playbook runs. Add -y in yum upgrade command, so process is not stuck waiting for confirmation in stdin from ansible. To see the ansible facts on your localhost you can run something like:

ansible localhost -m setup

[Possible solution to regenerate facts in a playbook]

 # You can try this to store the initial version
 vars:
    current_os_version: $ansible_distribution_version


 tasks:

    - name: Regenerate facts ?
      setup: filter=*
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top