Example #1
0
def main():
    module = ForemanHostgroupModule(
        foreman_spec=dict(
            name=dict(required=True),
            description=dict(),
            parent=dict(type='entity'),
            organization=dict(type='entity', required=False, ensure=False),
        ),
        argument_spec=dict(updated_name=dict(), ),
        required_by=dict(
            content_source=('organization', ),
            content_view=('organization', ),
            lifecycle_environment=('organization', ),
        ),
    )

    module_params = module.foreman_params
    with module.api_connection():
        if not module.desired_absent:
            if 'organization' in module_params:
                if 'organizations' in module_params:
                    if module_params['organization'] not in module_params[
                            'organizations']:
                        module_params['organizations'].append(
                            module_params['organization'])
                else:
                    module_params['organizations'] = [
                        module_params['organization']
                    ]
        expected_puppetclasses = module_params.pop('puppetclasses', None)
        entity = module.run()
        if not module.desired_absent and 'environment_id' in entity:
            ensure_puppetclasses(module, 'hostgroup', entity,
                                 expected_puppetclasses)
Example #2
0
def main():
    module = ForemanHostModule(
        foreman_spec=dict(
            name=dict(required=True),
            hostgroup=dict(type='entity'),
            location=dict(type='entity'),
            organization=dict(type='entity'),
            enabled=dict(type='bool'),
            managed=dict(type='bool'),
            build=dict(type='bool'),
            ip=dict(),
            mac=dict(),
            comment=dict(),
            owner=dict(type='entity', resource_type='users', flat_name='owner_id'),
            owner_group=dict(type='entity', resource_type='usergroups', flat_name='owner_id'),
            owner_type=dict(invisible=True),
            provision_method=dict(choices=['build', 'image', 'bootdisk']),
            image=dict(type='entity', scope=['compute_resource']),
            compute_attributes=dict(type='dict'),
        ),
        mutually_exclusive=[
            ['owner', 'owner_group']
        ],
        required_by=dict(
            image=('compute_resource',),
        ),
    )

    # additional param validation
    if '.' not in module.foreman_params['name']:
        module.fail_json(msg="The hostname must be FQDN")

    if not module.desired_absent:
        if 'build' in module.foreman_params and module.foreman_params['build']:
            # When 'build'=True, 'managed' has to be True. Assuming that user's priority is to build.
            if 'managed' in module.foreman_params and not module.foreman_params['managed']:
                module.warn("when 'build'=True, 'managed' is ignored and forced to True")
            module.foreman_params['managed'] = True
        elif 'build' not in module.foreman_params and 'managed' in module.foreman_params and not module.foreman_params['managed']:
            # When 'build' is not given and 'managed'=False, have to clear 'build' context that might exist on the server.
            module.foreman_params['build'] = False

        if 'mac' in module.foreman_params:
            module.foreman_params['mac'] = module.foreman_params['mac'].lower()

        if 'owner' in module.foreman_params:
            module.foreman_params['owner_type'] = 'User'
        elif 'owner_group' in module.foreman_params:
            module.foreman_params['owner_type'] = 'Usergroup'

    with module.api_connection():
        if not module.desired_absent:
            module.auto_lookup_entities()
        expected_puppetclasses = module.foreman_params.pop('puppetclasses', None)
        entity = module.run()
        if not module.desired_absent and 'environment_id' in entity:
            ensure_puppetclasses(module, 'host', entity, expected_puppetclasses)
Example #3
0
def main():
    module = ForemanHostgroupModule(
        foreman_spec=dict(
            name=dict(required=True),
            description=dict(),
            parent=dict(type='entity'),
            ansible_roles=dict(type='entity_list', ensure=False),
            organization=dict(type='entity', required=False, ensure=False),
        ),
        argument_spec=dict(
            updated_name=dict(),
        ),
        required_by=dict(
            content_source=('organization',),
            content_view=('organization',),
            lifecycle_environment=('organization',),
        ),
        required_plugins=[('ansible', ['ansible_roles'])],
    )

    module_params = module.foreman_params
    with module.api_connection():
        old_entity = module.lookup_entity('entity')
        if not module.desired_absent:
            if 'organization' in module_params:
                if 'organizations' in module_params:
                    if module_params['organization'] not in module_params['organizations']:
                        module_params['organizations'].append(module_params['organization'])
                else:
                    module_params['organizations'] = [module_params['organization']]
        expected_puppetclasses = module_params.pop('puppetclasses', None)
        entity = module.run()

        if not module.desired_absent and 'environment_id' in entity:
            ensure_puppetclasses(module, 'hostgroup', entity, expected_puppetclasses)

        ansible_roles = module_params.get('ansible_roles')
        if not module.desired_absent and ansible_roles is not None:
            desired_ansible_role_ids = [item['id'] for item in ansible_roles]
            current_ansible_role_ids = [
                item['id'] for item in module.resource_action(
                    'hostgroups', 'ansible_roles', {'id': entity['id']},
                    ignore_check_mode=True, record_change=False,
                )
            ] if old_entity else []
            if set(current_ansible_role_ids) != set(desired_ansible_role_ids):
                module.resource_action(
                    'hostgroups', 'assign_ansible_roles',
                    {'id': entity['id'], 'ansible_role_ids': desired_ansible_role_ids},
                )
Example #4
0
def main():
    module = ForemanHostModule(
        foreman_spec=dict(
            name=dict(required=True),
            hostgroup=dict(type='entity'),
            location=dict(type='entity'),
            organization=dict(type='entity'),
            enabled=dict(type='bool'),
            managed=dict(type='bool'),
            build=dict(type='bool'),
            ip=dict(),
            mac=dict(),
            comment=dict(),
            owner=dict(type='entity',
                       resource_type='users',
                       flat_name='owner_id'),
            owner_group=dict(type='entity',
                             resource_type='usergroups',
                             flat_name='owner_id'),
            owner_type=dict(invisible=True),
            provision_method=dict(choices=['build', 'image', 'bootdisk']),
            image=dict(type='entity', scope=['compute_resource']),
            compute_attributes=dict(type='dict'),
            interfaces_attributes=dict(type='nested_list',
                                       foreman_spec=interfaces_spec,
                                       ensure=True),
        ),
        mutually_exclusive=[['owner', 'owner_group']],
        required_by=dict(image=('compute_resource', ), ),
    )

    # additional param validation
    if '.' not in module.foreman_params['name']:
        module.fail_json(msg="The hostname must be FQDN")

    if not module.desired_absent:
        if 'build' in module.foreman_params and module.foreman_params['build']:
            # When 'build'=True, 'managed' has to be True. Assuming that user's priority is to build.
            if 'managed' in module.foreman_params and not module.foreman_params[
                    'managed']:
                module.warn(
                    "when 'build'=True, 'managed' is ignored and forced to True"
                )
            module.foreman_params['managed'] = True
        elif 'build' not in module.foreman_params and 'managed' in module.foreman_params and not module.foreman_params[
                'managed']:
            # When 'build' is not given and 'managed'=False, have to clear 'build' context that might exist on the server.
            module.foreman_params['build'] = False

        if 'mac' in module.foreman_params:
            module.foreman_params['mac'] = module.foreman_params['mac'].lower()

        if 'owner' in module.foreman_params:
            module.foreman_params['owner_type'] = 'User'
        elif 'owner_group' in module.foreman_params:
            module.foreman_params['owner_type'] = 'Usergroup'

    with module.api_connection():
        entity = module.lookup_entity('entity')

        if not module.desired_absent:
            module.auto_lookup_entities()

        if 'image' in module.foreman_params:
            if 'compute_attributes' not in module.foreman_params:
                module.foreman_params['compute_attributes'] = {}
            module.foreman_params['compute_attributes'][
                'image_id'] = module.foreman_params['image']['uuid']

        if 'compute_resource' in module.foreman_params:
            compute_resource = module.foreman_params['compute_resource']
            cluster = None
            if 'compute_attributes' in module.foreman_params:
                if 'cluster' in module.foreman_params['compute_attributes']:
                    cluster = module.find_cluster(
                        module.foreman_params['compute_attributes']['cluster'],
                        compute_resource)
                    module.foreman_params['compute_attributes'][
                        'cluster'] = cluster['_api_identifier']

                if 'volumes_attributes' in module.foreman_params[
                        'compute_attributes']:
                    for volume in module.foreman_params['compute_attributes'][
                            'volumes_attributes'].values():
                        if 'storage_pod' in volume:
                            storage_pod = module.find_storage_pod(
                                volume['storage_pod'], compute_resource,
                                cluster)
                            volume['storage_pod'] = storage_pod['id']
                        if 'storage_domain' in volume:
                            storage_domain = module.find_storage_domain(
                                volume['storage_domain'], compute_resource,
                                cluster)
                            volume['storage_domain'] = storage_domain['id']

            if 'interfaces_attributes' in module.foreman_params:
                for interface in module.foreman_params[
                        'interfaces_attributes']:
                    if 'compute_attributes' in interface and 'network' in interface[
                            'compute_attributes']:
                        network = module.find_network(
                            interface['compute_attributes']['network'],
                            compute_resource, cluster)
                        interface['compute_attributes']['network'] = network[
                            'id']

        # We use different APIs for creating a host with interfaces
        # and updating it, so let's differentiate based on entity being present or not
        if entity and 'interfaces_attributes' in module.foreman_params:
            interfaces = module.foreman_params.pop('interfaces_attributes')
        else:
            interfaces = None

        expected_puppetclasses = module.foreman_params.pop(
            'puppetclasses', None)

        entity = module.run()

        if not module.desired_absent:
            if 'environment_id' in entity:
                ensure_puppetclasses(module, 'host', entity,
                                     expected_puppetclasses)
            if interfaces is not None:
                ensure_host_interfaces(module, entity, interfaces)