-
Notifications
You must be signed in to change notification settings - Fork 7
/
install_patches.py
169 lines (147 loc) · 5.26 KB
/
install_patches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/python
"""
Ansible Module for installing patches on a managed host
2022 Christian Stankowic
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: install_patches
short_description: Install patches
description:
- Install patches on a managed host
author:
- "Christian Stankowic (@stdevel)"
extends_documentation_fragment:
- stdevel.uyuni.uyuni_auth
options:
name:
description: Name or profile ID of the managed host
required: True
type: str
include_patches:
description: List of patch names or IDs to install
type: list
elements: str
exclude_patches:
description: List of patch names or IDs to exclude from installation
type: list
elements: str
'''
EXAMPLES = '''
- name: Install patches
stdevel.uyuni.install_patches:
uyuni_host: 192.168.1.1
uyuni_user: admin
uyuni_password: admin
name: server.localdomain.loc
exclude_patches:
- openSUSE-2022-10013
- openSUSE-SLE-15.3-2022-2118
'''
RETURN = '''
entity:
description: State whether patch installation was scheduled successfully
returned: success
type: bool
'''
from ansible.module_utils.basic import AnsibleModule
from ..module_utils.exceptions import EmptySetException, SSLCertVerificationError
from ..module_utils.helper_functions import _configure_connection, get_host_id, get_patch_id, patch_already_installed
def _install_patches(module, api_instance):
"""
Installs patches on the host
"""
# get parameters
host = get_host_id(module.params.get('name'), api_instance)
try:
include_patches = [get_patch_id(x, api_instance)["id"] for x in module.params.get('include_patches')]
except (UnboundLocalError, TypeError):
include_patches = None
except EmptySetException:
module.fail_json(msg="Patch not found or applicable")
try:
exclude_patches = [get_patch_id(x, api_instance)["id"] for x in module.params.get('exclude_patches')]
except (UnboundLocalError, TypeError):
exclude_patches = None
except EmptySetException:
module.fail_json(msg="Patch not found or applicable")
try:
# get _all_ the patches
all_patches = api_instance.get_host_patches(host)
# exclude or include patches if defined
if exclude_patches:
patches = [x["id"] for x in all_patches if x["id"] not in exclude_patches]
elif include_patches:
patches = [x["id"] for x in all_patches if x["id"] in include_patches]
else:
patches = [x["id"] for x in all_patches]
# install patches
action_id = api_instance.install_patches(
get_host_id(
module.params.get('name'),
api_instance
),
patches
)
module.exit_json(changed=True, action_id=action_id)
except EmptySetException:
# check if already installed
if patch_already_installed(
get_host_id(
module.params.get('name'),
api_instance
),
patches,
api_instance
):
module.exit_json(changed=False)
module.fail_json(msg="Patch(es) not found or applicable")
except SSLCertVerificationError:
module.fail_json(msg="Failed to verify SSL certificate")
def main():
"""
Main function
"""
argument_spec = dict(
uyuni_host=dict(required=True),
uyuni_user=dict(required=True),
uyuni_password=dict(required=True, no_log=True),
uyuni_port=dict(default=443, type='int'),
uyuni_verify_ssl=dict(default=True, type='bool'),
name=dict(required=True),
include_patches=dict(type='list', elements='str', required=False),
exclude_patches=dict(type='list', elements='str', required=False)
)
module = AnsibleModule(
argument_spec=argument_spec,
mutually_exclusive=[('include_patches', 'exclude_patches')],
supports_check_mode=False
)
module_params = dict(
host=module.params.get('uyuni_host'),
username=module.params.get('uyuni_user'),
password=module.params.get('uyuni_password'),
port=module.params.get('uyuni_port'),
verify_ssl=module.params.get('uyuni_verify_ssl'),
include_patches=module.params.get('include_patches'),
exclude_patches=module.params.get('exclude_patches')
)
api_instance = _configure_connection(module_params)
_install_patches(module, api_instance)
if __name__ == '__main__':
main()