コード例 #1
0
def main():
    module = AnsibleModule(argument_spec=dict(
        username=dict(type=str, required=True),
        password=dict(type=str, required=True, no_log=True),
        url=dict(type=str, required=True),
        vlan=dict(type=str, required=True),
        name=dict(type=str, required=True),
        domainid=dict(type=str, required=False, default=1),
        description=dict(type=str, required=False, default="None"),
        validate_certs=dict(type='bool', default=True),
        state=dict(default='present', choices=['present', 'absent'])),
                           supports_check_mode=True)

    result = dict(changed=False)
    username = module.params['username']
    password = module.params['password']
    url = module.params['url']
    vlan = module.params['vlan']
    name = module.params['name']
    domainid = module.params['domainid']
    description = module.params['description']
    validate_certs = module.params['validate_certs']
    state = module.params['state']

    session = PhpIpamWrapper(username, password, url, validate_certs)
    try:
        session.create_session()
    except AttributeError:
        module.fail_json(msg='Error getting authorization token', **result)

    vlan_url = url + 'vlan/'
    found_vlan = session.get_vlan(vlan, domainid)

    optional_args = {
        'name': name,
        'description': description,
        'domainId': domainid
    }

    if state == 'present' and found_vlan is None:
        # Create vlan if not present
        if module.check_mode:
            module.exit_json(changed=True,
                             output="Vlan {} Created".format(vlan))

        creation = session.create(session,
                                  vlan_url,
                                  number=vlan,
                                  **optional_args)
        if creation['code'] == 201:
            result['changed'] = True
            result['output'] = creation
            module.exit_json(**result)
        else:
            module.fail_json(msg='vlan unable to be created', **result)
    elif state == 'present':
        # Update vlan information if necessary

        value_changed = False
        payload = {'name': name}
        vlan_id = session.get_vlan_id(vlan, domainid)

        for k in optional_args:
            if optional_args[k] != found_vlan[k]:
                value_changed = True
                payload[k] = optional_args[k]
        if value_changed:
            if module.check_mode:
                module.exit_json(changed=True,
                                 output="Vlan {} modified".format(vlan_id))

            patch_response = session.modify(session,
                                            vlan_url,
                                            id=vlan_id,
                                            **payload)
            result['changed'] = True
            result['output'] = patch_response
            module.exit_json(**result)
        else:
            result['changed'] = False
            module.exit_json(**result)
    else:
        # Delete vlan if it exist

        vlan_id = session.get_vlan_id(vlan)
        if vlan_id is None:
            result['output'] = 'Vlan doesn\'t exist'
            module.exit_json(**result)
        else:
            if module.check_mode:
                module.exit_json(changed=True)

            deletion = session.remove(session, vlan_url, vlan_id)
            if deletion['code'] == 200:
                result['changed'] = True
                result['output'] = deletion
                module.exit_json(**result)
            else:
                result['output'] = deletion
                module.fail_json(**result)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            username=dict(type=str, required=True),
            password=dict(type=str, required=True, no_log=True),
            url=dict(type=str, required=True),
            section=dict(type=str, required=True),
            master_section=dict(type=str, required=False, default='root'),
            description=dict(type=str, required=False),
            state=dict(default='present', choices=['present', 'absent'])
        ),
        supports_check_mode=False
    )

    result = dict(
        changed=False
    )
    username = module.params['username']
    password = module.params['password']
    url = module.params['url']
    section = module.params['section']
    master_section = module.params['master_section']
    description = module.params['description']
    state = module.params['state']

    session = PhpIpamWrapper(username, password, url)
    try:
        session.create_session()
    except AttributeError:
        module.fail_json(msg='Error getting authorization token', **result)

    section_url = url + 'sections/'
    found_section = session.get_section(section)
    master_section_id = set_master_section(session, master_section)
    if master_section_id is None:
        module.fail_json(msg='master_section does not exist', **result)
    else:
        optional_args = {'masterSection': set_master_section(session, master_section),
                         'description': description}
    if state == 'present' and found_section is None:
        # Create the section since it does not exist

        if optional_args['masterSection'] == '0':
            del optional_args['masterSection']
        creation = session.create(session,
                                  section_url,
                                  name=section,
                                  **optional_args)
        if creation['code'] == 201:
            result['changed'] = True
            result['output'] = creation
            module.exit_json(**result)
        elif creation['code'] == 500:
            result['output'] = creation
            module.exit_json(**result)
        else:
            result['output'] = creation
            module.fail_json(msg='Something went wrong', **result)
    elif state == 'present':
        # Potentially modify the section if it doesn't match

        value_changed = False
        payload = {}
        section_id = session.get_section_id(section)

        for k in optional_args:
            if optional_args[k] != found_section[k]:
                value_changed = True
                payload[k] = optional_args[k]
        if value_changed:
            patch_response = session.modify(session,
                                            section_url,
                                            id=section_id,
                                            **payload)
            result['changed'] = True
            result['output'] = patch_response
            module.exit_json(**result)
        else:
            result['output'] = patch_response
            module.exit_json(**result)
    else:
        # Ensure the section does not exist, delete if necessary

        section_info = session.get_section(section)
        try:
            deletion = session.remove(session,
                                      section_url,
                                      section_info['id'])
            if deletion['code'] == 200:
                result['changed'] = True
                result['output'] = deletion
                module.exit_json(**result)
        except KeyError:
            result['ouput'] = 'Section did not exist'
            module.exit_json(**result)
        except TypeError:
            result['output'] = 'Section did not exist'
            module.exit_json(**result)
def main():
    module = AnsibleModule(argument_spec=dict(
        username=dict(type=str, required=True),
        password=dict(type=str, required=True, no_log=True),
        url=dict(type=str, required=True),
        section=dict(type=str, required=True),
        subnet=dict(type=str, required=True),
        master_subnet=dict(type=str, required=False),
        description=dict(type=str, required=False),
        vlan=dict(type=str, required=False),
        state=dict(default='present', choices=['present', 'absent'])),
                           supports_check_mode=False)

    result = dict(changed=False)
    username = module.params['username']
    password = module.params['password']
    url = module.params['url']
    section = module.params['section']
    subnet = module.params['subnet']
    master_subnet = module.params['master_subnet']
    description = module.params['description']
    vlan = module.params['vlan']
    state = module.params['state']

    session = PhpIpamWrapper(username, password, url)
    try:
        session.create_session()
    except AttributeError:
        module.fail_json(msg='Error getting authorization token', **result)

    subnet_url = url + 'subnets/'
    try:
        section_id = session.get_section_id(section)
    except:
        module.fail_json(msg='section doesn\'t exist', **result)
    found_subnet = session.get_subnet(subnet, section)
    optional_args = {'description': description}
    if vlan:
        # If vlan is defined, make sure it exists and then set

        vlan_id = session.get_vlan_id(vlan)
        if vlan_id is None:
            module.fail_json(msg='vlan not found', **result)
        else:
            optional_args['vlanId'] = vlan_id

    if master_subnet:
        master_subnet_id = session.get_subnet_id(master_subnet, section)
        if master_subnet_id is None:
            module.fail_json(msg='master_subnet not found', **result)
        else:
            optional_args['masterSubnetId'] = master_subnet_id

    if state == 'present' and found_subnet is None:
        # Create subnet if it doesn't exist

        subnet_split = subnet.rsplit('/', 1)
        creation = session.create(session,
                                  subnet_url,
                                  subnet=subnet_split[0],
                                  mask=subnet_split[1],
                                  sectionId=section_id,
                                  **optional_args)
        if creation['code'] == 201:
            result['changed'] = True
            result['output'] = creation
            module.exit_json(**result)
        else:
            result['output'] = creation
            module.fail_json(**result)
    elif state == 'present':
        # Update subnet if necessary

        value_changed = False
        payload = {}
        for k in optional_args:
            if optional_args[k] != found_subnet[k]:
                value_changed = True
                payload[k] = optional_args[k]
        if value_changed:
            patch_response = session.modify(
                session,
                subnet_url,
                id=found_subnet['id'],
                sectionId=found_subnet['sectionId'],
                **payload)
            result['changed'] = True
            result['output'] = patch_response
            module.exit_json(**result)
        else:
            result['output'] = 'Subnet required no change'
            module.exit_json(**result)
    else:
        # Delete subnet if present
        try:
            deletion = session.remove(session, subnet_url, found_subnet['id'])
            if deletion['code'] == 200:
                result['changed'] = True
                result['output'] = deletion
                module.exit_json(**result)
        except TypeError:
            result['output'] = 'Subnet did not exist'
            module.exit_json(**result)