Sunday, 4 June 2017

Ansible playbook to patch and reboot RHEL 6 and RHEL 7 servers

How to run:

If single host give host ip like below in the command else for group patch, update the /etc/ansible/hosts file with server list and give the group name as host variable value. The playbook also collects required information like mount status resolv.conf and ntpstat as per my requirement, you can add your commands in the shell section. The information collected will be stored in the name of serverip-prepatch.txt and serverip-postpatch.txt and saves it in the playbook directory.


# ansible-playbook patchreboot.yml --extra-vars "host=10.10.11.70" --user ranjith

---
- hosts: "{{ host }}"
  become: yes
  become_method: sudo
  tasks:
    - name: running prepatch info commands
      shell: |
        df -hP
        cat /etc/resolv.conf
        ntpstat
      register: prepatch
      ignore_errors: True
    - name: removing old prepatch info file
      local_action: file path={{ playbook_dir }}/{{ inventory_hostname }}-prepatch.txt state=absent
    - name: storing prepatch info
      local_action: copy content={{ prepatch.stdout }} dest={{ playbook_dir }}/{{ inventory_hostname }}-prepatch.txt
    - name: upgrade all packages
      yum:
        name: '*'
        state: latest
    - name: restart server
      command: /sbin/reboot
      async: 0
      poll: 0
      ignore_errors: true
    - name: Pause for 180 seconds
      pause: minutes=3
    - name: wait for the server to restart
      local_action: wait_for host={{ inventory_hostname }}
                    port=22
                    delay=15
                    timeout=300
                    state=started
                    connect_timeout=15
    - name: restarting ntpserver
      service:
        name: ntpd
        state: restarted
        enabled: yes
    - name: running postpatch info commands
      shell: |
        df -hP
        cat /etc/resolv.conf
        sleep 10
        ntpstat
      register: postpatch
      ignore_errors: True
    - name: removing old postpatch info file
      local_action: file path={{ playbook_dir }}/{{ inventory_hostname }}-postpatch.txt state=absent
    - name: storing postpatch info
      local_action: copy content={{ postpatch.stdout }} dest={{ playbook_dir }}/{{ inventory_hostname }}-postpatch.txt

5 comments: