添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

On the web you can find many tips about how to update CentOS/RHEL linux systems. Some of them are quite old and do not leverage new features available in recent Ansible versions, other has some issues or do not provide a nice way to display what’s going on.

Recently I’ve spent some time tuning ansible playbook to develop a nice way to update my RedHat family systems.

The playbook does the following:

  • First, it checks if there are any packages to be updated and displays them.
  • Next, it starts the update.
  • After that it installs (if necessary) yum-utils package that provides needs-restarting command which tells us if the system reboot is required after the update.
  • Then it reboots host if necessary and wait for it to come back online.
  • At the end it displays a message with number of seconds that it took to reboot.
  • Below you will find tasks from the playbook (I do not post whole playbook as you can have your own requirements e.g. for serialization or host group):

    - name: check packages for updates
      shell: yum list updates | awk 'f;/Updated Packages/{f=1;}' | awk '{ print $1 }'
      changed_when: updates.stdout_lines | length > 0
      args:
        warn: false
      register: updates
    - name: display count
      debug:
        msg: "Found {{ updates.stdout_lines | length }} packages to be updated:\n\n{{ updates.stdout }}"
    - when: updates.stdout_lines | length > 0
      block:
        - name: install updates using yum
            name: "*"
            state: latest
        - name: install yum-utils
          package:
            name: yum-utils
        - name: check if reboot is required
          shell: needs-restarting -r
          failed_when: false
          register: reboot_required
          changed_when: false
    - when: updates.stdout_lines | length > 0 and reboot_required.rc != 0
      block:
        - name: reboot the server if required
          shell: sleep 3; reboot
          ignore_errors: true
          changed_when: false
          async: 1
          poll: 0
        - name: wait for server to come back after reboot
          wait_for_connection:
            timeout: 600
            delay: 20
          register: reboot_result
        - name: reboot time
          debug:
            msg: "The system rebooted in {{ reboot_result.elapsed }} seconds."
    

    I hope you find it useful :)

    Very useful! Thanks a lot!

    I have been searching for an elegant and simple solution for this as Red Hat is failing to solve this properly.

    On Debian systems it is quit easy to establish if a server needs a reboot but on Red Hat servers it is very difficult to determine if they need a reboot.

    there is a reboot module you should look into – it can reboot a machine more nicely than the last stanza on this page.
    https://docs.ansible.com/ansible/latest/modules/reboot_module.html

    luktom,

    Would you be able to automate this for me? I can pay you.
    : https://access.redhat.com/documentation/en-us/red_hat_gluster_storage/3.4/html/installation_guide/rhel6_to_rhel7

    Regards

    Thanks,

    On the shell task I always add this to be sure my locale system is always in english to parse the command output.

    environment:
    LC_ALL: C

    “args
    warn: false”
    for “changed_when” has been deprecated now when i use it in a playbook.
    what was it set false to warn for in the first place?
    never experienced this to warn about anything when i tried to remove it, and now it only works without this arg.

    Leave a Comment

    Click here to cancel reply