| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Tim Cragg (@timcragg) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_file_remote_path |
| 16 |
short_description: Manage Import/Export File Remote Paths (file:RemotePath) |
| 17 |
description: |
| 18 |
- Manage Import/Export File Remote Paths on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- The name of the File Remote Path. |
| 23 |
type: str |
| 24 |
description: |
| 25 |
description: |
| 26 |
- The description of the File Remote Path. |
| 27 |
type: str |
| 28 |
remote_host: |
| 29 |
description: |
| 30 |
- The hostname or IP Address of the remote host. |
| 31 |
type: str |
| 32 |
remote_port: |
| 33 |
description: |
| 34 |
- The port to access the remote host. |
| 35 |
type: int |
| 36 |
remote_protocol: |
| 37 |
description: |
| 38 |
- The protocol to use to connect to the remote host. |
| 39 |
type: str |
| 40 |
choices: [ ftp, scp, sftp ] |
| 41 |
auth_type: |
| 42 |
description: |
| 43 |
- The authentication type for the remote host. |
| 44 |
- Cannot be set to ssh_key if protocol is ftp. |
| 45 |
type: str |
| 46 |
choices: [ password, ssh_key ] |
| 47 |
remote_user: |
| 48 |
description: |
| 49 |
- The username to access the remote host. |
| 50 |
type: str |
| 51 |
remote_password: |
| 52 |
description: |
| 53 |
- The password to access the remote host. |
| 54 |
- Only used if auth_type is password. |
| 55 |
- Providing this option will always result in a change because it is a secure property that cannot be retrieved from APIC. |
| 56 |
type: str |
| 57 |
remote_ssh_key: |
| 58 |
description: |
| 59 |
- The private SSH key used to access the remote host. |
| 60 |
- Only used if auth_type is ssh_key. |
| 61 |
- Providing this option will always result in a change because it is a secure property that cannot be retrieved from APIC. |
| 62 |
type: str |
| 63 |
aliases: [ remote_key ] |
| 64 |
remote_ssh_passphrase: |
| 65 |
description: |
| 66 |
- The Pass phrase used to decode private_key. |
| 67 |
- Only used if auth_type is ssh_key. |
| 68 |
- Providing this option will always result in a change because it is a secure property that cannot be retrieved from APIC. |
| 69 |
type: str |
| 70 |
aliases: [ passphrase ] |
| 71 |
remote_path: |
| 72 |
description: |
| 73 |
- The path on which the data will reside on the remote host. |
| 74 |
type: str |
| 75 |
management_epg: |
| 76 |
description: |
| 77 |
- The management EPG to connect to the remote host on. |
| 78 |
type: str |
| 79 |
state: |
| 80 |
description: |
| 81 |
- Use C(present) or C(absent) for adding or removing. |
| 82 |
- Use C(query) for listing an object or multiple objects. |
| 83 |
type: str |
| 84 |
choices: [ absent, present, query ] |
| 85 |
default: present |
| 86 |
extends_documentation_fragment: |
| 87 |
- cisco.aci.aci |
| 88 |
- cisco.aci.annotation |
| 89 |
|
| 90 |
seealso: |
| 91 |
- name: APIC Management Information Model reference |
| 92 |
description: More information about the internal APIC class B(file:RemotePath). |
| 93 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 94 |
|
| 95 |
author: |
| 96 |
- Tim Cragg (@timcragg) |
| 97 |
""" |
| 98 |
|
| 99 |
EXAMPLES = r""" |
| 100 |
- name: Add a Remote Path |
| 101 |
cisco.aci.aci_file_remote_path: |
| 102 |
host: apic |
| 103 |
username: admin |
| 104 |
password: SomeSecretPassword |
| 105 |
name: ans_remote_path |
| 106 |
remote_host: test.example.com |
| 107 |
remote_port: 22 |
| 108 |
remote_protocol: scp |
| 109 |
remote_user: test_user |
| 110 |
auth_type: password |
| 111 |
remote_password: test_pass |
| 112 |
remote_path: /tmp |
| 113 |
state: present |
| 114 |
delegate_to: localhost |
| 115 |
|
| 116 |
- name: Query a Remote Path |
| 117 |
cisco.aci.aci_file_remote_path: |
| 118 |
host: apic |
| 119 |
username: admin |
| 120 |
password: SomeSecretPassword |
| 121 |
name: ans_remote_path |
| 122 |
state: query |
| 123 |
delegate_to: localhost |
| 124 |
|
| 125 |
- name: Query all Remote Paths |
| 126 |
cisco.aci.aci_file_remote_path: |
| 127 |
host: apic |
| 128 |
username: admin |
| 129 |
password: SomeSecretPassword |
| 130 |
state: query |
| 131 |
delegate_to: localhost |
| 132 |
|
| 133 |
- name: Remove a Remote Path |
| 134 |
cisco.aci.aci_file_remote_path: |
| 135 |
host: apic |
| 136 |
username: admin |
| 137 |
password: SomeSecretPassword |
| 138 |
name: ans_remote_path |
| 139 |
state: absent |
| 140 |
delegate_to: localhost |
| 141 |
""" |
| 142 |
|
| 143 |
RETURN = r""" |
| 144 |
current: |
| 145 |
description: The existing configuration from the APIC after the module has finished |
| 146 |
returned: success |
| 147 |
type: list |
| 148 |
sample: |
| 149 |
[ |
| 150 |
{ |
| 151 |
"fvTenant": { |
| 152 |
"attributes": { |
| 153 |
"descr": "Production environment", |
| 154 |
"dn": "uni/tn-production", |
| 155 |
"name": "production", |
| 156 |
"nameAlias": "", |
| 157 |
"ownerKey": "", |
| 158 |
"ownerTag": "" |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
] |
| 163 |
error: |
| 164 |
description: The error information as returned from the APIC |
| 165 |
returned: failure |
| 166 |
type: dict |
| 167 |
sample: |
| 168 |
{ |
| 169 |
"code": "122", |
| 170 |
"text": "unknown managed object class foo" |
| 171 |
} |
| 172 |
raw: |
| 173 |
description: The raw output returned by the APIC REST API (xml or json) |
| 174 |
returned: parse error |
| 175 |
type: str |
| 176 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 177 |
sent: |
| 178 |
description: The actual/minimal configuration pushed to the APIC |
| 179 |
returned: info |
| 180 |
type: list |
| 181 |
sample: |
| 182 |
{ |
| 183 |
"fvTenant": { |
| 184 |
"attributes": { |
| 185 |
"descr": "Production environment" |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
previous: |
| 190 |
description: The original configuration from the APIC before the module has started |
| 191 |
returned: info |
| 192 |
type: list |
| 193 |
sample: |
| 194 |
[ |
| 195 |
{ |
| 196 |
"fvTenant": { |
| 197 |
"attributes": { |
| 198 |
"descr": "Production", |
| 199 |
"dn": "uni/tn-production", |
| 200 |
"name": "production", |
| 201 |
"nameAlias": "", |
| 202 |
"ownerKey": "", |
| 203 |
"ownerTag": "" |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
] |
| 208 |
proposed: |
| 209 |
description: The assembled configuration from the user-provided parameters |
| 210 |
returned: info |
| 211 |
type: dict |
| 212 |
sample: |
| 213 |
{ |
| 214 |
"fvTenant": { |
| 215 |
"attributes": { |
| 216 |
"descr": "Production environment", |
| 217 |
"name": "production" |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
filter_string: |
| 222 |
description: The filter string used for the request |
| 223 |
returned: failure or debug |
| 224 |
type: str |
| 225 |
sample: ?rsp-prop-include=config-only |
| 226 |
method: |
| 227 |
description: The HTTP method used for the request to the APIC |
| 228 |
returned: failure or debug |
| 229 |
type: str |
| 230 |
sample: POST |
| 231 |
response: |
| 232 |
description: The HTTP response from the APIC |
| 233 |
returned: failure or debug |
| 234 |
type: str |
| 235 |
sample: OK (30 bytes) |
| 236 |
status: |
| 237 |
description: The HTTP status from the APIC |
| 238 |
returned: failure or debug |
| 239 |
type: int |
| 240 |
sample: 200 |
| 241 |
url: |
| 242 |
description: The HTTP url used for the request to the APIC |
| 243 |
returned: failure or debug |
| 244 |
type: str |
| 245 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 246 |
""" |
| 247 |
|
| 248 |
from ansible.module_utils.basic import AnsibleModule |
| 249 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 250 |
|
| 251 |
|
| 252 |
def main(): |
| 253 |
argument_spec = aci_argument_spec() |
| 254 |
argument_spec.update(aci_annotation_spec()) |
| 255 |
argument_spec.update( |
| 256 |
name=dict(type="str"), |
| 257 |
description=dict(type="str"), |
| 258 |
remote_host=dict(type="str"), |
| 259 |
remote_port=dict(type="int"), |
| 260 |
remote_protocol=dict(type="str", choices=["ftp", "scp", "sftp"]), |
| 261 |
remote_path=dict(type="str"), |
| 262 |
auth_type=dict(type="str", choices=["password", "ssh_key"]), |
| 263 |
remote_user=dict(type="str"), |
| 264 |
remote_password=dict(type="str", no_log=True), |
| 265 |
remote_ssh_key=dict(type="str", no_log=True, aliases=["remote_key"]), |
| 266 |
remote_ssh_passphrase=dict(type="str", no_log=True, aliases=["passphrase"]), |
| 267 |
management_epg=dict(type="str"), |
| 268 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 269 |
) |
| 270 |
|
| 271 |
module = AnsibleModule( |
| 272 |
argument_spec=argument_spec, |
| 273 |
supports_check_mode=True, |
| 274 |
required_if=[ |
| 275 |
["state", "present", ["name", "auth_type"]], |
| 276 |
["state", "absent", ["name"]], |
| 277 |
], |
| 278 |
required_together=[["remote_host", "remote_port"]], |
| 279 |
) |
| 280 |
|
| 281 |
name = module.params.get("name") |
| 282 |
description = module.params.get("description") |
| 283 |
remote_host = module.params.get("remote_host") |
| 284 |
remote_port = module.params.get("remote_port") |
| 285 |
remote_protocol = module.params.get("remote_protocol") |
| 286 |
remote_path = module.params.get("remote_path") |
| 287 |
auth_type = module.params.get("auth_type") |
| 288 |
remote_user = module.params.get("remote_user") |
| 289 |
remote_password = module.params.get("remote_password") |
| 290 |
remote_key = module.params.get("remote_ssh_key") |
| 291 |
passphrase = module.params.get("remote_ssh_passphrase") |
| 292 |
management_epg = module.params.get("management_epg") |
| 293 |
state = module.params.get("state") |
| 294 |
|
| 295 |
aci = ACIModule(module) |
| 296 |
|
| 297 |
if auth_type == "password": |
| 298 |
if remote_key is not None: |
| 299 |
aci.fail_json(msg="remote_key cannot be set if auth_type is password") |
| 300 |
if passphrase is not None: |
| 301 |
aci.fail_json(msg="passphrase cannot be set if auth_type is password") |
| 302 |
auth = "usePassword" |
| 303 |
elif auth_type == "ssh_key": |
| 304 |
if remote_password is not None: |
| 305 |
aci.fail_json(msg="remote_password cannot be set if auth_type is ssh_key") |
| 306 |
auth = "useSshKeyContents" |
| 307 |
else: |
| 308 |
auth = None |
| 309 |
|
| 310 |
aci.construct_url( |
| 311 |
root_class=dict( |
| 312 |
aci_class="fileRemotePath", |
| 313 |
aci_rn="fabric/path-{0}".format(name), |
| 314 |
module_object=name, |
| 315 |
target_filter={"name": name}, |
| 316 |
), |
| 317 |
child_classes=["fileRsARemoteHostToEpg"], |
| 318 |
) |
| 319 |
aci.get_existing() |
| 320 |
|
| 321 |
if state == "present": |
| 322 |
child_configs = [] |
| 323 |
if management_epg is not None: |
| 324 |
child_configs.append( |
| 325 |
dict( |
| 326 |
fileRsARemoteHostToEpg=dict( |
| 327 |
attributes=dict(tDn=("uni/tn-mgmt/mgmtp-default/{0}".format(management_epg))), |
| 328 |
) |
| 329 |
) |
| 330 |
) |
| 331 |
aci.payload( |
| 332 |
aci_class="fileRemotePath", |
| 333 |
class_config=dict( |
| 334 |
name=name, |
| 335 |
descr=description, |
| 336 |
authType=auth, |
| 337 |
host=remote_host, |
| 338 |
protocol=remote_protocol, |
| 339 |
remotePath=remote_path, |
| 340 |
remotePort=remote_port, |
| 341 |
userName=remote_user, |
| 342 |
userPasswd=remote_password, |
| 343 |
identityPrivateKeyContents=remote_key, |
| 344 |
identityPrivateKeyPassphrase=passphrase, |
| 345 |
), |
| 346 |
child_configs=child_configs, |
| 347 |
) |
| 348 |
|
| 349 |
aci.get_diff(aci_class="fileRemotePath") |
| 350 |
|
| 351 |
aci.post_config() |
| 352 |
|
| 353 |
elif state == "absent": |
| 354 |
aci.delete_config() |
| 355 |
|
| 356 |
aci.exit_json() |
| 357 |
|
| 358 |
|
| 359 |
if __name__ == "__main__": |
| 360 |
main() |