| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
--- |
| 14 |
module: aci_config_rollback |
| 15 |
short_description: Provides rollback and rollback preview functionality (config:ImportP) |
| 16 |
description: |
| 17 |
- Provides rollback and rollback preview functionality for Cisco ACI fabrics. |
| 18 |
- Config Rollbacks are done using snapshots C(aci_snapshot) with the configImportP class. |
| 19 |
options: |
| 20 |
compare_export_policy: |
| 21 |
description: |
| 22 |
- The export policy that the C(compare_snapshot) is associated to. |
| 23 |
type: str |
| 24 |
compare_snapshot: |
| 25 |
description: |
| 26 |
- The name of the snapshot to compare with C(snapshot). |
| 27 |
type: str |
| 28 |
description: |
| 29 |
description: |
| 30 |
- The description for the Import Policy. |
| 31 |
type: str |
| 32 |
aliases: [ descr ] |
| 33 |
export_policy: |
| 34 |
description: |
| 35 |
- The export policy that the C(snapshot) is associated to. |
| 36 |
type: str |
| 37 |
fail_on_decrypt: |
| 38 |
description: |
| 39 |
- Determines if the APIC should fail the rollback if unable to decrypt secured data. |
| 40 |
- The APIC defaults to C(true) when unset. |
| 41 |
type: bool |
| 42 |
import_mode: |
| 43 |
description: |
| 44 |
- Determines how the import should be handled by the APIC. |
| 45 |
- The APIC defaults to C(atomic) when unset. |
| 46 |
type: str |
| 47 |
choices: [ atomic, best-effort ] |
| 48 |
import_policy: |
| 49 |
description: |
| 50 |
- The name of the Import Policy to use for config rollback. |
| 51 |
type: str |
| 52 |
import_type: |
| 53 |
description: |
| 54 |
- Determines how the current and snapshot configuration should be compared for replacement. |
| 55 |
- The APIC defaults to C(replace) when unset. |
| 56 |
type: str |
| 57 |
choices: [ merge, replace ] |
| 58 |
snapshot: |
| 59 |
description: |
| 60 |
- The name of the snapshot to rollback to, or the base snapshot to use for comparison. |
| 61 |
- The C(aci_snapshot) module can be used to query the list of available snapshots. |
| 62 |
type: str |
| 63 |
required: true |
| 64 |
state: |
| 65 |
description: |
| 66 |
- Use C(preview) for previewing the diff between two snapshots. |
| 67 |
- Use C(rollback) for reverting the configuration to a previous snapshot. |
| 68 |
type: str |
| 69 |
choices: [ preview, rollback ] |
| 70 |
default: rollback |
| 71 |
extends_documentation_fragment: |
| 72 |
- cisco.aci.aci |
| 73 |
- cisco.aci.annotation |
| 74 |
|
| 75 |
notes: |
| 76 |
- It is strongly recommended to add a pause task after creating a Snapshot. |
| 77 |
- Wait for the Snapshot to be finished before querying, comparing Snapshots or processing to Rollbacks. |
| 78 |
seealso: |
| 79 |
- module: cisco.aci.aci_config_snapshot |
| 80 |
- name: APIC Management Information Model reference |
| 81 |
description: More information about the internal APIC class B(config:ImportP). |
| 82 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 83 |
author: |
| 84 |
- Jacob McGill (@jmcgill298) |
| 85 |
- Gaspard Micol (@gmicol) |
| 86 |
""" |
| 87 |
|
| 88 |
EXAMPLES = r""" |
| 89 |
--- |
| 90 |
- name: Create a Snapshot |
| 91 |
cisco.aci.aci_config_snapshot: |
| 92 |
host: apic |
| 93 |
username: admin |
| 94 |
password: SomeSecretPassword |
| 95 |
export_policy: config_backup |
| 96 |
state: present |
| 97 |
delegate_to: localhost |
| 98 |
|
| 99 |
- name: Wait for snapshot to be finished before querying |
| 100 |
pause: |
| 101 |
seconds: 10 |
| 102 |
|
| 103 |
- name: Query Existing Snapshots |
| 104 |
cisco.aci.aci_config_snapshot: |
| 105 |
host: apic |
| 106 |
username: admin |
| 107 |
password: SomeSecretPassword |
| 108 |
export_policy: config_backup |
| 109 |
state: query |
| 110 |
delegate_to: localhost |
| 111 |
|
| 112 |
- name: Compare Snapshot Files |
| 113 |
cisco.aci.aci_config_rollback: |
| 114 |
host: apic |
| 115 |
username: admin |
| 116 |
password: SomeSecretPassword |
| 117 |
export_policy: config_backup |
| 118 |
snapshot: run-2017-08-28T06-24-01 |
| 119 |
compare_export_policy: config_backup |
| 120 |
compare_snapshot: run-2017-08-27T23-43-56 |
| 121 |
state: preview |
| 122 |
delegate_to: localhost |
| 123 |
|
| 124 |
- name: Rollback Configuration |
| 125 |
cisco.aci.aci_config_rollback: |
| 126 |
host: apic |
| 127 |
username: admin |
| 128 |
password: SomeSecretPassword |
| 129 |
import_policy: rollback_config |
| 130 |
export_policy: config_backup |
| 131 |
snapshot: run-2017-08-28T06-24-01 |
| 132 |
state: rollback |
| 133 |
delegate_to: localhost |
| 134 |
|
| 135 |
- name: Rollback Configuration |
| 136 |
cisco.aci.aci_config_rollback: |
| 137 |
host: apic |
| 138 |
username: admin |
| 139 |
password: SomeSecretPassword |
| 140 |
import_policy: rollback_config |
| 141 |
export_policy: config_backup |
| 142 |
snapshot: run-2017-08-28T06-24-01 |
| 143 |
description: Rollback 8-27 changes |
| 144 |
import_mode: atomic |
| 145 |
import_type: replace |
| 146 |
fail_on_decrypt: true |
| 147 |
state: rollback |
| 148 |
delegate_to: localhost |
| 149 |
""" |
| 150 |
|
| 151 |
RETURN = r""" |
| 152 |
preview: |
| 153 |
description: A preview between two snapshots |
| 154 |
returned: when state is preview |
| 155 |
type: str |
| 156 |
error: |
| 157 |
description: The error information as returned from the APIC |
| 158 |
returned: failure |
| 159 |
type: dict |
| 160 |
sample: |
| 161 |
{ |
| 162 |
"code": "122", |
| 163 |
"text": "unknown managed object class foo" |
| 164 |
} |
| 165 |
raw: |
| 166 |
description: The raw output returned by the APIC REST API (xml or json) |
| 167 |
returned: parse error |
| 168 |
type: str |
| 169 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 170 |
filter_string: |
| 171 |
description: The filter string used for the request |
| 172 |
returned: failure or debug |
| 173 |
type: str |
| 174 |
sample: ?rsp-prop-include=config-only |
| 175 |
method: |
| 176 |
description: The HTTP method used for the request to the APIC |
| 177 |
returned: failure or debug |
| 178 |
type: str |
| 179 |
sample: POST |
| 180 |
response: |
| 181 |
description: The HTTP response from the APIC |
| 182 |
returned: failure or debug |
| 183 |
type: str |
| 184 |
sample: OK (30 bytes) |
| 185 |
status: |
| 186 |
description: The HTTP status from the APIC |
| 187 |
returned: failure or debug |
| 188 |
type: int |
| 189 |
sample: 200 |
| 190 |
url: |
| 191 |
description: The HTTP url used for the request to the APIC |
| 192 |
returned: failure or debug |
| 193 |
type: str |
| 194 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 195 |
""" |
| 196 |
|
| 197 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 198 |
from ansible.module_utils.basic import AnsibleModule |
| 199 |
from ansible.module_utils._text import to_bytes |
| 200 |
|
| 201 |
# Optional, only used for rollback preview |
| 202 |
try: |
| 203 |
import lxml.etree |
| 204 |
from xmljson import cobra |
| 205 |
|
| 206 |
XML_TO_JSON = True |
| 207 |
except ImportError: |
| 208 |
XML_TO_JSON = False |
| 209 |
|
| 210 |
|
| 211 |
def main(): |
| 212 |
argument_spec = aci_argument_spec() |
| 213 |
argument_spec.update(aci_annotation_spec()) |
| 214 |
argument_spec.update( |
| 215 |
compare_export_policy=dict(type="str"), |
| 216 |
compare_snapshot=dict(type="str"), |
| 217 |
description=dict(type="str", aliases=["descr"]), |
| 218 |
export_policy=dict(type="str"), |
| 219 |
fail_on_decrypt=dict(type="bool"), |
| 220 |
import_mode=dict(type="str", choices=["atomic", "best-effort"]), |
| 221 |
import_policy=dict(type="str"), |
| 222 |
import_type=dict(type="str", choices=["merge", "replace"]), |
| 223 |
snapshot=dict(type="str", required=True), |
| 224 |
state=dict(type="str", default="rollback", choices=["preview", "rollback"]), |
| 225 |
) |
| 226 |
|
| 227 |
module = AnsibleModule( |
| 228 |
argument_spec=argument_spec, |
| 229 |
supports_check_mode=False, |
| 230 |
required_if=[ |
| 231 |
["state", "preview", ["compare_export_policy", "compare_snapshot"]], |
| 232 |
["state", "rollback", ["import_policy"]], |
| 233 |
], |
| 234 |
) |
| 235 |
|
| 236 |
aci = ACIModule(module) |
| 237 |
|
| 238 |
description = module.params.get("description") |
| 239 |
export_policy = module.params.get("export_policy") |
| 240 |
fail_on_decrypt = aci.boolean(module.params.get("fail_on_decrypt")) |
| 241 |
import_mode = module.params.get("import_mode") |
| 242 |
import_policy = module.params.get("import_policy") |
| 243 |
import_type = module.params.get("import_type") |
| 244 |
snapshot = module.params.get("snapshot") |
| 245 |
state = module.params.get("state") |
| 246 |
|
| 247 |
if state == "rollback": |
| 248 |
if snapshot.startswith("run-"): |
| 249 |
snapshot = snapshot.replace("run-", "", 1) |
| 250 |
|
| 251 |
if not snapshot.endswith(".tar.gz"): |
| 252 |
snapshot += ".tar.gz" |
| 253 |
|
| 254 |
filename = "ce2_{0}-{1}".format(export_policy, snapshot) |
| 255 |
|
| 256 |
aci.construct_url( |
| 257 |
root_class=dict( |
| 258 |
aci_class="configImportP", |
| 259 |
aci_rn="fabric/configimp-{0}".format(import_policy), |
| 260 |
module_object=import_policy, |
| 261 |
target_filter={"name": import_policy}, |
| 262 |
), |
| 263 |
) |
| 264 |
|
| 265 |
aci.get_existing() |
| 266 |
|
| 267 |
aci.payload( |
| 268 |
aci_class="configImportP", |
| 269 |
class_config=dict( |
| 270 |
adminSt="triggered", |
| 271 |
descr=description, |
| 272 |
failOnDecryptErrors=fail_on_decrypt, |
| 273 |
fileName=filename, |
| 274 |
importMode=import_mode, |
| 275 |
importType=import_type, |
| 276 |
name=import_policy, |
| 277 |
snapshot="yes", |
| 278 |
), |
| 279 |
) |
| 280 |
|
| 281 |
aci.get_diff(aci_class="configImportP") |
| 282 |
|
| 283 |
aci.post_config() |
| 284 |
|
| 285 |
elif state == "preview": |
| 286 |
aci.path = "mqapi2/snapshots.diff.xml" |
| 287 |
preview_params = {"path": aci.path} |
| 288 |
preview_params.update(module.params.items()) |
| 289 |
if aci.params.get("port") is not None: |
| 290 |
aci.url = "{protocol}://{host}:{port}/{path}".format_map(preview_params) |
| 291 |
else: |
| 292 |
aci.url = "{protocol}://{host}/{path}".format_map(preview_params) |
| 293 |
aci.filter_string = ( |
| 294 |
"?s1dn=uni/backupst/snapshots-[uni/fabric/configexp-{export_policy}]/snapshot-{snapshot}&" |
| 295 |
"s2dn=uni/backupst/snapshots-[uni/fabric/configexp-{compare_export_policy}]/snapshot-{compare_snapshot}" |
| 296 |
).format_map(module.params) |
| 297 |
|
| 298 |
# Generate rollback comparison |
| 299 |
get_preview(aci) |
| 300 |
|
| 301 |
aci.exit_json() |
| 302 |
|
| 303 |
|
| 304 |
def get_preview(aci): |
| 305 |
""" |
| 306 |
This function is used to generate a preview between two snapshots and add the parsed results to the aci module return data. |
| 307 |
""" |
| 308 |
uri = aci.url + aci.filter_string |
| 309 |
|
| 310 |
resp, info = aci.api_call("GET", uri, data=None, return_response=True) |
| 311 |
|
| 312 |
# Handle APIC response |
| 313 |
if info.get("status") == 200: |
| 314 |
try: |
| 315 |
xml_to_json(aci, resp.read()) |
| 316 |
except AttributeError: |
| 317 |
xml_to_json(aci, info.get("body")) |
| 318 |
else: |
| 319 |
try: |
| 320 |
# APIC error |
| 321 |
aci.response_xml(info["body"]) |
| 322 |
aci.fail_json(msg="APIC Error {code}: {text}".format_map(aci.error)) |
| 323 |
except KeyError: |
| 324 |
# Connection error |
| 325 |
aci.fail_json(msg="Connection failed for {url}. {msg}".format_map(info)) |
| 326 |
|
| 327 |
|
| 328 |
def xml_to_json(aci, response_data): |
| 329 |
""" |
| 330 |
This function is used to convert preview XML data into JSON. |
| 331 |
""" |
| 332 |
if XML_TO_JSON: |
| 333 |
xml = lxml.etree.fromstring(to_bytes(response_data)) |
| 334 |
xmldata = cobra.data(xml) |
| 335 |
aci.result["preview"] = xmldata |
| 336 |
else: |
| 337 |
aci.result["preview"] = response_data |
| 338 |
|
| 339 |
|
| 340 |
if __name__ == "__main__": |
| 341 |
main() |