| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Akini Ross (@akinross) <[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": "community"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: mso_remote_location |
| 16 |
short_description: Manages remote locations |
| 17 |
description: |
| 18 |
- Manage remote locations on Cisco ACI Multi-Site. |
| 19 |
author: |
| 20 |
- Akini Ross (@akinross) |
| 21 |
options: |
| 22 |
remote_location: |
| 23 |
description: |
| 24 |
- The remote location's name. |
| 25 |
type: str |
| 26 |
aliases: [ name ] |
| 27 |
description: |
| 28 |
description: |
| 29 |
- The remote location's description. |
| 30 |
type: str |
| 31 |
remote_protocol: |
| 32 |
description: |
| 33 |
- The protocol used to export to the remote server. |
| 34 |
- If the remote location is a Windows server, you must use the C(sftp) protocol. |
| 35 |
choices: [ scp, sftp ] |
| 36 |
type: str |
| 37 |
remote_host: |
| 38 |
description: |
| 39 |
- The host name or IP address of the remote server. |
| 40 |
type: str |
| 41 |
remote_path: |
| 42 |
description: |
| 43 |
- The full path to a directory on the remote server where backups are saved. |
| 44 |
- The path must start with a slash (/) character and must not contain periods (.) or backslashes (\). |
| 45 |
- The directory must already exist on the server. |
| 46 |
type: str |
| 47 |
remote_port: |
| 48 |
description: |
| 49 |
- The port used to connect to the remote server. |
| 50 |
default: 22 |
| 51 |
type: int |
| 52 |
authentication_type: |
| 53 |
description: |
| 54 |
- The authentication method used to connect to the remote server. |
| 55 |
choices: [ password, ssh ] |
| 56 |
type: str |
| 57 |
remote_username: |
| 58 |
description: |
| 59 |
- The username used to log in to the remote server. |
| 60 |
type: str |
| 61 |
remote_password: |
| 62 |
description: |
| 63 |
- The password used to log in to the remote server. |
| 64 |
type: str |
| 65 |
remote_ssh_key: |
| 66 |
description: |
| 67 |
- The private ssh key used to log in to the remote server. |
| 68 |
- The private ssh key must be provided in PEM format. |
| 69 |
- The private ssh key must be a single line string with linebreaks represent as "\n". |
| 70 |
type: str |
| 71 |
remote_ssh_passphrase: |
| 72 |
description: |
| 73 |
- The private ssh key passphrase used to log in to the remote server. |
| 74 |
type: str |
| 75 |
state: |
| 76 |
description: |
| 77 |
- Use C(present) or C(absent) for adding or removing. |
| 78 |
- Use C(query) for listing an object or multiple objects. |
| 79 |
type: str |
| 80 |
choices: [ absent, present, query ] |
| 81 |
default: present |
| 82 |
extends_documentation_fragment: cisco.mso.modules |
| 83 |
""" |
| 84 |
|
| 85 |
EXAMPLES = r""" |
| 86 |
- name: Query all remote locations |
| 87 |
cisco.mso.mso_remote_location: |
| 88 |
host: mso_host |
| 89 |
username: admin |
| 90 |
password: SomeSecretPassword |
| 91 |
state: query |
| 92 |
register: backups |
| 93 |
|
| 94 |
- name: Query a remote location |
| 95 |
cisco.mso.mso_remote_location: |
| 96 |
host: mso_host |
| 97 |
username: admin |
| 98 |
password: SomeSecretPassword |
| 99 |
remote_location: ansible_test |
| 100 |
state: query |
| 101 |
register: query_result |
| 102 |
|
| 103 |
- name: Configure a remote location |
| 104 |
cisco.mso.mso_remote_location: |
| 105 |
host: mso_host |
| 106 |
username: admin |
| 107 |
password: SomeSecretPassword |
| 108 |
remote_location: ansible_test |
| 109 |
remote_protocol: scp |
| 110 |
remote_host: 10.0.0.1 |
| 111 |
remote_path: /username/backup |
| 112 |
remote_authentication_type: password |
| 113 |
remote_username: username |
| 114 |
remote_password: password |
| 115 |
state: present |
| 116 |
|
| 117 |
- name: Delete a remote location |
| 118 |
cisco.mso.mso_remote_location: |
| 119 |
host: mso_host |
| 120 |
username: admin |
| 121 |
password: SomeSecretPassword |
| 122 |
remote_location: ansible_test |
| 123 |
state: absent |
| 124 |
""" |
| 125 |
|
| 126 |
RETURN = r""" |
| 127 |
""" |
| 128 |
|
| 129 |
from ansible.module_utils.basic import AnsibleModule |
| 130 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 131 |
|
| 132 |
|
| 133 |
def main(): |
| 134 |
argument_spec = mso_argument_spec() |
| 135 |
argument_spec.update( |
| 136 |
remote_location=dict(type="str", aliases=["name"]), |
| 137 |
description=dict(type="str"), |
| 138 |
remote_protocol=dict(type="str", choices=["scp", "sftp"]), |
| 139 |
remote_host=dict(type="str"), |
| 140 |
remote_path=dict(type="str"), |
| 141 |
remote_port=dict(type="int", default=22), |
| 142 |
authentication_type=dict(type="str", choices=["password", "ssh"]), |
| 143 |
remote_username=dict(type="str"), |
| 144 |
remote_password=dict(type="str", no_log=True), |
| 145 |
remote_ssh_key=dict(type="str", no_log=True), |
| 146 |
remote_ssh_passphrase=dict(type="str", no_log=True), |
| 147 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 148 |
) |
| 149 |
|
| 150 |
module = AnsibleModule( |
| 151 |
argument_spec=argument_spec, |
| 152 |
supports_check_mode=True, |
| 153 |
required_if=[ |
| 154 |
["state", "present", ["remote_location", "remote_protocol", "remote_host", "remote_path", "authentication_type"]], |
| 155 |
["state", "absent", ["remote_location"]], |
| 156 |
["authentication_type", "password", ["remote_username", "remote_password"]], |
| 157 |
["authentication_type", "ssh", ["remote_ssh_key"]], |
| 158 |
], |
| 159 |
) |
| 160 |
|
| 161 |
location_name = module.params.get("remote_location") |
| 162 |
description = module.params.get("description") |
| 163 |
protocol = module.params.get("remote_protocol") |
| 164 |
host = module.params.get("remote_host") |
| 165 |
path = module.params.get("remote_path") |
| 166 |
port = module.params.get("remote_port") |
| 167 |
authentication_type = module.params.get("authentication_type") |
| 168 |
username = module.params.get("remote_username") |
| 169 |
password = module.params.get("remote_password") |
| 170 |
ssh_key = module.params.get("remote_ssh_key") |
| 171 |
passphrase = module.params.get("remote_ssh_passphrase") |
| 172 |
state = module.params.get("state") |
| 173 |
|
| 174 |
mso = MSOModule(module) |
| 175 |
api_path = "platform/remote-locations" |
| 176 |
mso.existing = mso.query_objs(api_path, key="remoteLocations") |
| 177 |
|
| 178 |
remote_location_obj = None |
| 179 |
if location_name and mso.existing: |
| 180 |
remote_location_obj = next((item for item in mso.existing if item.get("name") == location_name), None) |
| 181 |
if remote_location_obj: |
| 182 |
mso.existing = remote_location_obj |
| 183 |
|
| 184 |
if state == "query": |
| 185 |
if location_name and not remote_location_obj: |
| 186 |
existing_location_list = ", ".join([item.get("name") for item in mso.existing]) |
| 187 |
mso.module.fail_json(msg="Remote location {0} not found. Remote locations configured: {1}".format(location_name, existing_location_list)) |
| 188 |
|
| 189 |
elif state == "absent": |
| 190 |
mso.previous = mso.existing |
| 191 |
|
| 192 |
if module.check_mode: |
| 193 |
mso.existing = {} |
| 194 |
elif remote_location_obj: |
| 195 |
mso.existing = mso.request("{0}/{1}".format(api_path, remote_location_obj.get("id")), method="DELETE") |
| 196 |
|
| 197 |
elif state == "present": |
| 198 |
mso.previous = mso.existing |
| 199 |
|
| 200 |
credential = dict( |
| 201 |
authType=authentication_type if authentication_type == "password" else "sshKey", |
| 202 |
hostname=host, |
| 203 |
port=port, |
| 204 |
protocolType=protocol, |
| 205 |
remotePath=path, |
| 206 |
username=username, |
| 207 |
) |
| 208 |
|
| 209 |
if authentication_type == "password": |
| 210 |
credential.update(password=password) |
| 211 |
else: |
| 212 |
credential.update(sshKey=ssh_key) |
| 213 |
if passphrase: |
| 214 |
credential.update(passPhrase=passphrase) |
| 215 |
|
| 216 |
payload = dict(name=location_name, credential=credential) |
| 217 |
|
| 218 |
if description: |
| 219 |
payload.update(description=description) |
| 220 |
|
| 221 |
mso.proposed = payload |
| 222 |
|
| 223 |
if module.check_mode: |
| 224 |
mso.existing = mso.proposed |
| 225 |
else: |
| 226 |
if remote_location_obj: |
| 227 |
payload.update(id=remote_location_obj.get("id")) |
| 228 |
mso.existing = mso.request("{0}/{1}".format(api_path, remote_location_obj.get("id")), method="PUT", data=payload) |
| 229 |
else: |
| 230 |
mso.existing = mso.request(api_path, method="POST", data=payload) |
| 231 |
|
| 232 |
mso.existing["credential"].pop("password", None) |
| 233 |
mso.existing["credential"].pop("sshKey", None) |
| 234 |
mso.existing["credential"].pop("passPhrase", None) |
| 235 |
|
| 236 |
mso.exit_json() |
| 237 |
|
| 238 |
|
| 239 |
if __name__ == "__main__": |
| 240 |
main() |