Ejemplo 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):
    if has_import_error:
        module.fail_json(msg=import_error_msg)

    name = module.params.get('name')
    state = module.params.get('state')
    provider = module.params.get('provider').title()
    description = module.params.get('description')
    locations = module.params.get('locations')
    organizations = module.params.get('organizations')

    cement.create_server(
        server_url=module.params.get('server_url'),
        auth=(module.params.get('username'), module.params.get('password')),
        verify_ssl=module.params.get('verify_ssl'),
    )

    cement.ping_server(module)

    data = {'name': name, 'description': description}

    compute_resource = cement.find_compute_resource(module,
                                                    name=data.get('name'),
                                                    failsafe=True)

    if organizations:
        data['organization'] = [
            cement.find_organization(module, organization)
            for organization in organizations
        ]

    if locations:
        data['location'] = [
            cement.find_location(module, location) for location in locations
        ]

    data['provider'] = provider
    provider_params = get_provider_params(provider=provider)

    if state in ['present', 'present_with_defaults']:
        if not provider_params and not compute_resource:
            module.fail_json(
                msg=
                'To create a compute resource a valid provider must be supplied'
            )

        for key in provider_params.get('credentials'):
            data.__setitem__(key, module.params.get('provider_auth').get(key))

        for key in provider_params.get('params'):
            if key not in module.params:
                module.fail_json(msg='missing required param {}'.format(key))

            data.__setitem__(key, module.params.get(key))

    changed = cement.naildown_entity_state(provider_params.get('class'), data,
                                           compute_resource, state, module)

    return changed
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 = 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(),
            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)
    try:
        entities = find_entities(Location, name=entity_dict['name'])
        if len(entities) > 0:
            entity = entities[0]
        else:
            entity = None
    except Exception as e:
        module.fail_json(msg='Failed to find entity: %s ' % e)

    parent = entity_dict.pop('parent', None)
    if parent:
        entity_dict['parent'] = find_location(module, parent)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

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

    module.exit_json(changed=changed)
Ejemplo n.º 6
0
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            hostgroup=dict(),
            location=dict(),
            organization=dict(),
            enabled=dict(default='true', type='bool'),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        required_if=(
            ['state', 'present_with_defaults', ['hostgroup']],
            ['state', 'present', ['hostgroup']],
        ),
        supports_check_mode=True,
    )

    (host_dict, state) = module.parse_params()

    module.connect()

    host_dict['hostgroup'] = find_hostgroup(module,
                                            host_dict['hostgroup'],
                                            failsafe=True)

    host_dict['name'] = host_dict['name'] + '.' + \
        host_dict['hostgroup'].domain.read().fullname

    entity = find_host(module, host_dict['name'], failsafe=True)

    if 'location' in host_dict:
        host_dict['location'] = find_location(module, host_dict['location'])

    if 'organization' in host_dict:
        host_dict['organization'] = find_organization(
            module, host_dict['organization'])

    host_dict = sanitize_entity_dict(host_dict, name_map)
    changed = naildown_entity_state(Host, host_dict, entity, state, module)

    module.exit_json(changed=changed)