| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Gaspard Micol (@gmicol) <[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_l3out_bgp_protocol_profile |
| 16 |
short_description: Manage BGP Protocol Profile (bgp:ProtP) |
| 17 |
description: |
| 18 |
- Manage BGP Protocol Profile for The Logical Node Profiles on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
tenant: |
| 21 |
description: |
| 22 |
- The name of an existing tenant. |
| 23 |
type: str |
| 24 |
aliases: [ tenant_name ] |
| 25 |
l3out: |
| 26 |
description: |
| 27 |
- The name of an existing L3Out. |
| 28 |
type: str |
| 29 |
aliases: [ l3out_name ] |
| 30 |
node_profile: |
| 31 |
description: |
| 32 |
- The name of an existing logical node profile. |
| 33 |
type: str |
| 34 |
aliases: [ node_profile_name, logical_node ] |
| 35 |
bgp_timers_policy: |
| 36 |
description: |
| 37 |
- The name of an existing bgp timers policy. |
| 38 |
type: str |
| 39 |
aliases: [ bgp_timers_policy_name ] |
| 40 |
bgp_best_path_policy: |
| 41 |
description: |
| 42 |
- The name of the bgp best path control policy. |
| 43 |
type: str |
| 44 |
aliases: [ bgp_best_path_policy_name ] |
| 45 |
state: |
| 46 |
description: |
| 47 |
- Use C(present) or C(absent) for adding or removing. |
| 48 |
- Use C(query) for listing an object or multiple objects. |
| 49 |
type: str |
| 50 |
choices: [ absent, present, query ] |
| 51 |
default: present |
| 52 |
name_alias: |
| 53 |
description: |
| 54 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 55 |
type: str |
| 56 |
extends_documentation_fragment: |
| 57 |
- cisco.aci.aci |
| 58 |
- cisco.aci.annotation |
| 59 |
- cisco.aci.owner |
| 60 |
|
| 61 |
notes: |
| 62 |
- The C(tenant), C(l3out) and C(node_profile) used must exist before using this module in your playbook. |
| 63 |
The M(cisco.aci.aci_tenant), M(cisco.aci.aci_l3out) and M(cisco.aci.aci_l3out_logical_node_profile) modules can be used for this. |
| 64 |
- If C(bgp_timers_policy) and/or C(bgp_best_path_policy) are used, they must exist before using this module in your playbook. |
| 65 |
The M(cisco.aci.aci_bgp_timers_policy) and M(cisco.aci.aci_bgp_best_path_policy) modules can be used for this. |
| 66 |
seealso: |
| 67 |
- module: cisco.aci.aci_tenant |
| 68 |
- module: cisco.aci.aci_l3out |
| 69 |
- module: cisco.aci.aci_l3out_logical_node_profile |
| 70 |
- module: cisco.aci.aci_bgp_timers_policy |
| 71 |
- module: cisco.aci.aci_bgp_best_path_policy |
| 72 |
- name: APIC Management Information Model reference |
| 73 |
description: More information about the internal APIC class B(bgp:ProtP). |
| 74 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 75 |
author: |
| 76 |
- Gaspard Micol (@gmicol) |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
- name: Create a l3out BGP protocol profile |
| 81 |
cisco.aci.l3out_bgp_protocol_profile: |
| 82 |
host: apic |
| 83 |
username: admin |
| 84 |
password: SomeSecretPassword |
| 85 |
tenant: production |
| 86 |
l3out: prod_l3out |
| 87 |
node_profile: prod_node_profile |
| 88 |
bgp_timers_policy: prod_bgp_timers_policy |
| 89 |
bgp_best_path_policy: prod_bgp_best_path_policy |
| 90 |
state: present |
| 91 |
delegate_to: localhost |
| 92 |
|
| 93 |
- name: Delete a l3out BGP protocol profile |
| 94 |
cisco.aci.l3out_bgp_protocol_profile: |
| 95 |
host: apic |
| 96 |
username: admin |
| 97 |
password: SomeSecretPassword |
| 98 |
tenant: production |
| 99 |
l3out: prod_l3out |
| 100 |
node_profile: prod_node_profile |
| 101 |
state: absent |
| 102 |
delegate_to: localhost |
| 103 |
|
| 104 |
- name: Query all l3out BGP protocol profiles |
| 105 |
cisco.aci.l3out_bgp_protocol_profile: |
| 106 |
host: apic |
| 107 |
username: admin |
| 108 |
password: SomeSecretPassword |
| 109 |
state: query |
| 110 |
delegate_to: localhost |
| 111 |
register: query_result |
| 112 |
|
| 113 |
- name: Query a specific l3out BGP protocol profile |
| 114 |
cisco.aci.l3out_bgp_protocol_profile: |
| 115 |
host: apic |
| 116 |
username: admin |
| 117 |
password: SomeSecretPassword |
| 118 |
tenant: production |
| 119 |
l3out: prod_l3out |
| 120 |
node_profile: prod_node_profile |
| 121 |
state: query |
| 122 |
delegate_to: localhost |
| 123 |
register: query_result |
| 124 |
""" |
| 125 |
|
| 126 |
RETURN = r""" |
| 127 |
current: |
| 128 |
description: The existing configuration from the APIC after the module has finished |
| 129 |
returned: success |
| 130 |
type: list |
| 131 |
sample: |
| 132 |
[ |
| 133 |
{ |
| 134 |
"fvTenant": { |
| 135 |
"attributes": { |
| 136 |
"descr": "Production environment", |
| 137 |
"dn": "uni/tn-production", |
| 138 |
"name": "production", |
| 139 |
"nameAlias": "", |
| 140 |
"ownerKey": "", |
| 141 |
"ownerTag": "" |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
] |
| 146 |
error: |
| 147 |
description: The error information as returned from the APIC |
| 148 |
returned: failure |
| 149 |
type: dict |
| 150 |
sample: |
| 151 |
{ |
| 152 |
"code": "122", |
| 153 |
"text": "unknown managed object class foo" |
| 154 |
} |
| 155 |
raw: |
| 156 |
description: The raw output returned by the APIC REST API (xml or json) |
| 157 |
returned: parse error |
| 158 |
type: str |
| 159 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 160 |
sent: |
| 161 |
description: The actual/minimal configuration pushed to the APIC |
| 162 |
returned: info |
| 163 |
type: list |
| 164 |
sample: |
| 165 |
{ |
| 166 |
"fvTenant": { |
| 167 |
"attributes": { |
| 168 |
"descr": "Production environment" |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
previous: |
| 173 |
description: The original configuration from the APIC before the module has started |
| 174 |
returned: info |
| 175 |
type: list |
| 176 |
sample: |
| 177 |
[ |
| 178 |
{ |
| 179 |
"fvTenant": { |
| 180 |
"attributes": { |
| 181 |
"descr": "Production", |
| 182 |
"dn": "uni/tn-production", |
| 183 |
"name": "production", |
| 184 |
"nameAlias": "", |
| 185 |
"ownerKey": "", |
| 186 |
"ownerTag": "" |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
] |
| 191 |
proposed: |
| 192 |
description: The assembled configuration from the user-provided parameters |
| 193 |
returned: info |
| 194 |
type: dict |
| 195 |
sample: |
| 196 |
{ |
| 197 |
"fvTenant": { |
| 198 |
"attributes": { |
| 199 |
"descr": "Production environment", |
| 200 |
"name": "production" |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
filter_string: |
| 205 |
description: The filter string used for the request |
| 206 |
returned: failure or debug |
| 207 |
type: str |
| 208 |
sample: ?rsp-prop-include=config-only |
| 209 |
method: |
| 210 |
description: The HTTP method used for the request to the APIC |
| 211 |
returned: failure or debug |
| 212 |
type: str |
| 213 |
sample: POST |
| 214 |
response: |
| 215 |
description: The HTTP response from the APIC |
| 216 |
returned: failure or debug |
| 217 |
type: str |
| 218 |
sample: OK (30 bytes) |
| 219 |
status: |
| 220 |
description: The HTTP status from the APIC |
| 221 |
returned: failure or debug |
| 222 |
type: int |
| 223 |
sample: 200 |
| 224 |
url: |
| 225 |
description: The HTTP url used for the request to the APIC |
| 226 |
returned: failure or debug |
| 227 |
type: str |
| 228 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 229 |
""" |
| 230 |
|
| 231 |
from ansible.module_utils.basic import AnsibleModule |
| 232 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 233 |
|
| 234 |
|
| 235 |
def main(): |
| 236 |
argument_spec = aci_argument_spec() |
| 237 |
argument_spec.update(aci_annotation_spec()) |
| 238 |
argument_spec.update(aci_owner_spec()) |
| 239 |
argument_spec.update( |
| 240 |
tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects |
| 241 |
l3out=dict(type="str", aliases=["l3out_name"]), # Not required for querying all objects |
| 242 |
node_profile=dict(type="str", aliases=["node_profile_name", "logical_node"]), # Not required for querying all objects |
| 243 |
bgp_timers_policy=dict(type="str", aliases=["bgp_timers_policy_name"]), |
| 244 |
bgp_best_path_policy=dict(type="str", aliases=["bgp_best_path_policy_name"]), |
| 245 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 246 |
name_alias=dict(type="str"), |
| 247 |
) |
| 248 |
|
| 249 |
module = AnsibleModule( |
| 250 |
argument_spec=argument_spec, |
| 251 |
supports_check_mode=True, |
| 252 |
required_if=[ |
| 253 |
["state", "absent", ["tenant", "l3out", "node_profile"]], |
| 254 |
["state", "present", ["tenant", "l3out", "node_profile"]], |
| 255 |
], |
| 256 |
) |
| 257 |
|
| 258 |
bgp_timers_policy = module.params.get("bgp_timers_policy") |
| 259 |
bgp_best_path_policy = module.params.get("bgp_best_path_policy") |
| 260 |
state = module.params.get("state") |
| 261 |
tenant = module.params.get("tenant") |
| 262 |
l3out = module.params.get("l3out") |
| 263 |
node_profile = module.params.get("node_profile") |
| 264 |
name_alias = module.params.get("name_alias") |
| 265 |
|
| 266 |
aci = ACIModule(module) |
| 267 |
|
| 268 |
child_classes = ["bgpRsBgpNodeCtxPol", "bgpRsBestPathCtrlPol"] |
| 269 |
|
| 270 |
aci.construct_url( |
| 271 |
root_class=dict( |
| 272 |
aci_class="fvTenant", |
| 273 |
aci_rn="tn-{0}".format(tenant), |
| 274 |
module_object=tenant, |
| 275 |
target_filter={"name": tenant}, |
| 276 |
), |
| 277 |
subclass_1=dict( |
| 278 |
aci_class="l3extOut", |
| 279 |
aci_rn="out-{0}".format(l3out), |
| 280 |
module_object=l3out, |
| 281 |
target_filter={"name": l3out}, |
| 282 |
), |
| 283 |
subclass_2=dict( |
| 284 |
aci_class="l3extLNodeP", |
| 285 |
aci_rn="lnodep-{0}".format(node_profile), |
| 286 |
module_object=node_profile, |
| 287 |
target_filter={"name": node_profile}, |
| 288 |
), |
| 289 |
subclass_3=dict( |
| 290 |
aci_class="bgpProtP", |
| 291 |
aci_rn="protp", |
| 292 |
module_object="", |
| 293 |
target_filter={"name": ""}, |
| 294 |
), |
| 295 |
child_classes=child_classes, |
| 296 |
) |
| 297 |
|
| 298 |
aci.get_existing() |
| 299 |
|
| 300 |
if state == "present": |
| 301 |
child_configs = [] |
| 302 |
if bgp_timers_policy is not None: |
| 303 |
child_configs.append(dict(bgpRsBgpNodeCtxPol=dict(attributes=dict(tnBgpCtxPolName=bgp_timers_policy)))) |
| 304 |
if bgp_best_path_policy is not None: |
| 305 |
child_configs.append(dict(bgpRsBestPathCtrlPol=dict(attributes=dict(tnBgpBestPathCtrlPolName=bgp_best_path_policy)))) |
| 306 |
|
| 307 |
aci.payload( |
| 308 |
aci_class="bgpProtP", |
| 309 |
class_config=dict( |
| 310 |
nameAlias=name_alias, |
| 311 |
), |
| 312 |
child_configs=child_configs, |
| 313 |
) |
| 314 |
|
| 315 |
aci.get_diff(aci_class="bgpProtP") |
| 316 |
|
| 317 |
aci.post_config() |
| 318 |
|
| 319 |
elif state == "absent": |
| 320 |
aci.delete_config() |
| 321 |
|
| 322 |
aci.exit_json() |
| 323 |
|
| 324 |
|
| 325 |
if __name__ == "__main__": |
| 326 |
main() |