2017-08-14 19:11:21 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-08-14 19:11:21 +00:00
|
|
|
'status': ['preview'],
|
2018-10-11 20:34:35 +00:00
|
|
|
'supported_by': 'certified'}
|
2017-08-14 19:11:21 +00:00
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
2017-08-17 00:27:42 +00:00
|
|
|
module: aci_vrf
|
2018-02-24 00:27:37 +00:00
|
|
|
short_description: Manage contexts or VRFs (fv:Ctx)
|
2017-08-14 19:11:21 +00:00
|
|
|
description:
|
2018-02-24 00:27:37 +00:00
|
|
|
- Manage contexts or VRFs on Cisco ACI fabrics.
|
2017-08-14 19:11:21 +00:00
|
|
|
- Each context is a private network associated to a tenant, i.e. VRF.
|
2018-02-21 18:35:59 +00:00
|
|
|
notes:
|
|
|
|
- The C(tenant) used must exist before using this module in your playbook.
|
|
|
|
The M(aci_tenant) module can be used for this.
|
2018-12-13 18:40:53 +00:00
|
|
|
seealso:
|
|
|
|
- module: aci_tenant
|
|
|
|
- name: APIC Management Information Model reference
|
|
|
|
description: More information about the internal APIC class B(fv:Ctx).
|
|
|
|
link: https://developer.cisco.com/docs/apic-mim-ref/
|
2017-08-14 19:11:21 +00:00
|
|
|
author:
|
|
|
|
- Jacob McGill (@jmcgill298)
|
|
|
|
version_added: '2.4'
|
|
|
|
options:
|
|
|
|
tenant:
|
|
|
|
description:
|
|
|
|
- The name of the Tenant the VRF should belong to.
|
2018-12-17 17:23:10 +00:00
|
|
|
type: str
|
2017-08-14 19:11:21 +00:00
|
|
|
aliases: [ tenant_name ]
|
|
|
|
vrf:
|
|
|
|
description:
|
|
|
|
- The name of the VRF.
|
2018-12-17 17:23:10 +00:00
|
|
|
type: str
|
2017-08-14 19:11:21 +00:00
|
|
|
aliases: [ context, name, vrf_name ]
|
|
|
|
policy_control_direction:
|
|
|
|
description:
|
|
|
|
- Determines if the policy should be enforced by the fabric on ingress or egress.
|
2018-12-17 17:23:10 +00:00
|
|
|
type: str
|
2017-08-14 19:11:21 +00:00
|
|
|
choices: [ egress, ingress ]
|
|
|
|
policy_control_preference:
|
|
|
|
description:
|
2018-07-03 12:27:48 +00:00
|
|
|
- Determines if the fabric should enforce contract policies to allow routing and packet forwarding.
|
2018-12-17 17:23:10 +00:00
|
|
|
type: str
|
2017-08-14 19:11:21 +00:00
|
|
|
choices: [ enforced, unenforced ]
|
|
|
|
description:
|
|
|
|
description:
|
2017-08-17 00:27:42 +00:00
|
|
|
- The description for the VRF.
|
2018-12-17 17:23:10 +00:00
|
|
|
type: str
|
2018-01-16 22:03:29 +00:00
|
|
|
aliases: [ descr ]
|
2017-08-14 19:11:21 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Use C(present) or C(absent) for adding or removing.
|
|
|
|
- Use C(query) for listing an object or multiple objects.
|
2018-12-17 17:23:10 +00:00
|
|
|
type: str
|
2017-08-14 19:11:21 +00:00
|
|
|
choices: [ absent, present, query ]
|
|
|
|
default: present
|
|
|
|
extends_documentation_fragment: aci
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Add a new VRF to a tenant
|
2017-08-17 00:27:42 +00:00
|
|
|
aci_vrf:
|
2018-01-22 21:06:56 +00:00
|
|
|
host: apic
|
2017-08-14 19:11:21 +00:00
|
|
|
username: admin
|
|
|
|
password: SomeSecretPassword
|
|
|
|
vrf: vrf_lab
|
|
|
|
tenant: lab_tenant
|
|
|
|
descr: Lab VRF
|
|
|
|
policy_control_preference: enforced
|
|
|
|
policy_control_direction: ingress
|
|
|
|
state: present
|
2018-08-08 21:24:50 +00:00
|
|
|
delegate_to: localhost
|
2017-08-14 19:11:21 +00:00
|
|
|
|
|
|
|
- name: Remove a VRF for a tenant
|
2017-08-17 00:27:42 +00:00
|
|
|
aci_vrf:
|
2018-01-22 21:06:56 +00:00
|
|
|
host: apic
|
2017-08-14 19:11:21 +00:00
|
|
|
username: admin
|
|
|
|
password: SomeSecretPassword
|
|
|
|
vrf: vrf_lab
|
|
|
|
tenant: lab_tenant
|
|
|
|
state: absent
|
2018-08-08 21:24:50 +00:00
|
|
|
delegate_to: localhost
|
2017-08-14 19:11:21 +00:00
|
|
|
|
|
|
|
- name: Query a VRF of a tenant
|
2017-08-17 00:27:42 +00:00
|
|
|
aci_vrf:
|
2018-01-22 21:06:56 +00:00
|
|
|
host: apic
|
2017-08-14 19:11:21 +00:00
|
|
|
username: admin
|
|
|
|
password: SomeSecretPassword
|
|
|
|
vrf: vrf_lab
|
|
|
|
tenant: lab_tenant
|
|
|
|
state: query
|
2018-08-08 21:24:50 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
register: query_result
|
2017-08-14 19:11:21 +00:00
|
|
|
|
|
|
|
- name: Query all VRFs
|
2017-08-17 00:27:42 +00:00
|
|
|
aci_vrf:
|
2018-01-22 21:06:56 +00:00
|
|
|
host: apic
|
2017-08-14 19:11:21 +00:00
|
|
|
username: admin
|
|
|
|
password: SomeSecretPassword
|
|
|
|
state: query
|
2018-08-08 21:24:50 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
register: query_result
|
2017-08-14 19:11:21 +00:00
|
|
|
'''
|
|
|
|
|
2018-02-02 23:41:56 +00:00
|
|
|
RETURN = r'''
|
|
|
|
current:
|
|
|
|
description: The existing configuration from the APIC after the module has finished
|
|
|
|
returned: success
|
|
|
|
type: list
|
|
|
|
sample:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"fvTenant": {
|
|
|
|
"attributes": {
|
|
|
|
"descr": "Production environment",
|
|
|
|
"dn": "uni/tn-production",
|
|
|
|
"name": "production",
|
|
|
|
"nameAlias": "",
|
|
|
|
"ownerKey": "",
|
|
|
|
"ownerTag": ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
error:
|
|
|
|
description: The error information as returned from the APIC
|
|
|
|
returned: failure
|
|
|
|
type: dict
|
|
|
|
sample:
|
|
|
|
{
|
|
|
|
"code": "122",
|
|
|
|
"text": "unknown managed object class foo"
|
|
|
|
}
|
|
|
|
raw:
|
|
|
|
description: The raw output returned by the APIC REST API (xml or json)
|
|
|
|
returned: parse error
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-02-02 23:41:56 +00:00
|
|
|
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
|
|
|
|
sent:
|
|
|
|
description: The actual/minimal configuration pushed to the APIC
|
|
|
|
returned: info
|
|
|
|
type: list
|
|
|
|
sample:
|
|
|
|
{
|
|
|
|
"fvTenant": {
|
|
|
|
"attributes": {
|
|
|
|
"descr": "Production environment"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
previous:
|
|
|
|
description: The original configuration from the APIC before the module has started
|
|
|
|
returned: info
|
|
|
|
type: list
|
|
|
|
sample:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"fvTenant": {
|
|
|
|
"attributes": {
|
|
|
|
"descr": "Production",
|
|
|
|
"dn": "uni/tn-production",
|
|
|
|
"name": "production",
|
|
|
|
"nameAlias": "",
|
|
|
|
"ownerKey": "",
|
|
|
|
"ownerTag": ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
proposed:
|
|
|
|
description: The assembled configuration from the user-provided parameters
|
|
|
|
returned: info
|
|
|
|
type: dict
|
|
|
|
sample:
|
|
|
|
{
|
|
|
|
"fvTenant": {
|
|
|
|
"attributes": {
|
|
|
|
"descr": "Production environment",
|
|
|
|
"name": "production"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filter_string:
|
|
|
|
description: The filter string used for the request
|
|
|
|
returned: failure or debug
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-02-02 23:41:56 +00:00
|
|
|
sample: ?rsp-prop-include=config-only
|
|
|
|
method:
|
|
|
|
description: The HTTP method used for the request to the APIC
|
|
|
|
returned: failure or debug
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-02-02 23:41:56 +00:00
|
|
|
sample: POST
|
|
|
|
response:
|
|
|
|
description: The HTTP response from the APIC
|
|
|
|
returned: failure or debug
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-02-02 23:41:56 +00:00
|
|
|
sample: OK (30 bytes)
|
|
|
|
status:
|
|
|
|
description: The HTTP status from the APIC
|
|
|
|
returned: failure or debug
|
|
|
|
type: int
|
|
|
|
sample: 200
|
|
|
|
url:
|
|
|
|
description: The HTTP url used for the request to the APIC
|
|
|
|
returned: failure or debug
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-02-02 23:41:56 +00:00
|
|
|
sample: https://10.11.12.13/api/mo/uni/tn-production.json
|
|
|
|
'''
|
2017-08-14 19:11:21 +00:00
|
|
|
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
|
2017-08-14 19:11:21 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2018-01-17 16:11:30 +00:00
|
|
|
argument_spec = aci_argument_spec()
|
2017-08-14 19:11:21 +00:00
|
|
|
argument_spec.update(
|
|
|
|
description=dict(type='str', aliases=['descr']),
|
|
|
|
policy_control_direction=dict(choices=['ingress', 'egress'], type='str'),
|
|
|
|
policy_control_preference=dict(choices=['enforced', 'unenforced'], type='str'),
|
|
|
|
state=dict(choices=['absent', 'present', 'query'], type='str', default='present'),
|
|
|
|
tenant=dict(type='str', required=False, aliases=['tenant_name']), # Not required for querying all objects
|
|
|
|
vrf=dict(type='str', required=False, aliases=['context', 'name', 'vrf_name']), # Not required for querying all objects
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
2017-08-25 06:35:45 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
required_if=[
|
|
|
|
['state', 'absent', ['tenant', 'vrf']],
|
|
|
|
['state', 'present', ['tenant', 'vrf']],
|
|
|
|
],
|
2017-08-14 19:11:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
description = module.params['description']
|
|
|
|
policy_control_direction = module.params['policy_control_direction']
|
|
|
|
policy_control_preference = module.params['policy_control_preference']
|
|
|
|
state = module.params['state']
|
2017-11-22 15:07:39 +00:00
|
|
|
tenant = module.params['tenant']
|
2017-08-14 19:11:21 +00:00
|
|
|
vrf = module.params['vrf']
|
|
|
|
|
|
|
|
aci = ACIModule(module)
|
2017-11-22 15:07:39 +00:00
|
|
|
aci.construct_url(
|
|
|
|
root_class=dict(
|
|
|
|
aci_class='fvTenant',
|
2018-01-12 07:07:59 +00:00
|
|
|
aci_rn='tn-{0}'.format(tenant),
|
2017-11-22 15:07:39 +00:00
|
|
|
module_object=tenant,
|
2018-08-07 21:54:54 +00:00
|
|
|
target_filter={'name': tenant},
|
2017-11-22 15:07:39 +00:00
|
|
|
),
|
|
|
|
subclass_1=dict(
|
|
|
|
aci_class='fvCtx',
|
2018-01-12 07:07:59 +00:00
|
|
|
aci_rn='ctx-{0}'.format(vrf),
|
2017-11-22 15:07:39 +00:00
|
|
|
module_object=vrf,
|
2018-08-07 21:54:54 +00:00
|
|
|
target_filter={'name': vrf},
|
2017-11-22 15:07:39 +00:00
|
|
|
),
|
|
|
|
)
|
2018-02-21 18:35:59 +00:00
|
|
|
|
2017-08-14 19:11:21 +00:00
|
|
|
aci.get_existing()
|
|
|
|
|
|
|
|
if state == 'present':
|
2017-08-25 06:35:45 +00:00
|
|
|
aci.payload(
|
|
|
|
aci_class='fvCtx',
|
|
|
|
class_config=dict(
|
|
|
|
descr=description,
|
|
|
|
pcEnfDir=policy_control_direction,
|
|
|
|
pcEnfPref=policy_control_preference,
|
|
|
|
name=vrf,
|
|
|
|
),
|
|
|
|
)
|
2017-08-14 19:11:21 +00:00
|
|
|
|
|
|
|
aci.get_diff(aci_class='fvCtx')
|
|
|
|
|
|
|
|
aci.post_config()
|
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
aci.delete_config()
|
|
|
|
|
2018-02-02 23:41:56 +00:00
|
|
|
aci.exit_json()
|
2017-08-14 19:11:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|