Tuesday, July 12, 2022

Playbook to upgrade and update ubuntu servers

 from time to time we need to update and upgrade our linux servers, so I think this is a real case scenario


root@dcex1510ctrl001:~# cat ansible/playbooks/updateUpgrade.yaml
- name: Ansible playbook to update and upgrade all packages
  hosts: all
  tasks:
    - name: Update apt repo and cache on the clients
      apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600
    - name: Upgrade all packages on the clients
      apt:
        upgrade: dist
        force_apt_get: yes
    - name: Check if a reboot is needed on all servers
      register: reboot_required_file
      stat:
        path: /var/run/reboot-required

    - name: Reboot the server if kernel updated
      reboot:
        msg: "Reboot initiated by Ansible for kernel updates"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
      when: reboot_required_file.stat.exists


The first task will update the packages and force the client to use apt-get instead of using aptitude

The second task will upgrade the packages 

The third task will check this file /var/run/reboot-required and if it exists in the clients, then ansible will restart this client because it requires a reboot.

The fourth task will reboot the client based on the status of the file status which we get from the third task. please note that ansible will differentiate between the status of this file for each client. so if this file exists only in the first client, so ansible will reboot the first client only and it will not reboot the second client


root@dcex1510ctrl001:~#  ansible-playbook -i /root/ansible/hosts/hosts.ini /root/ansible/playbooks/updateUpgrade.yaml

PLAY [Ansible playbook to update and upgrade all packages] ***********************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [10.11.25.67]
ok: [10.11.25.68]

TASK [Update apt repo and cache on the clients] ***********************************************************************************
changed: [10.11.25.67]
changed: [10.11.25.68]

TASK [Upgrade all packages on the clients] ***********************************************************************************
changed: [10.11.25.67]
changed: [10.11.25.68]

TASK [Check if a reboot is needed on all servers] ***********************************************************************************
ok: [10.11.25.68]
ok: [10.11.25.67]

TASK [Reboot the server if kernel updated] ***********************************************************************************
changed: [10.11.25.67]
changed: [10.11.25.68]

PLAY RECAP ***********************************************************************************
10.11.25.67       : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.11.25.68       : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0




No comments:

Post a Comment

Install Vault on ubuntu 20

 Today I will install vault on ubuntu 20 to use it as a part of my Ansible learning journey  Add GPG key curl -fsSL https://apt.releases.has...