2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 11:48:54 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-10-28 06:49:14 +01:00
|
|
|
# Copyright: (c) 2019-2020, Andrew Klaus <andrewklaus@gmail.com>
|
2020-03-09 10:11:07 +01:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2020-10-28 06:49:14 +01:00
|
|
|
DOCUMENTATION = r'''
|
2020-03-09 10:11:07 +01:00
|
|
|
---
|
|
|
|
module: syspatch
|
|
|
|
|
|
|
|
short_description: Manage OpenBSD system patches
|
|
|
|
|
|
|
|
|
|
|
|
description:
|
2020-10-28 06:49:14 +01:00
|
|
|
- "Manage OpenBSD system patches using syspatch."
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
options:
|
|
|
|
revert:
|
|
|
|
description:
|
2020-10-28 06:49:14 +01:00
|
|
|
- Revert system patches.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
choices: [ all, one ]
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Andrew Klaus (@precurse)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Apply all available system patches
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.syspatch:
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: Revert last patch
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.syspatch:
|
2020-03-09 10:11:07 +01:00
|
|
|
revert: one
|
|
|
|
|
|
|
|
- name: Revert all patches
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.syspatch:
|
2020-03-09 10:11:07 +01:00
|
|
|
revert: all
|
|
|
|
|
|
|
|
# NOTE: You can reboot automatically if a patch requires it:
|
|
|
|
- name: Apply all patches and store result
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.syspatch:
|
2020-03-09 10:11:07 +01:00
|
|
|
register: syspatch
|
|
|
|
|
|
|
|
- name: Reboot if patch requires it
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.reboot:
|
2020-03-09 10:11:07 +01:00
|
|
|
when: syspatch.reboot_needed
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
rc:
|
|
|
|
description: The command return code (0 means success)
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
stdout:
|
2020-10-28 06:49:14 +01:00
|
|
|
description: syspatch standard output.
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "001_rip6cksum"
|
|
|
|
stderr:
|
2020-10-28 06:49:14 +01:00
|
|
|
description: syspatch standard error.
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "syspatch: need root privileges"
|
|
|
|
reboot_needed:
|
2020-10-28 06:49:14 +01:00
|
|
|
description: Whether or not a reboot is required after an update.
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: always
|
|
|
|
type: bool
|
|
|
|
sample: True
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def run_module():
|
|
|
|
# define available arguments/parameters a user can pass to the module
|
|
|
|
module_args = dict(
|
|
|
|
revert=dict(type='str', choices=['all', 'one'])
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
result = syspatch_run(module)
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
def syspatch_run(module):
|
|
|
|
cmd = module.get_bin_path('syspatch', True)
|
|
|
|
changed = False
|
|
|
|
reboot_needed = False
|
|
|
|
warnings = []
|
|
|
|
|
|
|
|
# Set safe defaults for run_flag and check_flag
|
|
|
|
run_flag = ['-c']
|
|
|
|
check_flag = ['-c']
|
|
|
|
if module.params['revert']:
|
|
|
|
check_flag = ['-l']
|
|
|
|
|
|
|
|
if module.params['revert'] == 'all':
|
|
|
|
run_flag = ['-R']
|
|
|
|
else:
|
|
|
|
run_flag = ['-r']
|
2021-03-09 21:23:20 +01:00
|
|
|
else:
|
2020-03-09 10:11:07 +01:00
|
|
|
check_flag = ['-c']
|
|
|
|
run_flag = []
|
|
|
|
|
|
|
|
# Run check command
|
|
|
|
rc, out, err = module.run_command([cmd] + check_flag)
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Command %s failed rc=%d, out=%s, err=%s" % (cmd, rc, out, err))
|
|
|
|
|
|
|
|
if len(out) > 0:
|
|
|
|
# Changes pending
|
|
|
|
change_pending = True
|
|
|
|
else:
|
|
|
|
# No changes pending
|
|
|
|
change_pending = False
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
changed = change_pending
|
|
|
|
elif change_pending:
|
|
|
|
rc, out, err = module.run_command([cmd] + run_flag)
|
|
|
|
|
|
|
|
# Workaround syspatch ln bug:
|
|
|
|
# http://openbsd-archive.7691.n7.nabble.com/Warning-applying-latest-syspatch-td354250.html
|
|
|
|
if rc != 0 and err != 'ln: /usr/X11R6/bin/X: No such file or directory\n':
|
|
|
|
module.fail_json(msg="Command %s failed rc=%d, out=%s, err=%s" % (cmd, rc, out, err))
|
2020-10-28 06:49:14 +01:00
|
|
|
elif out.lower().find('create unique kernel') >= 0:
|
2020-03-09 10:11:07 +01:00
|
|
|
# Kernel update applied
|
|
|
|
reboot_needed = True
|
2020-10-28 06:49:14 +01:00
|
|
|
elif out.lower().find('syspatch updated itself') >= 0:
|
2020-03-09 10:11:07 +01:00
|
|
|
warnings.append('Syspatch was updated. Please run syspatch again.')
|
|
|
|
|
|
|
|
# If no stdout, then warn user
|
|
|
|
if len(out) == 0:
|
|
|
|
warnings.append('syspatch had suggested changes, but stdout was empty.')
|
|
|
|
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
changed=changed,
|
|
|
|
reboot_needed=reboot_needed,
|
|
|
|
rc=rc,
|
|
|
|
stderr=err,
|
|
|
|
stdout=out,
|
|
|
|
warnings=warnings
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|