| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_software_update |
| 13 |
short_description: Manage the software update settings of a BIG-IP |
| 14 |
description: |
| 15 |
- Manage the software update settings of a BIG-IP. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
auto_check: |
| 19 |
description: |
| 20 |
- Specifies whether to automatically check for updates on the F5 |
| 21 |
Networks downloads server. |
| 22 |
type: bool |
| 23 |
auto_phone_home: |
| 24 |
description: |
| 25 |
- Specifies whether to automatically send phone home data to the |
| 26 |
F5 Networks PhoneHome server. |
| 27 |
type: bool |
| 28 |
frequency: |
| 29 |
description: |
| 30 |
- Specifies the schedule for the automatic update check. |
| 31 |
type: str |
| 32 |
choices: |
| 33 |
- daily |
| 34 |
- monthly |
| 35 |
- weekly |
| 36 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 37 |
author: |
| 38 |
- Tim Rupp (@caphrim007) |
| 39 |
- Wojciech Wypior (@wojtek0806) |
| 40 |
''' |
| 41 |
|
| 42 |
EXAMPLES = r''' |
| 43 |
- name: Enable automatic update checking |
| 44 |
bigip_software_update: |
| 45 |
auto_check: true |
| 46 |
provider: |
| 47 |
password: secret |
| 48 |
server: lb.mydomain.com |
| 49 |
user: admin |
| 50 |
delegate_to: localhost |
| 51 |
|
| 52 |
- name: Disable automatic update checking and phoning home |
| 53 |
bigip_software_update: |
| 54 |
auto_check: false |
| 55 |
auto_phone_home: false |
| 56 |
provider: |
| 57 |
password: secret |
| 58 |
server: lb.mydomain.com |
| 59 |
user: admin |
| 60 |
delegate_to: localhost |
| 61 |
''' |
| 62 |
|
| 63 |
RETURN = r''' |
| 64 |
auto_check: |
| 65 |
description: Whether the system automatically checks for updates. |
| 66 |
returned: changed |
| 67 |
type: bool |
| 68 |
sample: true |
| 69 |
auto_phone_home: |
| 70 |
description: Whether the system automatically sends phone home data. |
| 71 |
returned: changed |
| 72 |
type: bool |
| 73 |
sample: true |
| 74 |
frequency: |
| 75 |
description: Frequency of auto update checks. |
| 76 |
returned: changed |
| 77 |
type: str |
| 78 |
sample: weekly |
| 79 |
''' |
| 80 |
from datetime import datetime |
| 81 |
|
| 82 |
from ansible.module_utils.basic import AnsibleModule |
| 83 |
|
| 84 |
from ..module_utils.bigip import F5RestClient |
| 85 |
from ..module_utils.common import ( |
| 86 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec |
| 87 |
) |
| 88 |
from ..module_utils.icontrol import tmos_version |
| 89 |
from ..module_utils.teem import send_teem |
| 90 |
|
| 91 |
|
| 92 |
class Parameters(AnsibleF5Parameters): |
| 93 |
api_map = { |
| 94 |
'autoCheck': 'auto_check', |
| 95 |
'autoPhonehome': 'auto_phone_home' |
| 96 |
} |
| 97 |
|
| 98 |
api_attributes = [ |
| 99 |
'autoCheck', 'autoPhonehome', 'frequency', |
| 100 |
] |
| 101 |
|
| 102 |
updatables = [ |
| 103 |
'auto_check', 'auto_phone_home', 'frequency', |
| 104 |
] |
| 105 |
|
| 106 |
returnables = [ |
| 107 |
'auto_check', 'auto_phone_home', 'frequency', |
| 108 |
] |
| 109 |
|
| 110 |
|
| 111 |
class ApiParameters(Parameters): |
| 112 |
@property |
| 113 |
def auto_check(self): |
| 114 |
if self._values['auto_check'] is None: |
| 115 |
return None |
| 116 |
return self._values['auto_check'] |
| 117 |
|
| 118 |
|
| 119 |
class ModuleParameters(Parameters): |
| 120 |
@property |
| 121 |
def auto_check(self): |
| 122 |
if self._values['auto_check'] is None: |
| 123 |
return None |
| 124 |
elif self._values['auto_check'] is True: |
| 125 |
return 'enabled' |
| 126 |
else: |
| 127 |
return 'disabled' |
| 128 |
|
| 129 |
@property |
| 130 |
def auto_phone_home(self): |
| 131 |
if self._values['auto_phone_home'] is None: |
| 132 |
return None |
| 133 |
elif self._values['auto_phone_home'] is True: |
| 134 |
return 'enabled' |
| 135 |
else: |
| 136 |
return 'disabled' |
| 137 |
|
| 138 |
|
| 139 |
class Changes(Parameters): |
| 140 |
def to_return(self): |
| 141 |
result = {} |
| 142 |
try: |
| 143 |
for returnable in self.returnables: |
| 144 |
result[returnable] = getattr(self, returnable) |
| 145 |
result = self._filter_params(result) |
| 146 |
except Exception: |
| 147 |
pass |
| 148 |
return result |
| 149 |
|
| 150 |
|
| 151 |
class UsableChanges(Changes): |
| 152 |
pass |
| 153 |
|
| 154 |
|
| 155 |
class ReportableChanges(Changes): |
| 156 |
@property |
| 157 |
def auto_check(self): |
| 158 |
if self._values['auto_check'] == 'enabled': |
| 159 |
return True |
| 160 |
elif self._values['auto_check'] == 'disabled': |
| 161 |
return False |
| 162 |
|
| 163 |
@property |
| 164 |
def auto_phone_home(self): |
| 165 |
if self._values['auto_phone_home'] == 'enabled': |
| 166 |
return True |
| 167 |
elif self._values['auto_phone_home'] == 'disabled': |
| 168 |
return False |
| 169 |
|
| 170 |
|
| 171 |
class Difference(object): |
| 172 |
def __init__(self, want, have=None): |
| 173 |
self.want = want |
| 174 |
self.have = have |
| 175 |
|
| 176 |
def compare(self, param): |
| 177 |
try: |
| 178 |
result = getattr(self, param) |
| 179 |
return result |
| 180 |
except AttributeError: |
| 181 |
return self.__default(param) |
| 182 |
|
| 183 |
def __default(self, param): |
| 184 |
attr1 = getattr(self.want, param) |
| 185 |
try: |
| 186 |
attr2 = getattr(self.have, param) |
| 187 |
if attr1 != attr2: |
| 188 |
return attr1 |
| 189 |
except AttributeError: |
| 190 |
return attr1 |
| 191 |
|
| 192 |
|
| 193 |
class ModuleManager(object): |
| 194 |
def __init__(self, *args, **kwargs): |
| 195 |
self.module = kwargs.get('module', None) |
| 196 |
self.client = F5RestClient(**self.module.params) |
| 197 |
self.have = None |
| 198 |
self.want = ModuleParameters(params=self.module.params) |
| 199 |
self.changes = UsableChanges() |
| 200 |
|
| 201 |
def exec_module(self): |
| 202 |
start = datetime.now().isoformat() |
| 203 |
version = tmos_version(self.client) |
| 204 |
result = dict() |
| 205 |
|
| 206 |
changed = self.update() |
| 207 |
|
| 208 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 209 |
changes = reportable.to_return() |
| 210 |
result.update(**changes) |
| 211 |
result.update(dict(changed=changed)) |
| 212 |
self._announce_deprecations(result) |
| 213 |
send_teem(start, self.client, self.module, version) |
| 214 |
return result |
| 215 |
|
| 216 |
def _announce_deprecations(self, result): |
| 217 |
warnings = result.pop('__warnings', []) |
| 218 |
for warning in warnings: |
| 219 |
self.module.deprecate( |
| 220 |
msg=warning['msg'], |
| 221 |
version=warning['version'] |
| 222 |
) |
| 223 |
|
| 224 |
def _update_changed_options(self): |
| 225 |
diff = Difference(self.want, self.have) |
| 226 |
updatables = Parameters.updatables |
| 227 |
changed = dict() |
| 228 |
for k in updatables: |
| 229 |
change = diff.compare(k) |
| 230 |
if change is None: |
| 231 |
continue |
| 232 |
else: |
| 233 |
if isinstance(change, dict): |
| 234 |
changed.update(change) |
| 235 |
else: |
| 236 |
changed[k] = change |
| 237 |
if changed: |
| 238 |
self.changes = UsableChanges(params=changed) |
| 239 |
return True |
| 240 |
return False |
| 241 |
|
| 242 |
def should_update(self): |
| 243 |
result = self._update_changed_options() |
| 244 |
if result: |
| 245 |
return True |
| 246 |
return False |
| 247 |
|
| 248 |
def update(self): |
| 249 |
self.have = self.read_current_from_device() |
| 250 |
if not self.should_update(): |
| 251 |
return False |
| 252 |
if self.module.check_mode: |
| 253 |
return True |
| 254 |
self.update_on_device() |
| 255 |
return True |
| 256 |
|
| 257 |
def read_current_from_device(self): |
| 258 |
uri = "https://{0}:{1}/mgmt/tm/sys/software/update/".format( |
| 259 |
self.client.provider['server'], |
| 260 |
self.client.provider['server_port'], |
| 261 |
) |
| 262 |
resp = self.client.api.get(uri) |
| 263 |
try: |
| 264 |
response = resp.json() |
| 265 |
except ValueError as ex: |
| 266 |
raise F5ModuleError(str(ex)) |
| 267 |
|
| 268 |
if 'code' in response and response['code'] == 400: |
| 269 |
if 'message' in response: |
| 270 |
raise F5ModuleError(response['message']) |
| 271 |
else: |
| 272 |
raise F5ModuleError(resp.content) |
| 273 |
return ApiParameters(params=response) |
| 274 |
|
| 275 |
def update_on_device(self): |
| 276 |
params = self.changes.api_params() |
| 277 |
uri = "https://{0}:{1}/mgmt/tm/sys/software/update/".format( |
| 278 |
self.client.provider['server'], |
| 279 |
self.client.provider['server_port'], |
| 280 |
) |
| 281 |
resp = self.client.api.patch(uri, json=params) |
| 282 |
try: |
| 283 |
response = resp.json() |
| 284 |
except ValueError as ex: |
| 285 |
raise F5ModuleError(str(ex)) |
| 286 |
|
| 287 |
if 'code' in response and response['code'] == 400: |
| 288 |
if 'message' in response: |
| 289 |
raise F5ModuleError(response['message']) |
| 290 |
else: |
| 291 |
raise F5ModuleError(resp.content) |
| 292 |
|
| 293 |
|
| 294 |
class ArgumentSpec(object): |
| 295 |
def __init__(self): |
| 296 |
self.supports_check_mode = True |
| 297 |
argument_spec = dict( |
| 298 |
auto_check=dict( |
| 299 |
type='bool' |
| 300 |
), |
| 301 |
auto_phone_home=dict( |
| 302 |
type='bool' |
| 303 |
), |
| 304 |
frequency=dict( |
| 305 |
choices=['daily', 'monthly', 'weekly'] |
| 306 |
) |
| 307 |
) |
| 308 |
self.argument_spec = {} |
| 309 |
self.argument_spec.update(f5_argument_spec) |
| 310 |
self.argument_spec.update(argument_spec) |
| 311 |
|
| 312 |
|
| 313 |
def main(): |
| 314 |
spec = ArgumentSpec() |
| 315 |
|
| 316 |
module = AnsibleModule( |
| 317 |
argument_spec=spec.argument_spec, |
| 318 |
supports_check_mode=spec.supports_check_mode |
| 319 |
) |
| 320 |
|
| 321 |
try: |
| 322 |
mm = ModuleManager(module=module) |
| 323 |
results = mm.exec_module() |
| 324 |
module.exit_json(**results) |
| 325 |
except F5ModuleError as ex: |
| 326 |
module.fail_json(msg=str(ex)) |
| 327 |
|
| 328 |
|
| 329 |
if __name__ == '__main__': |
| 330 |
main() |