| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2019-2020, Andrew Klaus <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: syspatch |
| 11 |
|
| 12 |
short_description: Manage OpenBSD system patches |
| 13 |
|
| 14 |
|
| 15 |
description: |
| 16 |
- Manage OpenBSD system patches using syspatch. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
|
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
|
| 26 |
options: |
| 27 |
revert: |
| 28 |
description: |
| 29 |
- Revert system patches. |
| 30 |
type: str |
| 31 |
choices: [all, one] |
| 32 |
|
| 33 |
author: |
| 34 |
- Andrew Klaus (@precurse) |
| 35 |
""" |
| 36 |
|
| 37 |
EXAMPLES = r""" |
| 38 |
- name: Apply all available system patches |
| 39 |
community.general.syspatch: |
| 40 |
|
| 41 |
- name: Revert last patch |
| 42 |
community.general.syspatch: |
| 43 |
revert: one |
| 44 |
|
| 45 |
- name: Revert all patches |
| 46 |
community.general.syspatch: |
| 47 |
revert: all |
| 48 |
|
| 49 |
# NOTE: You can reboot automatically if a patch requires it: |
| 50 |
- name: Apply all patches and store result |
| 51 |
community.general.syspatch: |
| 52 |
register: syspatch |
| 53 |
|
| 54 |
- name: Reboot if patch requires it |
| 55 |
ansible.builtin.reboot: |
| 56 |
when: syspatch.reboot_needed |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = r""" |
| 60 |
reboot_needed: |
| 61 |
description: Whether or not a reboot is required after an update. |
| 62 |
returned: always |
| 63 |
type: bool |
| 64 |
sample: true |
| 65 |
""" |
| 66 |
|
| 67 |
from ansible.module_utils.basic import AnsibleModule |
| 68 |
|
| 69 |
|
| 70 |
def run_module(): |
| 71 |
# define available arguments/parameters a user can pass to the module |
| 72 |
module_args = dict(revert=dict(type="str", choices=["all", "one"])) |
| 73 |
|
| 74 |
module = AnsibleModule( |
| 75 |
argument_spec=module_args, |
| 76 |
supports_check_mode=True, |
| 77 |
) |
| 78 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 79 |
|
| 80 |
result = syspatch_run(module) |
| 81 |
|
| 82 |
module.exit_json(**result) |
| 83 |
|
| 84 |
|
| 85 |
def syspatch_run(module): |
| 86 |
cmd = module.get_bin_path("syspatch", True) |
| 87 |
changed = False |
| 88 |
reboot_needed = False |
| 89 |
|
| 90 |
# Set safe defaults for run_flag and check_flag |
| 91 |
run_flag = ["-c"] |
| 92 |
check_flag = ["-c"] |
| 93 |
if module.params["revert"]: |
| 94 |
check_flag = ["-l"] |
| 95 |
|
| 96 |
if module.params["revert"] == "all": |
| 97 |
run_flag = ["-R"] |
| 98 |
else: |
| 99 |
run_flag = ["-r"] |
| 100 |
else: |
| 101 |
check_flag = ["-c"] |
| 102 |
run_flag = [] |
| 103 |
|
| 104 |
# Run check command |
| 105 |
rc, out, err = module.run_command([cmd] + check_flag) |
| 106 |
|
| 107 |
if rc != 0: |
| 108 |
module.fail_json(msg=f"Command {cmd} failed rc={rc}, out={out}, err={err}") |
| 109 |
|
| 110 |
if len(out) > 0: |
| 111 |
# Changes pending |
| 112 |
change_pending = True |
| 113 |
else: |
| 114 |
# No changes pending |
| 115 |
change_pending = False |
| 116 |
|
| 117 |
if module.check_mode: |
| 118 |
changed = change_pending |
| 119 |
elif change_pending: |
| 120 |
rc, out, err = module.run_command([cmd] + run_flag) |
| 121 |
|
| 122 |
# Workaround syspatch ln bug: |
| 123 |
# http://openbsd-archive.7691.n7.nabble.com/Warning-applying-latest-syspatch-td354250.html |
| 124 |
if rc != 0 and err != "ln: /usr/X11R6/bin/X: No such file or directory\n": |
| 125 |
module.fail_json(msg=f"Command {cmd} failed rc={rc}, out={out}, err={err}") |
| 126 |
elif out.lower().find("create unique kernel") >= 0: |
| 127 |
# Kernel update applied |
| 128 |
reboot_needed = True |
| 129 |
elif out.lower().find("syspatch updated itself") >= 0: |
| 130 |
module.warn("Syspatch was updated. Please run syspatch again.") |
| 131 |
|
| 132 |
# If no stdout, then warn user |
| 133 |
if len(out) == 0: |
| 134 |
module.warn("syspatch had suggested changes, but stdout was empty.") |
| 135 |
|
| 136 |
changed = True |
| 137 |
else: |
| 138 |
changed = False |
| 139 |
|
| 140 |
return dict( |
| 141 |
changed=changed, |
| 142 |
reboot_needed=reboot_needed, |
| 143 |
rc=rc, |
| 144 |
stderr=err, |
| 145 |
stdout=out, |
| 146 |
) |
| 147 |
|
| 148 |
|
| 149 |
def main(): |
| 150 |
run_module() |
| 151 |
|
| 152 |
|
| 153 |
if __name__ == "__main__": |
| 154 |
main() |