Exemplo n.º 1
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(required=True),
            username=dict(required=True, no_log=True),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            name=dict(required=True),
            parent=dict(),
            organizations=dict(type='list'),
            state=dict(default='present', choices=['present', 'absent']),
        ),
        supports_check_mode=True,
    )

    if has_import_error:
        module.fail_json(msg=import_error_msg)

    entity_dict = dict([(k, v) for (k, v) in module.params.items()
                        if v is not None])

    server_url = entity_dict.pop('server_url')
    username = entity_dict.pop('username')
    password = entity_dict.pop('password')
    verify_ssl = entity_dict.pop('verify_ssl')
    state = entity_dict.pop('state')

    try:
        create_server(server_url, (username, password), verify_ssl)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    ping_server(module)
    name_or_title = entity_dict.pop('name')
    parent = entity_dict.pop('parent', None)

    # Get short name and parent from provided name
    parent_from_title, name = split_fqn(name_or_title)

    entity_dict['name'] = name
    if parent:
        entity_dict['parent'] = find_location(module, parent)
    elif parent_from_title:
        entity_dict['parent'] = find_location(module, parent_from_title)

    if 'organizations' in entity_dict:
        entity_dict['organizations'] = find_organizations(
            module, entity_dict['organizations'])

    entity = find_location(module,
                           title=build_fqn(name_or_title, parent),
                           failsafe=True)
    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Location, entity_dict, entity, state,
                                    module)

    module.exit_json(changed=changed)
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 = ForemanEntityAnsibleModule(entity_spec=dict(
        name=dict(required=True),
        parent=dict(type='entity', flat_name='parent_id'),
        organizations=dict(type='entity_list', flat_name='organization_ids'),
    ), )

    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:
        entity_dict['parent'] = module.find_resource_by_title(
            'locations', parent, 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()

    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_by_title('locations',
                                           build_fqn(name, parent),
                                           failsafe=True)

    module.ensure_entity('locations', entity_dict, entity)

    module.exit_json()
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            parent=dict(),
            organizations=dict(type='list'),
        ),
        supports_check_mode=True,
    )

    (server_params, entity_dict, state) = module.parse_params()

    try:
        (server_url, username, password, verify_ssl) = server_params
        create_server(server_url, (username, password), verify_ssl)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    ping_server(module)
    name_or_title = entity_dict.pop('name')
    parent = entity_dict.pop('parent', None)

    # Get short name and parent from provided name
    parent_from_title, name = split_fqn(name_or_title)

    entity_dict['name'] = name
    if parent:
        entity_dict['parent'] = find_location(module, parent)
    elif parent_from_title:
        entity_dict['parent'] = find_location(module, parent_from_title)

    if 'organizations' in entity_dict:
        entity_dict['organizations'] = find_organizations(module, entity_dict['organizations'])

    entity = find_location(module, title=build_fqn(name_or_title, parent), failsafe=True)
    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Location, entity_dict, entity, state, module)

    module.exit_json(changed=changed)
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            parent=dict(),
            organizations=dict(type='list'),
        ),
        supports_check_mode=True,
    )

    (entity_dict, state) = module.parse_params()

    module.connect()

    name_or_title = entity_dict.pop('name')
    parent = entity_dict.pop('parent', None)

    # Get short name and parent from provided name
    parent_from_title, name = split_fqn(name_or_title)

    entity_dict['name'] = name
    if parent:
        entity_dict['parent'] = find_location(module, parent)
    elif parent_from_title:
        entity_dict['parent'] = find_location(module, parent_from_title)

    if 'organizations' in entity_dict:
        entity_dict['organizations'] = find_organizations(
            module, entity_dict['organizations'])

    entity = find_location(module,
                           title=build_fqn(name_or_title, parent),
                           failsafe=True)
    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Location, entity_dict, entity, state,
                                    module)

    module.exit_json(changed=changed)
def main():
    module = ForemanHostgroupAnsibleModule(
        entity_spec=dict(
            name=dict(required=True),
            description=dict(),
            parent=dict(type='entity', flat_name='parent_id'),
            realm=dict(type='entity', flat_name='realm_id'),
            architecture=dict(type='entity', flat_name='architecture_id'),
            operatingsystem=dict(type='entity',
                                 flat_name='operatingsystem_id'),
            medium=dict(aliases=['media'],
                        type='entity',
                        flat_name='medium_id'),
            ptable=dict(type='entity', flat_name='ptable_id'),
            pxe_loader=dict(choices=[
                'PXELinux BIOS', 'PXELinux UEFI', 'Grub UEFI', 'Grub2 BIOS',
                'Grub2 ELF', 'Grub2 UEFI', 'Grub2 UEFI SecureBoot',
                'Grub2 UEFI HTTP', 'Grub2 UEFI HTTPS',
                'Grub2 UEFI HTTPS SecureBoot', 'iPXE Embedded',
                'iPXE UEFI HTTP', 'iPXE Chain BIOS', 'iPXE Chain UEFI'
            ]),
            root_pass=dict(no_log=True),
            environment=dict(type='entity', flat_name='environment_id'),
            puppetclasses=dict(type='entity_list',
                               flat_name='puppetclass_ids'),
            config_groups=dict(type='entity_list',
                               flat_name='config_group_ids'),
            puppet_proxy=dict(type='entity', flat_name='puppet_proxy_id'),
            puppet_ca_proxy=dict(type='entity',
                                 flat_name='puppet_ca_proxy_id'),
            openscap_proxy=dict(type='entity', flat_name='openscap_proxy_id'),
            parameters=dict(type='nested_list',
                            entity_spec=parameter_entity_spec),
            content_source=dict(type='entity', flat_name='content_source_id'),
            lifecycle_environment=dict(type='entity',
                                       flat_name='lifecycle_environment_id'),
            kickstart_repository=dict(type='entity',
                                      flat_name='kickstart_repository_id'),
            content_view=dict(type='entity', flat_name='content_view_id'),
        ),
        argument_spec=dict(
            organization=dict(),
            updated_name=dict(),
        ),
    )

    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

    katello_params = [
        'content_source', 'lifecycle_environment', 'content_view'
    ]

    if 'organization' not in entity_dict and list(
            set(katello_params) & set(entity_dict.keys())):
        module.fail_json(
            msg="Please specify the organization when using katello parameters."
        )

    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:
        entity_dict['parent'] = module.find_resource_by_title(
            'hostgroups',
            title=parent,
            thin=True,
            failsafe=module.desired_absent)

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

    if not module.desired_absent:
        if 'realm' in entity_dict:
            entity_dict['realm'] = module.find_resource_by_name(
                'realms', name=entity_dict['realm'], failsafe=False, thin=True)

        if 'architecture' in entity_dict:
            entity_dict['architecture'] = module.find_resource_by_name(
                'architectures',
                name=entity_dict['architecture'],
                failsafe=False,
                thin=True)

        if 'operatingsystem' in entity_dict:
            entity_dict['operatingsystem'] = module.find_operatingsystem(
                entity_dict['operatingsystem'], thin=True)

        if 'medium' in entity_dict:
            entity_dict['medium'] = module.find_resource_by_name(
                'media', name=entity_dict['medium'], failsafe=False, thin=True)

        if 'ptable' in entity_dict:
            entity_dict['ptable'] = module.find_resource_by_name(
                'ptables',
                name=entity_dict['ptable'],
                failsafe=False,
                thin=True)

        if 'environment' in entity_dict:
            entity_dict['environment'] = module.find_resource_by_name(
                'environments',
                name=entity_dict['environment'],
                failsafe=False,
                thin=True)

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

        for proxy in [
                'puppet_proxy', 'puppet_ca_proxy', 'openscap_proxy',
                'content_source'
        ]:
            if proxy in entity_dict:
                entity_dict[proxy] = module.find_resource_by_name(
                    'smart_proxies', entity_dict[proxy], thin=True)

        if 'organization' in entity_dict:
            if 'organizations' in entity_dict:
                if entity_dict['organization'] not in entity_dict[
                        'organizations']:
                    entity_dict['organizations'].append(
                        entity_dict['organization'])
            else:
                entity_dict['organizations'] = [entity_dict['organization']]

            entity_dict, scope = module.handle_organization_param(entity_dict)

        if 'lifecycle_environment' in entity_dict:
            entity_dict[
                'lifecycle_environment'] = module.find_resource_by_name(
                    'lifecycle_environments',
                    name=entity_dict['lifecycle_environment'],
                    params=scope,
                    failsafe=False,
                    thin=True)

        if 'kickstart_repository' in entity_dict:
            entity_dict['kickstart_repository'] = module.find_resource_by_name(
                'repositories',
                name=entity_dict['kickstart_repository'],
                params=scope,
                failsafe=False,
                thin=True)

        if 'content_view' in entity_dict:
            entity_dict['content_view'] = module.find_resource_by_name(
                'content_views',
                name=entity_dict['content_view'],
                params=scope,
                failsafe=False,
                thin=True)

    entity_dict = module.handle_common_host_params(entity_dict)
    entity_dict = module.handle_taxonomy_params(entity_dict)

    entity = module.find_resource_by_title('hostgroups',
                                           title=build_fqn(name, parent),
                                           failsafe=True)
    if entity:
        entity['root_pass'] = None
        if 'updated_name' in entity_dict:
            entity_dict['name'] = entity_dict.pop('updated_name')

    parameters = entity_dict.get('parameters')

    puppetclasses = entity_dict.pop('puppetclasses', None)
    current_puppetclasses = entity.pop('puppetclasses', None) if entity else []

    hostgroup = module.ensure_entity('hostgroups', entity_dict, entity)

    if hostgroup:
        scope = {'hostgroup_id': hostgroup['id']}
        module.ensure_scoped_parameters(scope, entity, parameters)

    if not module.desired_absent and 'environment_id' in hostgroup:
        if puppetclasses is not None:
            current_puppetclasses = [p['id'] for p in current_puppetclasses]
            for puppet_class_name in puppetclasses:
                scope = {'environment_id': hostgroup['environment_id']}

                # puppet classes API return puppet classes grouped by puppet module name
                puppet_modules = module.list_resource(
                    "puppetclasses",
                    params=scope,
                    search='name="{0}"'.format(puppet_class_name))

                # verify that only one puppet module is returned with only one puppet class inside
                # as provided search results have to be like "results": { "ntp": [{"id": 1, "name": "ntp" ...}]}
                # and get the puppet class id
                if len(puppet_modules) == 1 and len(
                        list(puppet_modules.values())[0]) == 1:
                    puppet_classes = list(puppet_modules.values())[0]
                    puppet_class_id = puppet_classes[0]['id']
                    if puppet_class_id in current_puppetclasses:
                        current_puppetclasses.remove(puppet_class_id)
                    else:
                        payload = {
                            'hostgroup_id': hostgroup['id'],
                            'puppetclass_id': puppet_class_id
                        }
                        module.ensure_entity('hostgroup_classes', {},
                                             None,
                                             params=payload,
                                             state='present',
                                             entity_spec={})
                else:
                    module.fail_json(msg='No data found for name="%s"' %
                                     puppet_class_name)
            if len(current_puppetclasses) > 0:
                for leftover_puppetclass in current_puppetclasses:
                    module.ensure_entity('hostgroup_classes', {},
                                         {'id': leftover_puppetclass},
                                         {'hostgroup_id': hostgroup['id']},
                                         'absent',
                                         entity_spec={})

    module.exit_json()
def main():
    module = ForemanEntityAnsibleModule(
        entity_spec=dict(
            name=dict(required=True),
            description=dict(),
            parent=dict(type='entity', flat_name='parent_id'),
            organizations=dict(type='entity_list',
                               flat_name='organization_ids'),
            locations=dict(type='entity_list', flat_name='location_ids'),
            compute_resource=dict(type='entity',
                                  flat_name='compute_resource_id'),
            compute_profile=dict(type='entity',
                                 flat_name='compute_profile_id'),
            domain=dict(type='entity', flat_name='domain_id'),
            subnet=dict(type='entity', flat_name='subnet_id'),
            subnet6=dict(type='entity', flat_name='subnet6_id'),
            realm=dict(type='entity', flat_name='realm_id'),
            architecture=dict(type='entity', flat_name='architecture_id'),
            operatingsystem=dict(type='entity',
                                 flat_name='operatingsystem_id'),
            medium=dict(aliases=['media'],
                        type='entity',
                        flat_name='medium_id'),
            ptable=dict(type='entity', flat_name='ptable_id'),
            pxe_loader=dict(choices=[
                'PXELinux BIOS', 'PXELinux UEFI', 'Grub UEFI', 'Grub2 BIOS',
                'Grub2 ELF', 'Grub2 UEFI', 'Grub2 UEFI SecureBoot',
                'Grub2 UEFI HTTP', 'Grub2 UEFI HTTPS',
                'Grub2 UEFI HTTPS SecureBoot', 'iPXE Embedded',
                'iPXE UEFI HTTP', 'iPXE Chain BIOS', 'iPXE Chain UEFI'
            ]),
            root_pass=dict(no_log=True),
            environment=dict(type='entity', flat_name='environment_id'),
            config_groups=dict(type='entity_list',
                               flat_name='config_group_ids'),
            puppet_proxy=dict(type='entity', flat_name='puppet_proxy_id'),
            puppet_ca_proxy=dict(type='entity',
                                 flat_name='puppet_ca_proxy_id'),
            openscap_proxy=dict(type='entity', flat_name='openscap_proxy_id'),
            parameters=dict(type='nested_list',
                            entity_spec=parameter_entity_spec),
            content_source=dict(type='entity', flat_name='content_source_id'),
            lifecycle_environment=dict(type='entity',
                                       flat_name='lifecycle_environment_id'),
            content_view=dict(type='entity', flat_name='content_view_id'),
        ),
        argument_spec=dict(organization=dict()),
    )
    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

    katello_params = [
        'content_source', 'lifecycle_environment', 'content_view'
    ]

    if 'organization' not in entity_dict and list(
            set(katello_params) & set(entity_dict.keys())):
        module.fail_json(
            msg="Please specify the organization when using katello parameters."
        )

    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:
        entity_dict['parent'] = module.find_resource_by_title(
            'hostgroups',
            title=parent,
            thin=True,
            failsafe=module.desired_absent)

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

    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 'compute_resource' in entity_dict:
            entity_dict['compute_resource'] = module.find_resource_by_name(
                'compute_resources',
                name=entity_dict['compute_resource'],
                failsafe=False,
                thin=True)

        if 'compute_profile' in entity_dict:
            entity_dict['compute_profile'] = module.find_resource_by_name(
                'compute_profiles',
                name=entity_dict['compute_profile'],
                failsafe=False,
                thin=True)

        if 'domain' in entity_dict:
            entity_dict['domain'] = module.find_resource_by_name(
                'domains',
                name=entity_dict['domain'],
                failsafe=False,
                thin=True)

        if 'subnet' in entity_dict:
            entity_dict['subnet'] = module.find_resource_by_name(
                'subnets',
                name=entity_dict['subnet'],
                failsafe=False,
                thin=True)

        if 'subnet6' in entity_dict:
            entity_dict['subnet6'] = module.find_resource_by_name(
                'subnets',
                name=entity_dict['subnet6'],
                failsafe=False,
                thin=True)

        if 'realm' in entity_dict:
            entity_dict['realm'] = module.find_resource_by_name(
                'realms', name=entity_dict['realm'], failsafe=False, thin=True)

        if 'architecture' in entity_dict:
            entity_dict['architecture'] = module.find_resource_by_name(
                'architectures',
                name=entity_dict['architecture'],
                failsafe=False,
                thin=True)

        if 'operatingsystem' in entity_dict:
            entity_dict['operatingsystem'] = module.find_operatingsystem(
                entity_dict['operatingsystem'], thin=True)

        if 'medium' in entity_dict:
            entity_dict['medium'] = module.find_resource_by_name(
                'media', name=entity_dict['medium'], failsafe=False, thin=True)

        if 'ptable' in entity_dict:
            entity_dict['ptable'] = module.find_resource_by_name(
                'ptables',
                name=entity_dict['ptable'],
                failsafe=False,
                thin=True)

        if 'environment' in entity_dict:
            entity_dict['environment'] = module.find_resource_by_name(
                'environments',
                name=entity_dict['environment'],
                failsafe=False,
                thin=True)

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

        for proxy in [
                'puppet_proxy', 'puppet_ca_proxy', 'openscap_proxy',
                'content_source'
        ]:
            if proxy in entity_dict:
                entity_dict[proxy] = module.find_resource_by_name(
                    'smart_proxies', entity_dict[proxy], thin=True)

        if 'organization' in entity_dict:
            if 'organizations' in entity_dict:
                if entity_dict['organization'] not in entity_dict[
                        'organizations']:
                    entity_dict['organizations'].append(
                        entity_dict['organization'])
            else:
                entity_dict['organizations'] = [entity_dict['organization']]
            entity_dict['organization'] = module.find_resource_by_name(
                'organizations',
                name=entity_dict['organization'],
                failsafe=False,
                thin=True)
            scope = {'organization_id': entity_dict['organization']['id']}

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

        if 'lifecycle_environment' in entity_dict:
            entity_dict[
                'lifecycle_environment'] = module.find_resource_by_name(
                    'lifecycle_environments',
                    name=entity_dict['lifecycle_environment'],
                    params=scope,
                    failsafe=False,
                    thin=True)

        if 'content_view' in entity_dict:
            entity_dict['content_view'] = module.find_resource_by_name(
                'content_views',
                name=entity_dict['content_view'],
                params=scope,
                failsafe=False,
                thin=True)

    entity = module.find_resource_by_title('hostgroups',
                                           title=build_fqn(name, parent),
                                           failsafe=True)
    if entity:
        entity['root_pass'] = None

    parameters = entity_dict.get('parameters')

    changed, hostgroup = module.ensure_entity('hostgroups', entity_dict,
                                              entity)

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

    module.exit_json(changed=changed)