| 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_interface_policy_port_security |
| 15 |
short_description: Manage port security (l2:PortSecurityPol) |
| 16 |
description: |
| 17 |
- Manage port security on Cisco ACI fabrics. |
| 18 |
options: |
| 19 |
port_security: |
| 20 |
description: |
| 21 |
- The name of the port security. |
| 22 |
type: str |
| 23 |
aliases: [ name ] |
| 24 |
description: |
| 25 |
description: |
| 26 |
- The description for the contract. |
| 27 |
type: str |
| 28 |
aliases: [ descr ] |
| 29 |
max_end_points: |
| 30 |
description: |
| 31 |
- Maximum number of end points. |
| 32 |
- Accepted values range between C(0) and C(12000). |
| 33 |
- The APIC defaults to C(0) when unset during creation. |
| 34 |
type: int |
| 35 |
port_security_timeout: |
| 36 |
description: |
| 37 |
- The delay time in seconds before MAC learning is re-enabled |
| 38 |
- Accepted values range between C(60) and C(3600) |
| 39 |
- The APIC defaults to C(60) when unset during creation |
| 40 |
type: int |
| 41 |
state: |
| 42 |
description: |
| 43 |
- Use C(present) or C(absent) for adding or removing. |
| 44 |
- Use C(query) for listing an object or multiple objects. |
| 45 |
type: str |
| 46 |
choices: [ absent, present, query ] |
| 47 |
default: present |
| 48 |
name_alias: |
| 49 |
description: |
| 50 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 51 |
type: str |
| 52 |
extends_documentation_fragment: |
| 53 |
- cisco.aci.aci |
| 54 |
- cisco.aci.annotation |
| 55 |
- cisco.aci.owner |
| 56 |
|
| 57 |
seealso: |
| 58 |
- name: APIC Management Information Model reference |
| 59 |
description: More information about the internal APIC class B(l2:PortSecurityPol). |
| 60 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 61 |
author: |
| 62 |
- Dag Wieers (@dagwieers) |
| 63 |
""" |
| 64 |
|
| 65 |
EXAMPLES = r""" |
| 66 |
- name: Create a Port Security interface policy |
| 67 |
cisco.aci.aci_interface_policy_port_security: |
| 68 |
host: apic |
| 69 |
username: admin |
| 70 |
password: SomeSecretPassword |
| 71 |
port_security: PS_1 |
| 72 |
max_end_points: 1 |
| 73 |
port_security_timeout: 60 |
| 74 |
state: present |
| 75 |
delegate_to: localhost |
| 76 |
|
| 77 |
- name: Delete a Port Security interface policy |
| 78 |
cisco.aci.aci_interface_policy_port_security: |
| 79 |
host: apic |
| 80 |
username: admin |
| 81 |
password: SomeSecretPassword |
| 82 |
port_security: PS_1 |
| 83 |
state: absent |
| 84 |
delegate_to: localhost |
| 85 |
|
| 86 |
- name: Query all Port Security interface policies |
| 87 |
cisco.aci.aci_interface_policy_port_security: |
| 88 |
host: apic |
| 89 |
username: admin |
| 90 |
password: SomeSecretPassword |
| 91 |
state: query |
| 92 |
delegate_to: localhost |
| 93 |
register: query_result |
| 94 |
|
| 95 |
- name: Query a specific Port Security interface policy |
| 96 |
cisco.aci.aci_interface_policy_port_security: |
| 97 |
host: apic |
| 98 |
username: admin |
| 99 |
password: SomeSecretPassword |
| 100 |
port_security: PS_1 |
| 101 |
state: query |
| 102 |
delegate_to: localhost |
| 103 |
register: query_result |
| 104 |
""" |
| 105 |
|
| 106 |
RETURN = r""" |
| 107 |
current: |
| 108 |
description: The existing configuration from the APIC after the module has finished |
| 109 |
returned: success |
| 110 |
type: list |
| 111 |
sample: |
| 112 |
[ |
| 113 |
{ |
| 114 |
"fvTenant": { |
| 115 |
"attributes": { |
| 116 |
"descr": "Production environment", |
| 117 |
"dn": "uni/tn-production", |
| 118 |
"name": "production", |
| 119 |
"nameAlias": "", |
| 120 |
"ownerKey": "", |
| 121 |
"ownerTag": "" |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
] |
| 126 |
error: |
| 127 |
description: The error information as returned from the APIC |
| 128 |
returned: failure |
| 129 |
type: dict |
| 130 |
sample: |
| 131 |
{ |
| 132 |
"code": "122", |
| 133 |
"text": "unknown managed object class foo" |
| 134 |
} |
| 135 |
raw: |
| 136 |
description: The raw output returned by the APIC REST API (xml or json) |
| 137 |
returned: parse error |
| 138 |
type: str |
| 139 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 140 |
sent: |
| 141 |
description: The actual/minimal configuration pushed to the APIC |
| 142 |
returned: info |
| 143 |
type: list |
| 144 |
sample: |
| 145 |
{ |
| 146 |
"fvTenant": { |
| 147 |
"attributes": { |
| 148 |
"descr": "Production environment" |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
previous: |
| 153 |
description: The original configuration from the APIC before the module has started |
| 154 |
returned: info |
| 155 |
type: list |
| 156 |
sample: |
| 157 |
[ |
| 158 |
{ |
| 159 |
"fvTenant": { |
| 160 |
"attributes": { |
| 161 |
"descr": "Production", |
| 162 |
"dn": "uni/tn-production", |
| 163 |
"name": "production", |
| 164 |
"nameAlias": "", |
| 165 |
"ownerKey": "", |
| 166 |
"ownerTag": "" |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
] |
| 171 |
proposed: |
| 172 |
description: The assembled configuration from the user-provided parameters |
| 173 |
returned: info |
| 174 |
type: dict |
| 175 |
sample: |
| 176 |
{ |
| 177 |
"fvTenant": { |
| 178 |
"attributes": { |
| 179 |
"descr": "Production environment", |
| 180 |
"name": "production" |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
filter_string: |
| 185 |
description: The filter string used for the request |
| 186 |
returned: failure or debug |
| 187 |
type: str |
| 188 |
sample: ?rsp-prop-include=config-only |
| 189 |
method: |
| 190 |
description: The HTTP method used for the request to the APIC |
| 191 |
returned: failure or debug |
| 192 |
type: str |
| 193 |
sample: POST |
| 194 |
response: |
| 195 |
description: The HTTP response from the APIC |
| 196 |
returned: failure or debug |
| 197 |
type: str |
| 198 |
sample: OK (30 bytes) |
| 199 |
status: |
| 200 |
description: The HTTP status from the APIC |
| 201 |
returned: failure or debug |
| 202 |
type: int |
| 203 |
sample: 200 |
| 204 |
url: |
| 205 |
description: The HTTP url used for the request to the APIC |
| 206 |
returned: failure or debug |
| 207 |
type: str |
| 208 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 209 |
""" |
| 210 |
|
| 211 |
from ansible.module_utils.basic import AnsibleModule |
| 212 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 213 |
|
| 214 |
|
| 215 |
def main(): |
| 216 |
argument_spec = aci_argument_spec() |
| 217 |
argument_spec.update(aci_annotation_spec()) |
| 218 |
argument_spec.update(aci_owner_spec()) |
| 219 |
argument_spec.update( |
| 220 |
port_security=dict(type="str", aliases=["name"]), # Not required for querying all objects |
| 221 |
description=dict(type="str", aliases=["descr"]), |
| 222 |
max_end_points=dict(type="int"), |
| 223 |
port_security_timeout=dict(type="int"), |
| 224 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 225 |
name_alias=dict(type="str"), |
| 226 |
) |
| 227 |
|
| 228 |
module = AnsibleModule( |
| 229 |
argument_spec=argument_spec, |
| 230 |
supports_check_mode=True, |
| 231 |
required_if=[ |
| 232 |
["state", "absent", ["port_security"]], |
| 233 |
["state", "present", ["port_security"]], |
| 234 |
], |
| 235 |
) |
| 236 |
|
| 237 |
port_security = module.params.get("port_security") |
| 238 |
description = module.params.get("description") |
| 239 |
max_end_points = module.params.get("max_end_points") |
| 240 |
port_security_timeout = module.params.get("port_security_timeout") |
| 241 |
name_alias = module.params.get("name_alias") |
| 242 |
if max_end_points is not None and max_end_points not in range(12001): |
| 243 |
module.fail_json(msg="The max_end_points must be between 0 and 12000") |
| 244 |
if port_security_timeout is not None and port_security_timeout not in range(60, 3601): |
| 245 |
module.fail_json(msg="The port_security_timeout must be between 60 and 3600") |
| 246 |
state = module.params.get("state") |
| 247 |
|
| 248 |
aci = ACIModule(module) |
| 249 |
aci.construct_url( |
| 250 |
root_class=dict( |
| 251 |
aci_class="l2PortSecurityPol", |
| 252 |
aci_rn="infra/portsecurityP-{0}".format(port_security), |
| 253 |
module_object=port_security, |
| 254 |
target_filter={"name": port_security}, |
| 255 |
), |
| 256 |
) |
| 257 |
|
| 258 |
aci.get_existing() |
| 259 |
|
| 260 |
if state == "present": |
| 261 |
aci.payload( |
| 262 |
aci_class="l2PortSecurityPol", |
| 263 |
class_config=dict( |
| 264 |
name=port_security, |
| 265 |
descr=description, |
| 266 |
maximum=max_end_points, |
| 267 |
nameAlias=name_alias, |
| 268 |
timeout=port_security_timeout, |
| 269 |
), |
| 270 |
) |
| 271 |
|
| 272 |
aci.get_diff(aci_class="l2PortSecurityPol") |
| 273 |
|
| 274 |
aci.post_config() |
| 275 |
|
| 276 |
elif state == "absent": |
| 277 |
aci.delete_config() |
| 278 |
|
| 279 |
aci.exit_json() |
| 280 |
|
| 281 |
|
| 282 |
if __name__ == "__main__": |
| 283 |
main() |