| 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_snapshot |
| 15 |
short_description: Manage Config Snapshots (config:Snapshot and config:ExportP) |
| 16 |
description: |
| 17 |
- Manage Config Snapshots on Cisco ACI fabrics. |
| 18 |
- Creating new Snapshots is done using the configExportP class. |
| 19 |
- Removing Snapshots is done using the configSnapshot class. |
| 20 |
options: |
| 21 |
description: |
| 22 |
description: |
| 23 |
- The description for the Config Export Policy. |
| 24 |
type: str |
| 25 |
aliases: [ descr ] |
| 26 |
export_policy: |
| 27 |
description: |
| 28 |
- The name of the Export Policy to use for Config Snapshots. |
| 29 |
type: str |
| 30 |
aliases: [ name ] |
| 31 |
format: |
| 32 |
description: |
| 33 |
- Sets the config backup to be formatted in JSON or XML. |
| 34 |
- The APIC defaults to C(json) when unset. |
| 35 |
type: str |
| 36 |
choices: [ json, xml ] |
| 37 |
include_secure: |
| 38 |
description: |
| 39 |
- Determines if secure information should be included in the backup. |
| 40 |
- The APIC defaults to C(true) when unset. |
| 41 |
type: bool |
| 42 |
max_count: |
| 43 |
description: |
| 44 |
- Determines how many snapshots can exist for the Export Policy before the APIC starts to rollover. |
| 45 |
- Accepted values range between C(1) and C(10). |
| 46 |
- The APIC defaults to C(3) when unset. |
| 47 |
type: int |
| 48 |
snapshot: |
| 49 |
description: |
| 50 |
- The name of the snapshot to delete. |
| 51 |
type: str |
| 52 |
state: |
| 53 |
description: |
| 54 |
- Use C(present) or C(absent) for adding or removing. |
| 55 |
- Use C(query) for listing an object or multiple objects. |
| 56 |
type: str |
| 57 |
choices: [ absent, present, query ] |
| 58 |
default: present |
| 59 |
extends_documentation_fragment: |
| 60 |
- cisco.aci.aci |
| 61 |
- cisco.aci.annotation |
| 62 |
|
| 63 |
notes: |
| 64 |
- The APIC does not provide a mechanism for naming the snapshots. |
| 65 |
- 'Snapshot files use the following naming structure: ce_<config export policy name>-<yyyy>-<mm>-<dd>T<hh>:<mm>:<ss>.<mss>+<hh>:<mm>.' |
| 66 |
- 'Snapshot objects use the following naming structure: run-<yyyy>-<mm>-<dd>T<hh>-<mm>-<ss>.' |
| 67 |
- It is strongly recommended to add a pause task after creating a Snapshot. |
| 68 |
- Wait for the Snapshot to be finished before querying, comparing Snapshots or processing to Rollbacks. |
| 69 |
seealso: |
| 70 |
- module: cisco.aci.aci_config_rollback |
| 71 |
- name: APIC Management Information Model reference |
| 72 |
description: More information about the internal APIC classes B(config:Snapshot) and B(config:ExportP). |
| 73 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 74 |
author: |
| 75 |
- Jacob McGill (@jmcgill298) |
| 76 |
- Gaspard Micol (@gmicol) |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
- name: Create a Snapshot |
| 81 |
cisco.aci.aci_config_snapshot: |
| 82 |
host: apic |
| 83 |
username: admin |
| 84 |
password: SomeSecretPassword |
| 85 |
state: present |
| 86 |
export_policy: config_backup |
| 87 |
max_count: 10 |
| 88 |
description: Backups taken before new configs are applied. |
| 89 |
delegate_to: localhost |
| 90 |
|
| 91 |
- name: Wait for snapshot to be finished before querying |
| 92 |
pause: |
| 93 |
seconds: 10 |
| 94 |
|
| 95 |
- name: Query all Snapshots |
| 96 |
cisco.aci.aci_config_snapshot: |
| 97 |
host: apic |
| 98 |
username: admin |
| 99 |
password: SomeSecretPassword |
| 100 |
state: query |
| 101 |
delegate_to: localhost |
| 102 |
register: query_result |
| 103 |
|
| 104 |
- name: Query Snapshots associated with a particular Export Policy |
| 105 |
cisco.aci.aci_config_snapshot: |
| 106 |
host: apic |
| 107 |
username: admin |
| 108 |
password: SomeSecretPassword |
| 109 |
export_policy: config_backup |
| 110 |
state: query |
| 111 |
delegate_to: localhost |
| 112 |
register: query_result |
| 113 |
|
| 114 |
- name: Delete a Snapshot |
| 115 |
cisco.aci.aci_config_snapshot: |
| 116 |
host: apic |
| 117 |
username: admin |
| 118 |
password: SomeSecretPassword |
| 119 |
export_policy: config_backup |
| 120 |
snapshot: run-2017-08-24T17-20-05 |
| 121 |
state: absent |
| 122 |
delegate_to: localhost |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = r""" |
| 126 |
current: |
| 127 |
description: The existing configuration from the APIC after the module has finished |
| 128 |
returned: success |
| 129 |
type: list |
| 130 |
sample: |
| 131 |
[ |
| 132 |
{ |
| 133 |
"fvTenant": { |
| 134 |
"attributes": { |
| 135 |
"descr": "Production environment", |
| 136 |
"dn": "uni/tn-production", |
| 137 |
"name": "production", |
| 138 |
"nameAlias": "", |
| 139 |
"ownerKey": "", |
| 140 |
"ownerTag": "" |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
] |
| 145 |
error: |
| 146 |
description: The error information as returned from the APIC |
| 147 |
returned: failure |
| 148 |
type: dict |
| 149 |
sample: |
| 150 |
{ |
| 151 |
"code": "122", |
| 152 |
"text": "unknown managed object class foo" |
| 153 |
} |
| 154 |
raw: |
| 155 |
description: The raw output returned by the APIC REST API (xml or json) |
| 156 |
returned: parse error |
| 157 |
type: str |
| 158 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 159 |
sent: |
| 160 |
description: The actual/minimal configuration pushed to the APIC |
| 161 |
returned: info |
| 162 |
type: list |
| 163 |
sample: |
| 164 |
{ |
| 165 |
"fvTenant": { |
| 166 |
"attributes": { |
| 167 |
"descr": "Production environment" |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
previous: |
| 172 |
description: The original configuration from the APIC before the module has started |
| 173 |
returned: info |
| 174 |
type: list |
| 175 |
sample: |
| 176 |
[ |
| 177 |
{ |
| 178 |
"fvTenant": { |
| 179 |
"attributes": { |
| 180 |
"descr": "Production", |
| 181 |
"dn": "uni/tn-production", |
| 182 |
"name": "production", |
| 183 |
"nameAlias": "", |
| 184 |
"ownerKey": "", |
| 185 |
"ownerTag": "" |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
] |
| 190 |
proposed: |
| 191 |
description: The assembled configuration from the user-provided parameters |
| 192 |
returned: info |
| 193 |
type: dict |
| 194 |
sample: |
| 195 |
{ |
| 196 |
"fvTenant": { |
| 197 |
"attributes": { |
| 198 |
"descr": "Production environment", |
| 199 |
"name": "production" |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
filter_string: |
| 204 |
description: The filter string used for the request |
| 205 |
returned: failure or debug |
| 206 |
type: str |
| 207 |
sample: ?rsp-prop-include=config-only |
| 208 |
method: |
| 209 |
description: The HTTP method used for the request to the APIC |
| 210 |
returned: failure or debug |
| 211 |
type: str |
| 212 |
sample: POST |
| 213 |
response: |
| 214 |
description: The HTTP response from the APIC |
| 215 |
returned: failure or debug |
| 216 |
type: str |
| 217 |
sample: OK (30 bytes) |
| 218 |
status: |
| 219 |
description: The HTTP status from the APIC |
| 220 |
returned: failure or debug |
| 221 |
type: int |
| 222 |
sample: 200 |
| 223 |
url: |
| 224 |
description: The HTTP url used for the request to the APIC |
| 225 |
returned: failure or debug |
| 226 |
type: str |
| 227 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 228 |
""" |
| 229 |
|
| 230 |
import json |
| 231 |
|
| 232 |
from ansible.module_utils.basic import AnsibleModule |
| 233 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 234 |
|
| 235 |
|
| 236 |
def main(): |
| 237 |
argument_spec = aci_argument_spec() |
| 238 |
argument_spec.update(aci_annotation_spec()) |
| 239 |
argument_spec.update( |
| 240 |
description=dict(type="str", aliases=["descr"]), |
| 241 |
export_policy=dict(type="str", aliases=["name"]), # Not required for querying all objects |
| 242 |
format=dict(type="str", choices=["json", "xml"]), |
| 243 |
include_secure=dict(type="bool"), |
| 244 |
max_count=dict(type="int"), |
| 245 |
snapshot=dict(type="str"), |
| 246 |
state=dict(type="str", choices=["absent", "present", "query"], default="present"), |
| 247 |
) |
| 248 |
|
| 249 |
module = AnsibleModule( |
| 250 |
argument_spec=argument_spec, |
| 251 |
supports_check_mode=False, |
| 252 |
required_if=[ |
| 253 |
["state", "absent", ["export_policy", "snapshot"]], |
| 254 |
["state", "present", ["export_policy"]], |
| 255 |
], |
| 256 |
) |
| 257 |
|
| 258 |
aci = ACIModule(module) |
| 259 |
|
| 260 |
description = module.params.get("description") |
| 261 |
export_policy = module.params.get("export_policy") |
| 262 |
file_format = module.params.get("format") |
| 263 |
include_secure = aci.boolean(module.params.get("include_secure")) |
| 264 |
max_count = module.params.get("max_count") |
| 265 |
if max_count is not None: |
| 266 |
if max_count in range(1, 11): |
| 267 |
max_count = str(max_count) |
| 268 |
else: |
| 269 |
module.fail_json(msg="Parameter 'max_count' must be a number between 1 and 10") |
| 270 |
snapshot = module.params.get("snapshot") |
| 271 |
if snapshot is not None and not snapshot.startswith("run-"): |
| 272 |
snapshot = "run-" + snapshot |
| 273 |
state = module.params.get("state") |
| 274 |
|
| 275 |
if state == "present": |
| 276 |
aci.construct_url( |
| 277 |
root_class=dict( |
| 278 |
aci_class="configExportP", |
| 279 |
aci_rn="fabric/configexp-{0}".format(export_policy), |
| 280 |
module_object=export_policy, |
| 281 |
target_filter={"name": export_policy}, |
| 282 |
), |
| 283 |
) |
| 284 |
|
| 285 |
aci.get_existing() |
| 286 |
|
| 287 |
aci.payload( |
| 288 |
aci_class="configExportP", |
| 289 |
class_config=dict( |
| 290 |
adminSt="triggered", |
| 291 |
descr=description, |
| 292 |
format=file_format, |
| 293 |
includeSecureFields=include_secure, |
| 294 |
maxSnapshotCount=max_count, |
| 295 |
name=export_policy, |
| 296 |
snapshot="yes", |
| 297 |
), |
| 298 |
) |
| 299 |
|
| 300 |
aci.get_diff("configExportP") |
| 301 |
|
| 302 |
# Create a new Snapshot |
| 303 |
aci.post_config() |
| 304 |
|
| 305 |
# Query for job information and add to results |
| 306 |
path = "api/node/mo/uni/backupst/jobs-[uni/fabric/configexp-{0}].json".format(export_policy) |
| 307 |
resp, info = aci.api_call("GET", "{0}/{1}".format(aci.base_url, path), return_response=True) |
| 308 |
try: |
| 309 |
aci.imdata = json.loads(resp.read()).get("imdata") |
| 310 |
except AttributeError: |
| 311 |
aci.imdata = json.loads(info.get("body")).get("imdata") |
| 312 |
aci.result["job_details"] = {} |
| 313 |
if aci.imdata and isinstance(aci.imdata, list) and isinstance(aci.imdata[0], dict): |
| 314 |
aci.result["job_details"] = aci.imdata[0].get("configJobCont", {}) |
| 315 |
|
| 316 |
else: |
| 317 |
# Prefix the proper url to export_policy |
| 318 |
if export_policy is not None: |
| 319 |
export_policy = "uni/fabric/configexp-{0}".format(export_policy) |
| 320 |
|
| 321 |
aci.construct_url( |
| 322 |
root_class=dict( |
| 323 |
aci_class="configSnapshotCont", |
| 324 |
aci_rn="backupst/snapshots-[{0}]".format(export_policy), |
| 325 |
module_object=export_policy, |
| 326 |
target_filter={"name": export_policy}, |
| 327 |
), |
| 328 |
subclass_1=dict( |
| 329 |
aci_class="configSnapshot", |
| 330 |
aci_rn="snapshot-{0}".format(snapshot), |
| 331 |
module_object=snapshot, |
| 332 |
target_filter={"name": snapshot}, |
| 333 |
), |
| 334 |
) |
| 335 |
|
| 336 |
aci.get_existing() |
| 337 |
|
| 338 |
if state == "absent": |
| 339 |
# Build POST request to used to remove Snapshot |
| 340 |
aci.payload( |
| 341 |
aci_class="configSnapshot", |
| 342 |
class_config=dict( |
| 343 |
name=snapshot, |
| 344 |
retire="yes", |
| 345 |
), |
| 346 |
) |
| 347 |
|
| 348 |
if aci.existing: |
| 349 |
aci.get_diff("configSnapshot") |
| 350 |
|
| 351 |
# Mark Snapshot for Deletion |
| 352 |
aci.post_config() |
| 353 |
|
| 354 |
aci.exit_json() |
| 355 |
|
| 356 |
|
| 357 |
if __name__ == "__main__": |
| 358 |
main() |