def main():
    module = ForemanEntityApypieAnsibleModule(argument_spec=dict(
        name=dict(required=True),
        parent=dict(),
        organizations=dict(type='list'),
    ), )

    entity_dict = module.clean_params()

    module.connect()

    # Get short name and parent from provided name
    name, parent = split_fqn(entity_dict['name'])
    entity_dict['name'] = name

    if 'parent' in entity_dict:
        if parent:
            module.fail_json(
                msg=
                "Please specify the parent either separately, or as part of the title."
            )
        parent = entity_dict['parent']
    if parent:
        search_string = 'title="{}"'.format(parent)
        entity_dict['parent'] = module.find_resource(
            'locations',
            search=search_string,
            thin=True,
            failsafe=module.desired_absent)

        if module.desired_absent and entity_dict['parent'] is None:
            # Parent location does not exist so just exit here
            module.exit_json(changed=False)

    if not module.desired_absent:
        if 'organizations' in entity_dict:
            entity_dict['organizations'] = module.find_resources_by_name(
                'organizations', entity_dict['organizations'], thin=True)

    entity = module.find_resource('locations',
                                  search='title="{}"'.format(
                                      build_fqn(name, parent)),
                                  failsafe=True)

    changed = module.ensure_resource_state('locations',
                                           entity_dict,
                                           entity,
                                           name_map=name_map)

    module.exit_json(changed=changed)
def main():
    module = ForemanEntityApypieAnsibleModule(
        entity_spec=dict(
            name=dict(required=True),
            controller=dict(required=True),
            public=dict(defaut='true', type='bool'),
            query=dict(),
        ),
        argument_spec=dict(state=dict(
            default='present',
            choices=['present_with_defaults', 'present', 'absent']), ),
        required_if=(
            ['state', 'present', ['query']],
            ['state', 'present_with_defaults', ['query']],
        ),
    )

    entity_dict = module.clean_params()

    module.connect()

    search = 'name="{}",controller="{}"'.format(entity_dict['name'],
                                                entity_dict['controller'])
    entity = module.find_resource('bookmarks', search, failsafe=True)

    changed = module.ensure_entity_state('bookmarks', entity_dict, entity)

    module.exit_json(changed=changed)
def main():
    module = ForemanEntityApypieAnsibleModule(
        argument_spec=dict(
            operatingsystem=dict(required=True),
            state=dict(default='present',
                       choices=['present', 'present_with_defaults', 'absent']),
        ),
        entity_spec=dict(
            template_kind=dict(required=True,
                               type='entity',
                               flat_name='template_kind_id'),
            provisioning_template=dict(type='entity',
                                       flat_name='provisioning_template_id'),
        ),
        required_if=(
            ['state', 'present', ['provisioning_template']],
            ['state', 'present_with_defaults', ['provisioning_template']],
        ),
    )

    entity_dict = module.clean_params()

    module.connect()

    entity_dict['operatingsystem'] = module.find_operatingsystem(
        entity_dict['operatingsystem'], thin=True)
    entity_dict['template_kind'] = module.find_resource_by_name(
        'template_kinds', entity_dict['template_kind'], thin=True)

    scope = {'operatingsystem_id': entity_dict['operatingsystem']['id']}
    search = 'template_kind_id={}'.format(entity_dict['template_kind']['id'])
    entity = module.find_resource('os_default_templates',
                                  search,
                                  params=scope,
                                  failsafe=True)

    # Find Provisioning Template
    if 'provisioning_template' in entity_dict:
        if module.desired_absent:
            module.fail_json(
                msg='Provisioning template must not be specified for deletion.'
            )
        entity_dict['provisioning_template'] = module.find_resource_by_name(
            'provisioning_templates', entity_dict['provisioning_template'])
        if entity_dict['provisioning_template'][
                'template_kind_id'] != entity_dict['template_kind']['id']:
            module.fail_json(msg='Provisioning template kind mismatching.')

    changed = module.ensure_entity_state('os_default_templates',
                                         entity_dict,
                                         entity,
                                         params=scope)

    module.exit_json(changed=changed)
예제 #4
0
def main():
    module = ForemanEntityApypieAnsibleModule(argument_spec=dict(
        name=dict(required=True),
        firstname=dict(required=False),
        lastname=dict(required=False),
        mail=dict(required=False),
        description=dict(required=False),
        admin=dict(required=False, type='bool', default=False),
        user_password=dict(required=False, no_log=True),
        default_location=dict(required=False),
        default_organization=dict(required=False),
        auth_source=dict(required=False),
        timezone=dict(required=False, choices=timezone_list),
        locale=dict(required=False, choices=locale_list),
        roles=dict(required=False, type='list'),
        locations=dict(required=False, type='list'),
        organizations=dict(required=False, type='list')), )

    entity_dict = module.clean_params()

    module.connect()

    search = 'login="******"'.format(entity_dict['name'])
    entity = module.find_resource('users', search, failsafe=True)

    if not module.desired_absent:
        if 'mail' not in entity_dict:
            entity_dict['mail'] = entity['mail']

        if 'default_location' in entity_dict:
            entity_dict['default_location'] = module.find_resource_by_title(
                'locations', entity_dict['default_location'], thin=True)['id']

        if 'default_organization' in entity_dict:
            entity_dict['default_organization'] = module.find_resource_by_name(
                'organizations',
                entity_dict['default_organization'],
                thin=True)['id']

        if 'auth_source' in entity_dict:
            entity_dict['auth_source'] = module.find_resource_by_name(
                'auth_sources', entity_dict['auth_source'], thin=True)['id']

        if 'roles' in entity_dict:
            entity_dict['roles'] = module.find_resources_by_name(
                'roles', entity_dict['roles'], thin=True)

        if 'locations' in entity_dict:
            entity_dict['locations'] = module.find_resources_by_title(
                'locations', entity_dict['locations'], thin=True)

        if 'organizations' in entity_dict:
            entity_dict['organizations'] = module.find_resources_by_name(
                'organizations', entity_dict['organizations'], thin=True)

    check_missing = None
    if 'user_password' in entity_dict:
        check_missing = [name_map['user_password']]

    changed = module.ensure_resource_state('users',
                                           entity_dict,
                                           entity,
                                           name_map=name_map,
                                           check_missing=check_missing)

    module.exit_json(changed=changed, entity_dict=entity_dict)
예제 #5
0
def main():
    module = ForemanEntityApypieAnsibleModule(
        entity_spec=dict(
            name=dict(),
            release_name=dict(),
            description=dict(),
            family=dict(),
            major=dict(),
            minor=dict(),
            architectures=dict(type='entity_list', flat_name='architecture_ids'),
            media=dict(type='entity_list', flat_name='medium_ids'),
            ptables=dict(type='entity_list', flat_name='ptable_ids'),
            provisioning_templates=dict(type='entity_list', flat_name='provisioning_template_ids'),
            password_hash=dict(choices=['MD5', 'SHA256', 'SHA512']),
            parameters=dict(type='nested_list', entity_spec=parameter_entity_spec),
        ),
        argument_spec=dict(
            state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']),
        ),
        required_if=[
            ['state', 'present', ['name', 'major', 'family']],
            ['state', 'present_with_defaults', ['name', 'major', 'family']],
        ],
        required_one_of=[
            ['description', 'name'],
            ['description', 'major'],
        ],
    )

    entity_dict = module.clean_params()

    module.connect()

    # Try to find the Operating System to work on
    # name is however not unique, but description is, as well as "<name> <major>[.<minor>]"
    entity = None
    # If we have a description, search for it
    if 'description' in entity_dict and entity_dict['description'] != '':
        search_string = 'description="{}"'.format(entity_dict['description'])
        entity = module.find_resource('operatingsystems', search_string, failsafe=True)
    # If we did not yet find a unique OS, search by name & version
    # In case of state == absent, those information might be missing, we assume that we did not find an operatingsytem to delete then
    if entity is None and 'name' in entity_dict and 'major' in entity_dict:
        search_string = ','.join('{}="{}"'.format(key, entity_dict[key]) for key in ('name', 'major', 'minor') if key in entity_dict)
        entity = module.find_resource('operatingsystems', search_string, failsafe=True)

    if not entity and (module.state == 'present' or module.state == 'present_with_defaults'):
        # we actually attempt to create a new one...
        for param_name in ['major', 'family', 'password_hash']:
            if param_name not in entity_dict.keys():
                module.fail_json(msg='{} is a required parameter to create a new operating system.'.format(param_name))

    if not module.desired_absent:
        if 'architectures' in entity_dict:
            entity_dict['architectures'] = module.find_resources_by_name('architectures', entity_dict['architectures'], thin=True)

        if 'media' in entity_dict:
            entity_dict['media'] = module.find_resources_by_name('media', entity_dict['media'], thin=True)

        if 'ptables' in entity_dict:
            entity_dict['ptables'] = module.find_resources_by_name('ptables', entity_dict['ptables'], thin=True)

        if 'provisioning_templates' in entity_dict:
            entity_dict['provisioning_templates'] = module.find_resources_by_name('provisioning_templates', entity_dict['provisioning_templates'], thin=True)

    parameters = entity_dict.get('parameters')

    changed, operating_system = module.ensure_entity('operatingsystems', entity_dict, entity)

    if operating_system:
        scope = {'operatingsystem_id': operating_system['id']}
        changed |= module.ensure_scoped_parameters(scope, entity, parameters)

    module.exit_json(changed=changed)