Feature Requests
Deferred

Patch Management using Ansible

Utilizing Ansible, we can introduce patch management.

6 Comments

Write a comment...

Posting anonymously

JP
Josh Preston
Josh Preston·1 month ago

I've got a fork going for the ansible collection that allows you to target groups and other things (like agent reporting), auto-registration (you need keys).

https://github.com/mrjoshuap/PatchMon-ansible

I have created PatchMon groups for "Automated", "Manual", and "No Reboot" group, but you can use whatever slicing criteria you'd like. When combined with the dynamic inventory it allows for a large variation in workflows.

For instance, my playbook to patch systems looks like:

---
# Apply pending package updates on auto-patchable hosts that PatchMon reports
# as security_required. Targets Automated and No_Reboot (union). Gates on
# hostvar patchmon_update_state instead of the patchmon_security_required
# inventory group, which only exists when at least one host matches. Manual
# is excluded via group_names. After upgrades, runs a PatchMon agent report.
# Does not reboot — use PatchMon - Reboot Needed. Optional AAP Limit narrows further.
- name: Apply security updates for PatchMon security_required hosts
  hosts: Automated:No_Reboot:!status_stopped:!unmanaged_guests:!network:!localhost
  gather_facts: true
  become: true

  tasks:
    - name: Apply apt upgrades
      ansible.builtin.apt:
        update_cache: true
        upgrade: safe
        force_apt_get: true
      when:
        - >-
          (patchmon_update_state | default('')) == 'security_required'
          or (patchmon_security_updates_count | default(0) | int > 0)
        - "'Manual' not in group_names"
        - ansible_pkg_mgr == 'apt'

    - name: Apply dnf upgrades  # noqa: package-latest
      ansible.builtin.dnf:
        name: "*"
        state: latest
        update_cache: true
      when:
        - >-
          (patchmon_update_state | default('')) == 'security_required'
          or (patchmon_security_updates_count | default(0) | int > 0)
        - "'Manual' not in group_names"
        - ansible_pkg_mgr in ['dnf', 'yum']

    - name: Fail on unsupported package manager
      ansible.builtin.fail:
        msg: >-
          Unsupported package manager {{ ansible_pkg_mgr }} on
          {{ inventory_hostname }}; expected apt, dnf, or yum.
      when:
        - >-
          (patchmon_update_state | default('')) == 'security_required'
          or (patchmon_security_updates_count | default(0) | int > 0)
        - "'Manual' not in group_names"
        - ansible_pkg_mgr not in ['apt', 'dnf', 'yum']

    - name: Report updated package state to PatchMon
      ansible.builtin.include_role:
        name: patchmon.core.agent
      vars:
        agent_state: report
      when:
        - >-
          (patchmon_update_state | default('')) == 'security_required'
          or (patchmon_security_updates_count | default(0) | int > 0)
        - "'Manual' not in group_names"

Then, separately I run the reboots:

---
# Reboot Automated hosts that PatchMon reports as needing a reboot.
# Uses hostvar patchmon_needs_reboot (always set with include_stats) instead of
# the patchmon_needs_reboot group, which only exists when at least one host
# currently needs a reboot. Manual / No_Reboot are excluded via group_names so
# missing empty groups do not break host matching. Optional AAP Limit narrows further.
- name: Reboot hosts that PatchMon reports need a reboot
  hosts: Automated:!status_stopped:!unmanaged_guests:!network:!localhost
  gather_facts: false
  become: true
  serial: 1

  tasks:
    - name: Reboot host when PatchMon reports needs reboot
      ansible.builtin.reboot:
        reboot_timeout: 600
      when:
        - patchmon_needs_reboot | default(false) | bool
        - "'Manual' not in group_names"
        - "'No_Reboot' not in group_names"

I run them on a schedule by application stacks to control the reboots.

9
9technologygroup·7 months ago

Patch Management feature has now been introduced as part of V2.0.0

G
gompstar·10 months ago

Ansible is a great tool but, it has more overhead. What about something simple like unattended upgrades at least to start with.

https://documentation.ubuntu.com/server/how-to/software/automatic-updates/

But unattended updates are done by the OS, and can't be scheduled through a third party. If I only want updates on a specific date and time, there must be something triggering it, for example ansible. Almost all business have a fixed day and time for updates. So doing auto updates is not very handy.
Also other OS'ses don't have this option by default, which makes it not very useful for patch management as well.

other thing: updates separated by groups would be wonderful, so test can be deployed on let's say Tuesday, production on Saturday.

One thing that is important, if you'd just use cron, it could be an issue, because if you'd set for example 'the 3rd Tuesday test' and 'the 3rd Saturday prod', it could trigger prod first if the month starts on a Saturday.

J
jschaufuss·1 year ago

It would be perfect to use custom ansible scripts for like specific host groups

Z
zero77·1 year ago

Ansible is a great tool but, it has more overhead.
What about something simple like unattended upgrades at least to start with.

https://documentation.ubuntu.com/server/how-to/software/automatic-updates/

D
dennis-vanstrien·1 year ago

Please ad this feature, this is the whole reason for me to use this application