def main():
    module = ForemanEntityApypieAnsibleModule(
        entity_spec=dict(
            name=dict(required=True),
            locations=dict(type='entity_list', flat_name='location_ids'),
            organizations=dict(type='entity_list', flat_name='organization_ids'),
            operatingsystems=dict(type='entity_list', flat_name='operatingsystem_ids'),
            os_family=dict(),
            path=dict(),
        ),
    )

    entity_dict = module.clean_params()

    module.connect()
    name = entity_dict['name']

    affects_multiple = name == '*'
    # sanitize user input, filter unuseful configuration combinations with 'name: *'
    if affects_multiple:
        if module.state == 'present_with_defaults':
            module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together")
        if module.desired_absent:
            if list(entity_dict.keys()) != ['name']:
                module.fail_json(msg='When deleting all installation media, there is no need to specify further parameters %s ' % entity_dict.keys())

    if affects_multiple:
        entities = module.list_resource('media')
        if not module.desired_absent:  # not 'thin'
            entities = [module.show_resource('media', entity['id']) for entity in entities]
        if not entities:
            # Nothing to do shortcut to exit
            module.exit_json(changed=False)
    else:
        entity = module.find_resource_by_name('media', name=entity_dict['name'], failsafe=True)

    if not module.desired_absent:
        if 'operatingsystems' in entity_dict:
            entity_dict['operatingsystems'] = module.find_operatingsystems(entity_dict['operatingsystems'], thin=True)
            if not affects_multiple and len(entity_dict['operatingsystems']) == 1 and 'os_family' not in entity_dict and entity is None:
                entity_dict['os_family'] = module.show_resource('operatingsystems', entity_dict['operatingsystems'][0]['id'])['family']

        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)

    changed = False
    if not affects_multiple:
        changed = module.ensure_entity_state('media', entity_dict, entity)
    else:
        entity_dict.pop('name')
        for entity in entities:
            changed |= module.ensure_entity_state('media', entity_dict, entity)

    module.exit_json(changed=changed)
コード例 #2
0
def main():
    module = ForemanEntityApypieAnsibleModule(entity_spec=dict(
        name=dict(required=True),
        operatingsystems=dict(type='entity_list',
                              flat_name='operatingsystem_ids'),
    ), )

    entity_dict = module.clean_params()

    module.connect()

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

    entity = module.find_resource_by_name('architectures',
                                          name=entity_dict['name'],
                                          failsafe=True)

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

    module.exit_json(changed=changed)
def main():
    module = ForemanEntityApypieAnsibleModule(
        argument_spec=dict(
            file_name=dict(type='path'),
            state=dict(default='present',
                       choices=['absent', 'present_with_defaults', 'present']),
        ),
        entity_spec=dict(
            audit_comment=dict(),
            kind=dict(choices=[
                'finish',
                'iPXE',
                'job_template',
                'POAP',
                'provision',
                'ptable',
                'PXELinux',
                'PXEGrub',
                'PXEGrub2',
                'script',
                'snippet',
                'user_data',
                'ZTP',
            ],
                      type='entity',
                      flat_name='template_kind_id'),
            template=dict(),
            locations=dict(type='entity_list', flat_name='location_ids'),
            locked=dict(type='bool'),
            name=dict(),
            organizations=dict(type='entity_list',
                               flat_name='organization_ids'),
            operatingsystems=dict(type='entity_list',
                                  flat_name='operatingsystem_ids'),
            snippet=dict(type='invisible'),
        ),
        mutually_exclusive=[
            ['file_name', 'template'],
        ],
        required_one_of=[
            ['name', 'file_name', 'template'],
        ],
    )

    # We do not want a template text for bulk operations
    if module.params['name'] == '*':
        if module.params['file_name'] or module.params['template']:
            module.fail_json(
                msg="Neither file_name nor template allowed if 'name: *'!")

    entity_dict = module.clean_params()
    file_name = entity_dict.pop('file_name', None)

    if file_name or 'template' in entity_dict:
        if file_name:
            parsed_dict = parse_template_from_file(file_name, module)
        else:
            parsed_dict = parse_template(entity_dict['template'], module)
        # sanitize name from template data
        # The following condition can actually be hit, when someone is trying to import a
        # template with the name set to '*'.
        # Besides not being sensible, this would go horribly wrong in this module.
        if 'name' in parsed_dict and parsed_dict['name'] == '*':
            module.fail_json(msg="Cannot use '*' as a template name!")
        # module params are priorized
        parsed_dict.update(entity_dict)
        entity_dict = parsed_dict

    # make sure, we have a name
    if 'name' not in entity_dict:
        if file_name:
            entity_dict['name'] = os.path.splitext(
                os.path.basename(file_name))[0]
        else:
            module.fail_json(
                msg='No name specified and no filename to infer it.')

    affects_multiple = entity_dict['name'] == '*'
    # sanitize user input, filter unuseful configuration combinations with 'name: *'
    if affects_multiple:
        if module.state == 'present_with_defaults':
            module.fail_json(
                msg=
                "'state: present_with_defaults' and 'name: *' cannot be used together"
            )
        if module.desired_absent:
            if len(entity_dict.keys()) != 1:
                module.fail_json(
                    msg=
                    "When deleting all templates, there is no need to specify further parameters."
                )

    module.connect()

    if affects_multiple:
        entities = module.list_resource('provisioning_templates')
        if not entities:
            # Nothing to do; shortcut to exit
            module.exit_json(changed=False)
        if not module.desired_absent:  # not 'thin'
            entities = [
                module.show_resource('provisioning_templates', entity['id'])
                for entity in entities
            ]
    else:
        entity = module.find_resource_by_name('provisioning_templates',
                                              name=entity_dict['name'],
                                              failsafe=True)

    if not module.desired_absent:
        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)

        if 'operatingsystems' in entity_dict:
            entity_dict['operatingsystems'] = module.find_operatingsystems(
                entity_dict['operatingsystems'], thin=True)

    if not affects_multiple:
        entity_dict = find_template_kind(module, entity_dict)

    changed = False
    if not affects_multiple:
        changed = module.ensure_entity_state('provisioning_templates',
                                             entity_dict, entity)
    else:
        entity_dict.pop('name')
        for entity in entities:
            changed |= module.ensure_entity_state('provisioning_templates',
                                                  entity_dict, entity)

    module.exit_json(changed=changed)