def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        contract=dict(type='str',
                      aliases=['contract_name'
                               ]),  # Not required for querying all objects
        subject=dict(type='str',
                     aliases=['contract_subject', 'name', 'subject_name'
                              ]),  # Not required for querying all objects
        tenant=dict(type='str',
                    aliases=['tenant_name'
                             ]),  # Not required for querying all objects
        priority=dict(type='str',
                      choices=['unspecified', 'level1', 'level2', 'level3']),
        reverse_filter=dict(type='bool'),
        dscp=dict(type='str',
                  aliases=['target'],
                  choices=[
                      'AF11', 'AF12', 'AF13', 'AF21', 'AF22', 'AF23', 'AF31',
                      'AF32', 'AF33', 'AF41', 'AF42', 'AF43', 'CS0', 'CS1',
                      'CS2', 'CS3', 'CS4', 'CS5', 'CS6', 'CS7', 'EF', 'VA',
                      'unspecified'
                  ]),
        description=dict(type='str', aliases=['descr']),
        consumer_match=dict(
            type='str', choices=['all', 'at_least_one', 'at_most_one',
                                 'none']),
        provider_match=dict(
            type='str', choices=['all', 'at_least_one', 'at_most_one',
                                 'none']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['contract', 'subject', 'tenant']],
            ['state', 'present', ['contract', 'subject', 'tenant']],
        ],
    )

    aci = ACIModule(module)

    subject = module.params.get('subject')
    priority = module.params.get('priority')
    reverse_filter = aci.boolean(module.params.get('reverse_filter'))
    contract = module.params.get('contract')
    dscp = module.params.get('dscp')
    description = module.params.get('description')
    consumer_match = module.params.get('consumer_match')
    if consumer_match is not None:
        consumer_match = MATCH_MAPPING.get(consumer_match)
    provider_match = module.params.get('provider_match')
    if provider_match is not None:
        provider_match = MATCH_MAPPING.get(provider_match)
    state = module.params.get('state')
    tenant = module.params.get('tenant')

    aci.construct_url(
        root_class=dict(
            aci_class='fvTenant',
            aci_rn='tn-{0}'.format(tenant),
            module_object=tenant,
            target_filter={'name': tenant},
        ),
        subclass_1=dict(
            aci_class='vzBrCP',
            aci_rn='brc-{0}'.format(contract),
            module_object=contract,
            target_filter={'name': contract},
        ),
        subclass_2=dict(
            aci_class='vzSubj',
            aci_rn='subj-{0}'.format(subject),
            module_object=subject,
            target_filter={'name': subject},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='vzSubj',
            class_config=dict(
                name=subject,
                prio=priority,
                revFltPorts=reverse_filter,
                targetDscp=dscp,
                consMatchT=consumer_match,
                provMatchT=provider_match,
                descr=description,
            ),
        )

        aci.get_diff(aci_class='vzSubj')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        tenant=dict(type='str', aliases=['tenant_name']),  # Not required for querying all objects
        ospf=dict(type='str', aliases=['ospf_interface', 'name']),  # Not required for querying all objects
        description=dict(type='str', aliases=['descr']),
        network_type=dict(type='str', choices=['bcast', 'p2p']),
        cost=dict(type='int'),
        controls=dict(type='list', choices=['advert-subnet', 'bfd', 'mtu-ignore', 'passive']),
        dead_interval=dict(type='int'),
        hello_interval=dict(type='int'),
        prefix_suppression=dict(type='bool'),
        priority=dict(type='int'),
        retransmit_interval=dict(type='int'),
        transmit_delay=dict(type='int'),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['ospf', 'tenant']],
            ['state', 'present', ['ospf', 'tenant']],
        ],
    )

    aci = ACIModule(module)

    tenant = module.params.get('tenant')
    ospf = module.params.get('ospf')
    description = module.params.get('description')

    if module.params.get('controls') is None:
        controls = None
    else:
        controls = ','.join(module.params.get('controls'))

    cost = module.params.get('cost')
    if cost is not None and cost not in range(1, 451):
        module.fail_json(msg="Parameter 'cost' is only valid in range between 1 and 450.")

    dead_interval = module.params.get('dead_interval')
    if dead_interval is not None and dead_interval not in range(1, 65536):
        module.fail_json(msg="Parameter 'dead_interval' is only valid in range between 1 and 65536.")

    hello_interval = module.params.get('hello_interval')
    if hello_interval is not None and hello_interval not in range(1, 65536):
        module.fail_json(msg="Parameter 'hello_interval' is only valid in range between 1 and 65536.")

    network_type = module.params.get('network_type')
    prefix_suppression = aci.boolean(module.params.get('prefix_suppression'), 'enabled', 'disabled')
    priority = module.params.get('priority')
    if priority is not None and priority not in range(0, 256):
        module.fail_json(msg="Parameter 'priority' is only valid in range between 1 and 255.")

    retransmit_interval = module.params.get('retransmit_interval')
    if retransmit_interval is not None and retransmit_interval not in range(1, 65536):
        module.fail_json(msg="Parameter 'retransmit_interval' is only valid in range between 1 and 65536.")

    transmit_delay = module.params.get('transmit_delay')
    if transmit_delay is not None and transmit_delay not in range(1, 451):
        module.fail_json(msg="Parameter 'transmit_delay' is only valid in range between 1 and 450.")

    state = module.params.get('state')

    aci.construct_url(
        root_class=dict(
            aci_class='ospfIfPol',
            aci_rn='tn-{0}/ospfIfPol-{1}'.format(tenant, ospf),
            module_object=ospf,
            target_filter={'name': ospf},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='ospfIfPol',
            class_config=dict(
                name=ospf,
                descr=description,
                cost=cost,
                ctrl=controls,
                deadIntvl=dead_interval,
                helloIntvl=hello_interval,
                nwT=network_type,
                pfxSuppress=prefix_suppression,
                prio=priority,
                rexmitIntvl=retransmit_interval,
                xmitDelay=transmit_delay,
            ),
        )

        aci.get_diff(aci_class='ospfIfPol')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #3
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        contract=dict(type='str',
                      aliases=['contract_name'
                               ]),  # Not required for querying all objects
        filter=dict(type='str',
                    aliases=['filter_name'
                             ]),  # Not required for querying all objects
        subject=dict(type='str',
                     aliases=['contract_subject', 'subject_name'
                              ]),  # Not required for querying all objects
        tenant=dict(type='str',
                    aliases=['tenant_name'
                             ]),  # Not required for querying all objects
        log=dict(type='str', choices=['log', 'none'], aliases=['directive']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['contract', 'filter', 'subject', 'tenant']],
            ['state', 'present', ['contract', 'filter', 'subject', 'tenant']],
        ],
    )

    contract = module.params.get('contract')
    filter_name = module.params.get('filter')
    log = module.params.get('log')
    subject = module.params.get('subject')
    tenant = module.params.get('tenant')
    state = module.params.get('state')

    # Add subject_filter key to modul.params for building the URL
    module.params['subject_filter'] = filter_name

    # Convert log to empty string if none, as that is what API expects. An empty string is not a good option to present the user.
    if log == 'none':
        log = ''

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='fvTenant',
            aci_rn='tn-{0}'.format(tenant),
            module_object=tenant,
            target_filter={'name': tenant},
        ),
        subclass_1=dict(
            aci_class='vzBrCP',
            aci_rn='brc-{0}'.format(contract),
            module_object=contract,
            target_filter={'name': contract},
        ),
        subclass_2=dict(
            aci_class='vzSubj',
            aci_rn='subj-{0}'.format(subject),
            module_object=subject,
            target_filter={'name': subject},
        ),
        subclass_3=dict(
            aci_class='vzRsSubjFiltAtt',
            aci_rn='rssubjFiltAtt-{0}'.format(filter_name),
            module_object=filter_name,
            target_filter={'tnVzFilterName': filter_name},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='vzRsSubjFiltAtt',
            class_config=dict(
                tnVzFilterName=filter_name,
                directives=log,
            ),
        )

        aci.get_diff(aci_class='vzRsSubjFiltAtt')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    # Remove subject_filter used to build URL from module.params
    module.params.pop('subject_filter')

    aci.exit_json()
예제 #4
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        protection_group=dict(type='str', aliases=['name', 'protection_group_name']),  # Not required for querying all objects
        protection_group_id=dict(type='int', aliases=['id']),
        vpc_domain_policy=dict(type='str', aliases=['vpc_domain_policy_name']),
        switch_1_id=dict(type='int'),
        switch_2_id=dict(type='int'),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['protection_group']],
            ['state', 'present', ['protection_group', 'protection_group_id', 'switch_1_id', 'switch_2_id']],
        ],
    )

    protection_group = module.params.get('protection_group')
    protection_group_id = module.params.get('protection_group_id')
    vpc_domain_policy = module.params.get('vpc_domain_policy')
    switch_1_id = module.params.get('switch_1_id')
    switch_2_id = module.params.get('switch_2_id')
    state = module.params.get('state')

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='fabricExplicitGEp',
            aci_rn='fabric/protpol/expgep-{0}'.format(protection_group),
            module_object=protection_group,
            target_filter={'name': protection_group},
        ),
        child_classes=['fabricNodePEp', 'fabricNodePEp', 'fabricRsVpcInstPol'],
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='fabricExplicitGEp',
            class_config=dict(
                name=protection_group,
                id=protection_group_id,
            ),
            child_configs=[
                dict(
                    fabricNodePEp=dict(
                        attributes=dict(
                            id='{0}'.format(switch_1_id),
                        ),
                    ),
                ),
                dict(
                    fabricNodePEp=dict(
                        attributes=dict(
                            id='{0}'.format(switch_2_id),
                        ),
                    ),
                ),
                dict(
                    fabricRsVpcInstPol=dict(
                        attributes=dict(
                            tnVpcInstPolName=vpc_domain_policy,
                        ),
                    ),
                ),
            ],
        )

        aci.get_diff(aci_class='fabricExplicitGEp')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        pool=dict(type='str',
                  aliases=['name', 'pool_name'
                           ]),  # Not required for querying all objects
        description=dict(type='str', aliases=['descr']),
        pool_allocation_mode=dict(type='str',
                                  aliases=['allocation_mode', 'mode'],
                                  choices=['dynamic', 'static']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['pool']],
            ['state', 'present', ['pool']],
        ],
    )

    description = module.params.get('description')
    pool = module.params.get('pool')
    pool_allocation_mode = module.params.get('pool_allocation_mode')
    state = module.params.get('state')

    pool_name = pool

    # ACI Pool URL requires the allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
    if pool is not None:
        if pool_allocation_mode is not None:
            pool_name = '[{0}]-{1}'.format(pool, pool_allocation_mode)
        else:
            module.fail_json(
                msg=
                "ACI requires the 'pool_allocation_mode' when 'pool' is provided"
            )

    aci = ACIModule(module)
    aci.construct_url(root_class=dict(
        aci_class='fvnsVlanInstP',
        aci_rn='infra/vlanns-{0}'.format(pool_name),
        module_object=pool,
        target_filter={'name': pool},
    ), )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='fvnsVlanInstP',
            class_config=dict(
                allocMode=pool_allocation_mode,
                descr=description,
                name=pool,
            ),
        )

        aci.get_diff(aci_class='fvnsVlanInstP')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #6
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        allow_useg=dict(type='str', choices=['encap', 'useg']),
        ap=dict(type='str',
                aliases=['app_profile', 'app_profile_name'
                         ]),  # Not required for querying all objects
        deploy_immediacy=dict(type='str', choices=['immediate', 'lazy']),
        domain=dict(type='str',
                    aliases=['domain_name', 'domain_profile'
                             ]),  # Not required for querying all objects
        domain_type=dict(type='str',
                         choices=['l2dom', 'phys', 'vmm'],
                         aliases=['type'
                                  ]),  # Not required for querying all objects
        encap=dict(type='int'),
        encap_mode=dict(type='str', choices=['auto', 'vlan', 'vxlan']),
        switching_mode=dict(type='str',
                            default='native',
                            choices=['AVE', 'native']),
        epg=dict(type='str',
                 aliases=['name', 'epg_name'
                          ]),  # Not required for querying all objects
        netflow=dict(type='bool'),
        primary_encap=dict(type='int'),
        resolution_immediacy=dict(
            type='str', choices=['immediate', 'lazy', 'pre-provision']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
        tenant=dict(type='str',
                    aliases=['tenant_name'
                             ]),  # Not required for querying all objects
        vm_provider=dict(type='str',
                         choices=[
                             'cloudfoundry', 'kubernetes', 'microsoft',
                             'openshift', 'openstack', 'redhat', 'vmware'
                         ]),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['domain_type', 'vmm', ['vm_provider']],
            [
                'state', 'absent',
                ['ap', 'domain', 'domain_type', 'epg', 'tenant']
            ],
            [
                'state', 'present',
                ['ap', 'domain', 'domain_type', 'epg', 'tenant']
            ],
        ],
    )

    aci = ACIModule(module)

    allow_useg = module.params.get('allow_useg')
    ap = module.params.get('ap')
    deploy_immediacy = module.params.get('deploy_immediacy')
    domain = module.params.get('domain')
    domain_type = module.params.get('domain_type')
    vm_provider = module.params.get('vm_provider')
    encap = module.params.get('encap')
    if encap is not None:
        if encap in range(1, 4097):
            encap = 'vlan-{0}'.format(encap)
        else:
            module.fail_json(msg='Valid VLAN assignments are from 1 to 4096')
    encap_mode = module.params.get('encap_mode')
    switching_mode = module.params.get('switching_mode')
    epg = module.params.get('epg')
    netflow = aci.boolean(module.params.get('netflow'), 'enabled', 'disabled')
    primary_encap = module.params.get('primary_encap')
    if primary_encap is not None:
        if primary_encap in range(1, 4097):
            primary_encap = 'vlan-{0}'.format(primary_encap)
        else:
            module.fail_json(msg='Valid VLAN assignments are from 1 to 4096')
    resolution_immediacy = module.params.get('resolution_immediacy')
    state = module.params.get('state')
    tenant = module.params.get('tenant')

    if domain_type in ['l2dom', 'phys'] and vm_provider is not None:
        module.fail_json(msg="Domain type '%s' cannot have a 'vm_provider'" %
                         domain_type)

    # Compile the full domain for URL building
    if domain_type == 'vmm':
        epg_domain = 'uni/vmmp-{0}/dom-{1}'.format(
            VM_PROVIDER_MAPPING[vm_provider], domain)
    elif domain_type == 'l2dom':
        epg_domain = 'uni/l2dom-{0}'.format(domain)
    elif domain_type == 'phys':
        epg_domain = 'uni/phys-{0}'.format(domain)
    else:
        epg_domain = None

    aci.construct_url(
        root_class=dict(
            aci_class='fvTenant',
            aci_rn='tn-{0}'.format(tenant),
            module_object=tenant,
            target_filter={'name': tenant},
        ),
        subclass_1=dict(
            aci_class='fvAp',
            aci_rn='ap-{0}'.format(ap),
            module_object=ap,
            target_filter={'name': ap},
        ),
        subclass_2=dict(
            aci_class='fvAEPg',
            aci_rn='epg-{0}'.format(epg),
            module_object=epg,
            target_filter={'name': epg},
        ),
        subclass_3=dict(
            aci_class='fvRsDomAtt',
            aci_rn='rsdomAtt-[{0}]'.format(epg_domain),
            module_object=epg_domain,
            target_filter={'tDn': epg_domain},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='fvRsDomAtt',
            class_config=dict(
                classPref=allow_useg,
                encap=encap,
                encapMode=encap_mode,
                switchingMode=switching_mode,
                instrImedcy=deploy_immediacy,
                netflowPref=netflow,
                primaryEncap=primary_encap,
                resImedcy=resolution_immediacy,
            ),
        )

        aci.get_diff(aci_class='fvRsDomAtt')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #7
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        aaa_password=dict(type='str', no_log=True),
        aaa_password_lifetime=dict(type='int'),
        aaa_password_update_required=dict(type='bool'),
        aaa_user=dict(type='str', aliases=['name']),  # Not required for querying all objects
        clear_password_history=dict(type='bool'),
        description=dict(type='str', aliases=['descr']),
        email=dict(type='str'),
        enabled=dict(type='bool'),
        expiration=dict(type='str'),
        expires=dict(type='bool'),
        first_name=dict(type='str'),
        last_name=dict(type='str'),
        phone=dict(type='str'),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['aaa_user']],
            ['state', 'present', ['aaa_user']],
            ['expires', True, ['expiration']],
        ],
    )

    aci = ACIModule(module)

    if not HAS_DATEUTIL:
        module.fail_json(msg='dateutil required for this module')

    aaa_password = module.params.get('aaa_password')
    aaa_password_lifetime = module.params.get('aaa_password_lifetime')
    aaa_password_update_required = aci.boolean(module.params.get('aaa_password_update_required'))
    aaa_user = module.params.get('aaa_user')
    clear_password_history = aci.boolean(module.params.get('clear_password_history'), 'yes', 'no')
    description = module.params.get('description')
    email = module.params.get('email')
    enabled = aci.boolean(module.params.get('enabled'), 'active', 'inactive')
    expires = aci.boolean(module.params.get('expires'))
    first_name = module.params.get('first_name')
    last_name = module.params.get('last_name')
    phone = module.params.get('phone')
    state = module.params.get('state')

    expiration = module.params.get('expiration')
    if expiration is not None and expiration != 'never':
        try:
            expiration = aci.iso8601_format(dateutil.parser.parse(expiration).replace(tzinfo=tzutc()))
        except Exception as e:
            module.fail_json(msg="Failed to parse date format '%s', %s" % (module.params.get('expiration'), e))

    aci.construct_url(
        root_class=dict(
            aci_class='aaaUser',
            aci_rn='userext/user-{0}'.format(aaa_user),
            module_object=aaa_user,
            target_filter={'name': aaa_user},
        ),
    )
    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='aaaUser',
            class_config=dict(
                accountStatus=enabled,
                clearPwdHistory=clear_password_history,
                descr=description,
                email=email,
                expiration=expiration,
                expires=expires,
                firstName=first_name,
                lastName=last_name,
                name=aaa_user,
                phone=phone,
                pwd=aaa_password,
                pwdLifeTime=aaa_password_lifetime,
                pwdUpdateRequired=aaa_password_update_required,
            ),
        )

        aci.get_diff(aci_class='aaaUser')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #8
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        source=dict(type='str', aliases=['name', 'source_name']),  # Not required for querying all objects
        polling_interval=dict(type='int'),
        url=dict(type='str'),
        url_username=dict(type='str'),
        url_password=dict(type='str', no_log=True),
        url_protocol=dict(type='str', default='scp', choices=['http', 'local', 'scp', 'usbkey'], aliases=['url_proto']),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['source']],
            ['state', 'present', ['url_protocol', 'source', 'url']],
        ],
    )

    polling_interval = module.params.get('polling_interval')
    url_protocol = module.params.get('url_protocol')
    state = module.params.get('state')
    source = module.params.get('source')
    url = module.params.get('url')
    url_password = module.params.get('url_password')
    url_username = module.params.get('url_username')

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='firmwareOSource',
            aci_rn='fabric/fwrepop',
            module_object=source,
            target_filter={'name': source},
        ),
    )
    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='firmwareOSource',
            class_config=dict(
                name=source,
                url=url,
                password=url_password,
                pollingInterval=polling_interval,
                proto=url_protocol,
                user=url_username,
            ),
        )

        aci.get_diff(aci_class='firmwareOSource')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        pool_type=dict(type='str',
                       required=True,
                       aliases=['type'],
                       choices=['vlan', 'vsan', 'vxlan']),
        description=dict(type='str', aliases=['descr']),
        pool=dict(type='str',
                  aliases=['name', 'pool_name'
                           ]),  # Not required for querying all objects
        pool_allocation_mode=dict(type='str',
                                  aliases=['allocation_mode', 'mode'],
                                  choices=['dynamic', 'static']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['pool']],
            ['state', 'present', ['pool']],
        ],
    )

    description = module.params.get('description')
    pool = module.params.get('pool')
    pool_type = module.params.get('pool_type')
    pool_allocation_mode = module.params.get('pool_allocation_mode')
    state = module.params.get('state')

    aci_class = ACI_POOL_MAPPING[pool_type]['aci_class']
    aci_mo = ACI_POOL_MAPPING[pool_type]['aci_mo']
    pool_name = pool

    # ACI Pool URL requires the pool_allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
    if pool_type != 'vxlan' and pool is not None:
        if pool_allocation_mode is not None:
            pool_name = '[{0}]-{1}'.format(pool, pool_allocation_mode)
        else:
            module.fail_json(
                msg=
                "ACI requires parameter 'pool_allocation_mode' for 'pool_type' of 'vlan' and 'vsan' when parameter 'pool' is provided"
            )

    # Vxlan pools do not support pool allocation modes
    if pool_type == 'vxlan' and pool_allocation_mode is not None:
        module.fail_json(
            msg=
            "vxlan pools do not support setting the 'pool_allocation_mode'; please remove this parameter from the task"
        )

    aci = ACIModule(module)
    aci.construct_url(root_class=dict(
        aci_class=aci_class,
        aci_rn='{0}{1}'.format(aci_mo, pool_name),
        module_object=pool,
        target_filter={'name': pool},
    ), )

    aci.get_existing()

    if state == 'present':
        # Filter out module parameters with null values
        aci.payload(aci_class=aci_class,
                    class_config=dict(
                        allocMode=pool_allocation_mode,
                        descr=description,
                        name=pool,
                    ))

        # Generate config diff which will be used as POST request body
        aci.get_diff(aci_class=aci_class)

        # Submit changes if module not in check_mode and the proposed is different than existing
        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #10
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        description=dict(type='str', aliases=['descr']),
        node_id=dict(type='int'),  # Not required for querying all objects
        pod_id=dict(type='int'),
        role=dict(type='str',
                  choices=['leaf', 'spine', 'unspecified'],
                  aliases=['role_name']),
        serial=dict(type='str',
                    aliases=['serial_number'
                             ]),  # Not required for querying all objects
        switch=dict(type='str', aliases=['name', 'switch_name']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['node_id', 'serial']],
            ['state', 'present', ['node_id', 'serial']],
        ],
    )

    pod_id = module.params.get('pod_id')
    serial = module.params.get('serial')
    node_id = module.params.get('node_id')
    switch = module.params.get('switch')
    description = module.params.get('description')
    role = module.params.get('role')
    state = module.params.get('state')

    aci = ACIModule(module)
    aci.construct_url(root_class=dict(
        aci_class='fabricNodeIdentP',
        aci_rn='controller/nodeidentpol/nodep-{0}'.format(serial),
        module_object=serial,
        target_filter={'serial': serial},
    ))

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='fabricNodeIdentP',
            class_config=dict(
                descr=description,
                name=switch,
                nodeId=node_id,
                podId=pod_id,
                # NOTE: Originally we were sending 'rn', but now we need 'dn' for idempotency
                # FIXME: Did this change with ACI version ?
                dn='uni/controller/nodeidentpol/nodep-{0}'.format(serial),
                # rn='nodep-{0}'.format(serial),
                role=role,
                serial=serial,
            ))

        aci.get_diff(aci_class='fabricNodeIdentP')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json(**aci.result)
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update({
        'description': dict(type='str'),
        'leaf_profile': dict(type='str', aliases=['leaf_profile_name']),  # Not required for querying all objects
        'leaf': dict(type='str', aliases=['name', 'leaf_name', 'leaf_profile_leaf_name', 'leaf_selector_name']),  # Not required for querying all objects
        'leaf_node_blk': dict(type='str', aliases=['leaf_node_blk_name', 'node_blk_name']),
        'leaf_node_blk_description': dict(type='str'),
        # NOTE: Keyword 'from' is a reserved word in python, so we need it as a string
        'from': dict(type='int', aliases=['node_blk_range_from', 'from_range', 'range_from']),
        'to': dict(type='int', aliases=['node_blk_range_to', 'to_range', 'range_to']),
        'policy_group': dict(type='str', aliases=['policy_group_name']),
        'state': dict(type='str', default='present', choices=['absent', 'present', 'query']),
    })

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['leaf_profile', 'leaf']],
            ['state', 'present', ['leaf_profile', 'leaf', 'leaf_node_blk', 'from', 'to']]
        ]
    )

    description = module.params.get('description')
    leaf_profile = module.params.get('leaf_profile')
    leaf = module.params.get('leaf')
    leaf_node_blk = module.params.get('leaf_node_blk')
    leaf_node_blk_description = module.params.get('leaf_node_blk_description')
    from_ = module.params.get('from')
    to_ = module.params.get('to')
    policy_group = module.params.get('policy_group')
    state = module.params.get('state')

    # Build child_configs dynamically
    child_configs = [
        dict(
            infraNodeBlk=dict(
                attributes=dict(
                    descr=leaf_node_blk_description,
                    name=leaf_node_blk,
                    from_=from_,
                    to_=to_,
                ),
            ),
        ),
    ]

    # Add infraRsAccNodePGrp only when policy_group was defined
    if policy_group is not None:
        child_configs.append(dict(
            infraRsAccNodePGrp=dict(
                attributes=dict(
                    tDn='uni/infra/funcprof/accnodepgrp-{0}'.format(policy_group),
                ),
            ),
        ))

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='infraNodeP',
            aci_rn='infra/nprof-{0}'.format(leaf_profile),
            module_object=leaf_profile,
            target_filter={'name': leaf_profile},
        ),
        subclass_1=dict(
            aci_class='infraLeafS',
            # NOTE: normal rn: leaves-{name}-typ-{type}, hence here hardcoded to range for purposes of module
            aci_rn='leaves-{0}-typ-range'.format(leaf),
            module_object=leaf,
            target_filter={'name': leaf},
        ),
        # NOTE: infraNodeBlk is not made into a subclass because there is a 1-1 mapping between node block and leaf selector name
        child_classes=['infraNodeBlk', 'infraRsAccNodePGrp'],

    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='infraLeafS',
            class_config=dict(
                descr=description,
                name=leaf,
            ),
            child_configs=child_configs,
        )

        aci.get_diff(aci_class='infraLeafS')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #12
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        pool=dict(type='str', aliases=['pool_name']),  # Not required for querying all objects
        block_name=dict(type='str', aliases=['name']),  # Not required for querying all objects
        block_end=dict(type='int', aliases=['end']),  # Not required for querying all objects
        block_start=dict(type='int', aliases=["start"]),  # Not required for querying all objects
        allocation_mode=dict(type='str', aliases=['mode'], choices=['dynamic', 'inherit', 'static']),
        description=dict(type='str', aliases=['descr']),
        pool_allocation_mode=dict(type='str', aliases=['pool_mode'], choices=['dynamic', 'static']),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['pool', 'block_end', 'block_name', 'block_start']],
            ['state', 'present', ['pool', 'block_end', 'block_name', 'block_start']],
        ],
    )

    allocation_mode = module.params.get('allocation_mode')
    description = module.params.get('description')
    pool = module.params.get('pool')
    pool_allocation_mode = module.params.get('pool_allocation_mode')
    block_end = module.params.get('block_end')
    block_name = module.params.get('block_name')
    block_start = module.params.get('block_start')
    state = module.params.get('state')

    if block_end is not None:
        encap_end = 'vlan-{0}'.format(block_end)
    else:
        encap_end = None

    if block_start is not None:
        encap_start = 'vlan-{0}'.format(block_start)
    else:
        encap_start = None

    # Collect proper mo information
    aci_block_mo = 'from-[{0}]-to-[{1}]'.format(encap_start, encap_end)
    pool_name = pool

    # Validate block_end and block_start are valid for its respective encap type
    for encap_id in block_end, block_start:
        if encap_id is not None:
            if not 1 <= encap_id <= 4094:
                module.fail_json(msg="vlan pools must have 'block_start' and 'block_end' values between 1 and 4094")

    if block_end is not None and block_start is not None:
        # Validate block_start is less than block_end
        if block_start > block_end:
            module.fail_json(msg="The 'block_start' must be less than or equal to the 'block_end'")

    elif block_end is None and block_start is None:
        if block_name is None:
            # Reset range managed object to None for aci util to properly handle query
            aci_block_mo = None

    # ACI Pool URL requires the allocation mode (ex: uni/infra/vlanns-[poolname]-static)
    if pool is not None:
        if pool_allocation_mode is not None:
            pool_name = '[{0}]-{1}'.format(pool, pool_allocation_mode)
        else:
            module.fail_json(msg="ACI requires the 'pool_allocation_mode' when 'pool' is provided")

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='fvnsVlanInstP',
            aci_rn='infra/vlanns-{0}'.format(pool_name),
            module_object=pool,
            target_filter={'name': pool},
        ),
        subclass_1=dict(
            aci_class='fvnsEncapBlk',
            aci_rn=aci_block_mo,
            module_object=aci_block_mo,
            target_filter={'from': encap_start, 'to': encap_end, 'name': block_name},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='fvnsEncapBlk',
            class_config={
                "allocMode": allocation_mode,
                "descr": description,
                "from": encap_start,
                "name": block_name,
                "to": encap_end,
            },
        )

        aci.get_diff(aci_class='fvnsEncapBlk')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #13
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        tenant=dict(type='str',
                    aliases=['tenant_name'
                             ]),  # Not required for querying all objects
        ap=dict(type='str',
                aliases=['app_profile', 'app_profile_name'
                         ]),  # Not required for querying all objects
        epg=dict(type='str',
                 aliases=['epg_name'
                          ]),  # Not required for querying all objects
        description=dict(type='str', aliases=['descr']),
        encap_id=dict(type='int', aliases=['vlan', 'vlan_id']),
        primary_encap_id=dict(type='int',
                              aliases=['primary_vlan', 'primary_vlan_id']),
        deploy_immediacy=dict(type='str', choices=['immediate', 'lazy']),
        interface_mode=dict(type='str',
                            choices=[
                                '802.1p', 'access', 'native', 'regular',
                                'tagged', 'trunk', 'untagged'
                            ],
                            aliases=['interface_mode_name', 'mode']),
        interface_type=dict(
            type='str',
            default='switch_port',
            choices=['fex', 'port_channel', 'switch_port', 'vpc']),
        pod_id=dict(type='int',
                    aliases=['pod', 'pod_number'
                             ]),  # Not required for querying all objects
        leafs=dict(type='list',
                   aliases=['leaves', 'nodes', 'paths', 'switches'
                            ]),  # Not required for querying all objects
        interface=dict(type='str'),  # Not required for querying all objects
        extpaths=dict(type='int'),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['interface_type', 'fex', ['extpaths']],
            [
                'state', 'absent',
                ['ap', 'epg', 'interface', 'leafs', 'pod_id', 'tenant']
            ],
            [
                'state', 'present',
                [
                    'ap', 'encap_id', 'epg', 'interface', 'leafs', 'pod_id',
                    'tenant'
                ]
            ],
        ],
    )

    tenant = module.params.get('tenant')
    ap = module.params.get('ap')
    epg = module.params.get('epg')
    description = module.params.get('description')
    encap_id = module.params.get('encap_id')
    primary_encap_id = module.params.get('primary_encap_id')
    deploy_immediacy = module.params.get('deploy_immediacy')
    interface_mode = module.params.get('interface_mode')
    interface_type = module.params.get('interface_type')
    pod_id = module.params.get('pod_id')
    leafs = module.params.get('leafs')
    if leafs is not None:
        # Process leafs, and support dash-delimited leafs
        leafs = []
        for leaf in module.params.get('leafs'):
            # Users are likely to use integers for leaf IDs, which would raise an exception when using the join method
            leafs.extend(str(leaf).split('-'))
        if len(leafs) == 1:
            if interface_type == 'vpc':
                module.fail_json(
                    msg='A interface_type of "vpc" requires 2 leafs')
            leafs = leafs[0]
        elif len(leafs) == 2:
            if interface_type != 'vpc':
                module.fail_json(
                    msg=
                    'The interface_types "switch_port", "port_channel", and "fex" \
                    do not support using multiple leafs for a single binding')
            leafs = "-".join(leafs)
        else:
            module.fail_json(
                msg='The "leafs" parameter must not have more than 2 entries')
    interface = module.params.get('interface')
    extpaths = module.params.get('extpaths')
    state = module.params.get('state')

    if encap_id is not None:
        if encap_id not in range(1, 4097):
            module.fail_json(msg='Valid VLAN assigments are from 1 to 4096')
        encap_id = 'vlan-{0}'.format(encap_id)

    if primary_encap_id is not None:
        if primary_encap_id not in range(1, 4097):
            module.fail_json(msg='Valid VLAN assigments are from 1 to 4096')
        primary_encap_id = 'vlan-{0}'.format(primary_encap_id)

    static_path = INTERFACE_TYPE_MAPPING[interface_type].format(
        pod_id=pod_id, leafs=leafs, extpaths=extpaths, interface=interface)

    path_target_filter = {}
    if pod_id is not None and leafs is not None and interface is not None and (
            interface_type != 'fex' or extpaths is not None):
        path_target_filter = {'tDn': static_path}

    if interface_mode is not None:
        interface_mode = INTERFACE_MODE_MAPPING[interface_mode]

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='fvTenant',
            aci_rn='tn-{0}'.format(tenant),
            module_object=tenant,
            target_filter={'name': tenant},
        ),
        subclass_1=dict(
            aci_class='fvAp',
            aci_rn='ap-{0}'.format(ap),
            module_object=ap,
            target_filter={'name': ap},
        ),
        subclass_2=dict(
            aci_class='fvAEPg',
            aci_rn='epg-{0}'.format(epg),
            module_object=epg,
            target_filter={'name': epg},
        ),
        subclass_3=dict(
            aci_class='fvRsPathAtt',
            aci_rn='rspathAtt-[{0}]'.format(static_path),
            module_object=static_path,
            target_filter=path_target_filter,
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='fvRsPathAtt',
            class_config=dict(
                descr=description,
                encap=encap_id,
                primaryEncap=primary_encap_id,
                instrImedcy=deploy_immediacy,
                mode=interface_mode,
                tDn=static_path,
            ),
        )

        aci.get_diff(aci_class='fvRsPathAtt')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        bd=dict(type='str',
                aliases=['bd_name', 'bridge_domain'
                         ]),  # Not required for querying all objects
        l3out=dict(type='str'),  # Not required for querying all objects
        tenant=dict(type='str',
                    aliases=['tenant_name'
                             ]),  # Not required for querying all objects
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_together=[['gateway', 'mask']],
        required_if=[
            ['state', 'present', ['bd', 'l3out', 'tenant']],
            ['state', 'absent', ['bd', 'l3out', 'tenant']],
        ],
    )

    bd = module.params.get('bd')
    l3out = module.params.get('l3out')
    state = module.params.get('state')
    tenant = module.params.get('tenant')

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='fvTenant',
            aci_rn='tn-{0}'.format(tenant),
            module_object=tenant,
            target_filter={'name': tenant},
        ),
        subclass_1=dict(
            aci_class='fvBD',
            aci_rn='BD-{0}'.format(bd),
            module_object=bd,
            target_filter={'name': bd},
        ),
        subclass_2=dict(
            aci_class='fvRsBDToOut',
            aci_rn='rsBDToOut-{0}'.format(l3out),
            module_object=l3out,
            target_filter={'tnL3extOutName': l3out},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='fvRsBDToOut',
            class_config=dict(tnL3extOutName=l3out),
        )

        aci.get_diff(aci_class='fvRsBDToOut')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        contract=dict(type='str', aliases=['contract_name', 'name']),  # Not required for querying all objects
        tenant=dict(type='str', aliases=['tenant_name']),  # Not required for querying all objects
        description=dict(type='str', aliases=['descr']),
        scope=dict(type='str', choices=['application-profile', 'context', 'global', 'tenant']),
        priority=dict(type='str', choices=['level1', 'level2', 'level3', 'unspecified']),  # No default provided on purpose
        dscp=dict(type='str',
                  choices=['AF11', 'AF12', 'AF13', 'AF21', 'AF22', 'AF23', 'AF31', 'AF32', 'AF33', 'AF41', 'AF42', 'AF43',
                           'CS0', 'CS1', 'CS2', 'CS3', 'CS4', 'CS5', 'CS6', 'CS7', 'EF', 'VA', 'unspecified'],
                  aliases=['target']),  # No default provided on purpose
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['contract', 'tenant']],
            ['state', 'present', ['contract', 'tenant']],
        ],
    )

    contract = module.params.get('contract')
    description = module.params.get('description')
    scope = module.params.get('scope')
    priority = module.params.get('priority')
    dscp = module.params.get('dscp')
    state = module.params.get('state')
    tenant = module.params.get('tenant')

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='fvTenant',
            aci_rn='tn-{0}'.format(tenant),
            module_object=tenant,
            target_filter={'name': tenant},
        ),
        subclass_1=dict(
            aci_class='vzBrCP',
            aci_rn='brc-{0}'.format(contract),
            module_object=contract,
            target_filter={'name': contract},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='vzBrCP',
            class_config=dict(
                name=contract,
                descr=description,
                scope=scope,
                prio=priority,
                targetDscp=dscp,
            ),
        )

        aci.get_diff(aci_class='vzBrCP')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #16
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        domain_type=dict(type='str', required=True, choices=['fc', 'l2dom', 'l3dom', 'phys', 'vmm'], aliases=['type']),
        domain=dict(type='str', aliases=['domain_name', 'domain_profile', 'name']),  # Not required for querying all objects
        dscp=dict(type='str',
                  choices=['AF11', 'AF12', 'AF13', 'AF21', 'AF22', 'AF23', 'AF31', 'AF32', 'AF33', 'AF41', 'AF42', 'AF43',
                           'CS0', 'CS1', 'CS2', 'CS3', 'CS4', 'CS5', 'CS6', 'CS7', 'EF', 'VA', 'unspecified'],
                  aliases=['target']),
        encap_mode=dict(type='str', choices=['unknown', 'vlan', 'vxlan']),
        multicast_address=dict(type='str'),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
        vm_provider=dict(type='str', choices=['cloudfoundry', 'kubernetes', 'microsoft', 'openshift', 'openstack', 'redhat', 'vmware']),
        vswitch=dict(type='str', choices=['avs', 'default', 'dvs', 'unknown']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['domain_type', 'vmm', ['vm_provider']],
            ['state', 'absent', ['domain', 'domain_type']],
            ['state', 'present', ['domain', 'domain_type']],
        ],
    )

    dscp = module.params.get('dscp')
    domain = module.params.get('domain')
    domain_type = module.params.get('domain_type')
    encap_mode = module.params.get('encap_mode')
    multicast_address = module.params.get('multicast_address')
    vm_provider = module.params.get('vm_provider')
    vswitch = module.params.get('vswitch')
    if vswitch is not None:
        vswitch = VSWITCH_MAPPING.get(vswitch)
    state = module.params.get('state')

    if domain_type != 'vmm':
        if vm_provider is not None:
            module.fail_json(msg="Domain type '{0}' cannot have parameter 'vm_provider'".format(domain_type))
        if encap_mode is not None:
            module.fail_json(msg="Domain type '{0}' cannot have parameter 'encap_mode'".format(domain_type))
        if multicast_address is not None:
            module.fail_json(msg="Domain type '{0}' cannot have parameter 'multicast_address'".format(domain_type))
        if vswitch is not None:
            module.fail_json(msg="Domain type '{0}' cannot have parameter 'vswitch'".format(domain_type))

    if dscp is not None and domain_type not in ['l2dom', 'l3dom']:
        module.fail_json(msg="DSCP values can only be assigned to 'l2ext and 'l3ext' domains")

    # Compile the full domain for URL building
    if domain_type == 'fc':
        domain_class = 'fcDomP'
        domain_mo = 'uni/fc-{0}'.format(domain)
        domain_rn = 'fc-{0}'.format(domain)
    elif domain_type == 'l2dom':
        domain_class = 'l2extDomP'
        domain_mo = 'uni/l2dom-{0}'.format(domain)
        domain_rn = 'l2dom-{0}'.format(domain)
    elif domain_type == 'l3dom':
        domain_class = 'l3extDomP'
        domain_mo = 'uni/l3dom-{0}'.format(domain)
        domain_rn = 'l3dom-{0}'.format(domain)
    elif domain_type == 'phys':
        domain_class = 'physDomP'
        domain_mo = 'uni/phys-{0}'.format(domain)
        domain_rn = 'phys-{0}'.format(domain)
    elif domain_type == 'vmm':
        domain_class = 'vmmDomP'
        domain_mo = 'uni/vmmp-{0}/dom-{1}'.format(VM_PROVIDER_MAPPING.get(vm_provider), domain)
        domain_rn = 'vmmp-{0}/dom-{1}'.format(VM_PROVIDER_MAPPING.get(vm_provider), domain)

    # Ensure that querying all objects works when only domain_type is provided
    if domain is None:
        domain_mo = None

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class=domain_class,
            aci_rn=domain_rn,
            module_object=domain_mo,
            target_filter={'name': domain},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class=domain_class,
            class_config=dict(
                encapMode=encap_mode,
                mcastAddr=multicast_address,
                mode=vswitch,
                name=domain,
                targetDscp=dscp,
            ),
        )

        aci.get_diff(aci_class=domain_class)

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        tenant=dict(type='str', aliases=['tenant_name']),  # Not required for querying all objects
        epr_policy=dict(type='str', aliases=['epr_name', 'name']),  # Not required for querying all objects
        bounce_age=dict(type='int'),
        bounce_trigger=dict(type='str', choices=['coop', 'flood']),
        hold_interval=dict(type='int'),
        local_ep_interval=dict(type='int'),
        remote_ep_interval=dict(type='int'),
        description=dict(type='str', aliases=['descr']),
        move_frequency=dict(type='int'),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['epr_policy', 'tenant']],
            ['state', 'present', ['epr_policy', 'tenant']],
        ],
    )

    epr_policy = module.params.get('epr_policy')
    bounce_age = module.params.get('bounce_age')
    if bounce_age is not None and bounce_age != 0 and bounce_age not in range(150, 65536):
        module.fail_json(msg="The bounce_age must be a value of 0 or between 150 and 65535")
    if bounce_age == 0:
        bounce_age = 'infinite'
    bounce_trigger = module.params.get('bounce_trigger')
    if bounce_trigger is not None:
        bounce_trigger = BOUNCE_TRIG_MAPPING[bounce_trigger]
    description = module.params.get('description')
    hold_interval = module.params.get('hold_interval')
    if hold_interval is not None and hold_interval not in range(5, 65536):
        module.fail_json(msg="The hold_interval must be a value between 5 and 65535")
    local_ep_interval = module.params.get('local_ep_interval')
    if local_ep_interval is not None and local_ep_interval != 0 and local_ep_interval not in range(120, 65536):
        module.fail_json(msg="The local_ep_interval must be a value of 0 or between 120 and 65535")
    if local_ep_interval == 0:
        local_ep_interval = "infinite"
    move_frequency = module.params.get('move_frequency')
    if move_frequency is not None and move_frequency not in range(65536):
        module.fail_json(msg="The move_frequency must be a value between 0 and 65535")
    if move_frequency == 0:
        move_frequency = "none"
    remote_ep_interval = module.params.get('remote_ep_interval')
    if remote_ep_interval is not None and remote_ep_interval not in range(120, 65536):
        module.fail_json(msg="The remote_ep_interval must be a value of 0 or between 120 and 65535")
    if remote_ep_interval == 0:
        remote_ep_interval = "infinite"
    state = module.params.get('state')
    tenant = module.params.get('tenant')

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='fvTenant',
            aci_rn='tn-{0}'.format(tenant),
            module_object=tenant,
            target_filter={'name': tenant},
        ),
        subclass_1=dict(
            aci_class='fvEpRetPol',
            aci_rn='epRPol-{0}'.format(epr_policy),
            module_object=epr_policy,
            target_filter={'name': epr_policy},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='fvEpRetPol',
            class_config=dict(
                name=epr_policy,
                descr=description,
                bounceAgeIntvl=bounce_age,
                bounceTrig=bounce_trigger,
                holdIntvl=hold_interval,
                localEpAgeIntvl=local_ep_interval,
                remoteEpAgeIntvl=remote_ep_interval,
                moveFreq=move_frequency,
            ),
        )

        aci.get_diff(aci_class='fvEpRetPol')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #18
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        name=dict(type='str', aliases=['name', 'scheduler_name']),  # Not required for querying all objects
        description=dict(type='str', aliases=['descr']),
        windowname=dict(type='str', aliases=['windowname']),
        recurring=dict(type='bool'),
        concurCap=dict(type='int'),  # Number of devices it will run against concurrently
        maxTime=dict(type='str'),  # The amount of minutes a process will be able to run (unlimited or dd:hh:mm:ss)
        date=dict(type='str', aliases=['date']),  # The date the process will run YYYY-MM-DDTHH:MM:SS
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
        hour=dict(type='int'),
        minute=dict(type='int'),
        day=dict(type='str', default='every-day', choices=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
                                                           'Saturday', 'Sunday', 'every-day', 'even-day', 'odd-day']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['name']],
            ['state', 'present', ['name']],
        ],
    )

    state = module.params.get('state')
    name = module.params.get('name')
    windowname = module.params.get('windowname')
    recurring = module.params.get('recurring')
    date = module.params.get('date')
    hour = module.params.get('hour')
    minute = module.params.get('minute')
    maxTime = module.params.get('maxTime')
    concurCap = module.params.get('concurCap')
    day = module.params.get('day')
    description = module.params.get('description')

    if recurring:
        child_configs = [dict(trigRecurrWindowP=dict(attributes=dict(name=windowname, hour=hour, minute=minute,
                                                                     procCa=maxTime, concurCap=concurCap, day=day,)))]
    elif recurring is False:
        child_configs = [dict(trigAbsWindowP=dict(attributes=dict(name=windowname, procCap=maxTime,
                                                                  concurCap=concurCap, date=date,)))]
    else:
        child_configs = []

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='trigSchedP',
            aci_rn='fabric/schedp-{0}'.format(name),
            target_filter={'name': name},
            module_object=name,
        ),

    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='trigSchedP',
            class_config=dict(
                name=name,
                descr=description,
            ),
            child_configs=child_configs,

        )

        aci.get_diff(aci_class='trigSchedP')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #19
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        domain_type=dict(type='str',
                         required=True,
                         choices=['fc', 'l2dom', 'l3dom', 'phys', 'vmm']),
        domain=dict(type='str',
                    aliases=['domain_name', 'domain_profile'
                             ]),  # Not required for querying all objects
        pool=dict(type='str',
                  aliases=['pool_name', 'vlan_pool'
                           ]),  # Not required for querying all objects
        pool_allocation_mode=dict(type='str',
                                  required=True,
                                  aliases=['allocation_mode', 'mode'],
                                  choices=['dynamic', 'static']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
        vm_provider=dict(type='str',
                         choices=[
                             'cloudfoundry', 'kubernetes', 'microsoft',
                             'openshift', 'openstack', 'redhat', 'vmware'
                         ]),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['domain_type', 'vmm', ['vm_provider']],
            ['state', 'absent', ['domain', 'domain_type', 'pool']],
            ['state', 'present', ['domain', 'domain_type', 'pool']],
        ],
    )

    domain = module.params.get('domain')
    domain_type = module.params.get('domain_type')
    pool = module.params.get('pool')
    pool_allocation_mode = module.params.get('pool_allocation_mode')
    vm_provider = module.params.get('vm_provider')
    state = module.params.get('state')

    # Report when vm_provider is set when type is not virtual
    if domain_type != 'vmm' and vm_provider is not None:
        module.fail_json(msg="Domain type '{0}' cannot have a 'vm_provider'".
                         format(domain_type))

    # ACI Pool URL requires the allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
    pool_name = pool
    if pool is not None:
        pool_name = '[{0}]-{1}'.format(pool, pool_allocation_mode)

    # Compile the full domain for URL building
    if domain_type == 'fc':
        domain_class = 'fcDomP'
        domain_mo = 'uni/fc-{0}'.format(domain)
        domain_rn = 'fc-{0}'.format(domain)
    elif domain_type == 'l2dom':
        domain_class = 'l2extDomP'
        domain_mo = 'uni/l2dom-{0}'.format(domain)
        domain_rn = 'l2dom-{0}'.format(domain)
    elif domain_type == 'l3dom':
        domain_class = 'l3extDomP'
        domain_mo = 'uni/l3dom-{0}'.format(domain)
        domain_rn = 'l3dom-{0}'.format(domain)
    elif domain_type == 'phys':
        domain_class = 'physDomP'
        domain_mo = 'uni/phys-{0}'.format(domain)
        domain_rn = 'phys-{0}'.format(domain)
    elif domain_type == 'vmm':
        domain_class = 'vmmDomP'
        domain_mo = 'uni/vmmp-{0}/dom-{1}'.format(
            VM_PROVIDER_MAPPING[vm_provider], domain)
        domain_rn = 'vmmp-{0}/dom-{1}'.format(VM_PROVIDER_MAPPING[vm_provider],
                                              domain)

    # Ensure that querying all objects works when only domain_type is provided
    if domain is None:
        domain_mo = None

    aci_mo = 'uni/infra/vlanns-{0}'.format(pool_name)

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class=domain_class,
            aci_rn=domain_rn,
            module_object=domain_mo,
            target_filter={'name': domain},
        ),
        child_classes=['infraRsVlanNs'],
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(aci_class=domain_class,
                    class_config=dict(name=domain),
                    child_configs=[
                        {
                            'infraRsVlanNs': {
                                'attributes': {
                                    'tDn': aci_mo
                                }
                            }
                        },
                    ])

        aci.get_diff(aci_class=domain_class)

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        aep=dict(type='str',
                 aliases=['aep_name'
                          ]),  # Not required for querying all objects
        domain=dict(type='str',
                    aliases=['domain_name', 'domain_profile'
                             ]),  # Not required for querying all objects
        domain_type=dict(type='str',
                         choices=['fc', 'l2dom', 'l3dom', 'phys', 'vmm'],
                         aliases=['type'
                                  ]),  # Not required for querying all objects
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
        vm_provider=dict(type='str',
                         choices=[
                             'cloudfoundry', 'kubernetes', 'microsoft',
                             'openshift', 'openstack', 'redhat', 'vmware'
                         ]),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['domain_type', 'vmm', ['vm_provider']],
            ['state', 'absent', ['aep', 'domain', 'domain_type']],
            ['state', 'present', ['aep', 'domain', 'domain_type']],
        ],
        required_together=[
            ['domain', 'domain_type'],
        ],
    )

    aep = module.params.get('aep')
    domain = module.params.get('domain')
    domain_type = module.params.get('domain_type')
    vm_provider = module.params.get('vm_provider')
    state = module.params.get('state')

    # Report when vm_provider is set when type is not virtual
    if domain_type != 'vmm' and vm_provider is not None:
        module.fail_json(msg="Domain type '{0}' cannot have a 'vm_provider'".
                         format(domain_type))

    # Compile the full domain for URL building
    if domain_type == 'fc':
        domain_mo = 'uni/fc-{0}'.format(domain)
    elif domain_type == 'l2dom':
        domain_mo = 'uni/l2dom-{0}'.format(domain)
    elif domain_type == 'l3dom':
        domain_mo = 'uni/l3dom-{0}'.format(domain)
    elif domain_type == 'phys':
        domain_mo = 'uni/phys-{0}'.format(domain)
    elif domain_type == 'vmm':
        domain_mo = 'uni/vmmp-{0}/dom-{1}'.format(
            VM_PROVIDER_MAPPING[vm_provider], domain)
    else:
        domain_mo = None

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='infraAttEntityP',
            aci_rn='infra/attentp-{0}'.format(aep),
            module_object=aep,
            target_filter={'name': aep},
        ),
        subclass_1=dict(
            aci_class='infraRsDomP',
            aci_rn='rsdomP-[{0}]'.format(domain_mo),
            module_object=domain_mo,
            target_filter={'tDn': domain_mo},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='infraRsDomP',
            class_config=dict(tDn=domain_mo),
        )

        aci.get_diff(aci_class='infraRsDomP')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #21
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        name=dict(type='str',
                  aliases=['credential_name', 'credential_profile']),
        credential_password=dict(type='str', no_log=True),
        credential_username=dict(type='str'),
        description=dict(type='str', aliases=['descr']),
        domain=dict(type='str', aliases=['domain_name', 'domain_profile']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
        vm_provider=dict(type='str', choices=VM_PROVIDER_MAPPING.keys()))

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['domain']],
            ['state', 'present', ['domain']],
        ],
    )

    name = module.params.get('name')
    credential_password = module.params.get('credential_password')
    credential_username = module.params.get('credential_username')
    description = module.params.get('description')
    domain = module.params.get('domain')
    state = module.params.get('state')
    vm_provider = module.params.get('vm_provider')

    credential_class = 'vmmUsrAccP'
    usracc_mo = 'uni/vmmp-{0}/dom-{1}/usracc-{2}'.format(
        VM_PROVIDER_MAPPING.get(vm_provider), domain, name)
    usracc_rn = 'vmmp-{0}/dom-{1}/usracc-{2}'.format(
        VM_PROVIDER_MAPPING.get(vm_provider), domain, name)

    # Ensure that querying all objects works when only domain is provided
    if name is None:
        usracc_mo = None

    aci = ACIModule(module)
    aci.construct_url(root_class=dict(
        aci_class=credential_class,
        aci_rn=usracc_rn,
        module_object=usracc_mo,
        target_filter={
            'name': domain,
            'usracc': name
        },
    ), )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class=credential_class,
            class_config=dict(descr=description,
                              name=name,
                              pwd=credential_password,
                              usr=credential_username),
        )

        aci.get_diff(aci_class=credential_class)

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
예제 #22
0
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        tenant=dict(type='str',
                    aliases=['tenant_name'
                             ]),  # Not required for querying all objects
        rtp=dict(type='str',
                 aliases=['name', 'rtp_name'
                          ]),  # Not required for querying all objects
        description=dict(type='str', aliases=['descr']),
        tag=dict(type='int'),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['rtp', 'tenant']],
            ['state', 'present', ['rtp', 'tenant']],
        ],
    )

    rtp = module.params.get('rtp')
    description = module.params.get('description')
    tag = module.params.get('tag')
    state = module.params.get('state')
    tenant = module.params.get('tenant')

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='fvTenant',
            aci_rn='tn-{0}'.format(tenant),
            module_object=tenant,
            target_filter={'name': tenant},
        ),
        subclass_1=dict(
            aci_class='l3extRouteTagPol',
            aci_rn='rttag-{0}'.format(rtp),
            module_object=rtp,
            target_filter={'name': rtp},
        ),
    )

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='l3extRouteTagPol',
            class_config=dict(
                name=rtp,
                descr=description,
                tag=tag,
            ),
        )

        aci.get_diff(aci_class='l3extRouteTagPol')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        leaf_profile=dict(type='str',
                          aliases=['leaf_profile_name'
                                   ]),  # Not required for querying all objects
        interface_selector=dict(type='str',
                                aliases=[
                                    'interface_profile_name',
                                    'interface_selector_name', 'name'
                                ]),  # Not required for querying all objects
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']))

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[[
            'state', 'absent', ['leaf_profile', 'interface_selector']
        ], ['state', 'present', ['leaf_profile', 'interface_selector']]],
    )

    leaf_profile = module.params.get('leaf_profile')
    # WARNING: interface_selector accepts non existing interface_profile names and they appear on APIC gui with a state of "missing-target"
    interface_selector = module.params.get('interface_selector')
    state = module.params.get('state')

    # Defining the interface profile tDn for clarity
    interface_selector_tDn = 'uni/infra/accportprof-{0}'.format(
        interface_selector)

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class='infraNodeP',
            aci_rn='infra/nprof-{0}'.format(leaf_profile),
            module_object=leaf_profile,
            target_filter={'name': leaf_profile},
        ),
        subclass_1=dict(
            aci_class='infraRsAccPortP',
            aci_rn='rsaccPortP-[{0}]'.format(interface_selector_tDn),
            module_object=interface_selector,
            target_filter={'name': interface_selector},
        ))

    aci.get_existing()

    if state == 'present':
        aci.payload(
            aci_class='infraRsAccPortP',
            class_config=dict(tDn=interface_selector_tDn),
        )

        aci.get_diff(aci_class='infraRsAccPortP')

        aci.post_config()

    elif state == 'absent':
        aci.delete_config()

    aci.exit_json()