def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            content_type=dict(required=True, choices=['gpg_key', 'cert']),
            content=dict(required=True),
        ),
        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)

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])
    entity = find_content_credential(module,
                                     name=entity_dict['name'],
                                     organization=entity_dict['organization'],
                                     failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

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

    module.exit_json(changed=changed)
def main(module):

    (server_params, module_params, state) = module.parse_params()
    name = module_params.get('name')
    updated_name = module_params.get('updated_name')

    (server_url, username, password, verify_ssl) = server_params
    cement.create_server(
        server_url=server_url,
        auth=(username, password),
        verify_ssl=verify_ssl,
    )

    cement.ping_server(module)

    data = {'name': name}

    compute_profile = cement.find_compute_profile(module,
                                                  name=name,
                                                  failsafe=True)

    if state == 'present' and updated_name:
        data['name'] = updated_name

    data = cement.sanitize_entity_dict(data, name_map)

    changed = cement.naildown_entity_state(nailgun.entities.ComputeProfile,
                                           data, compute_profile, state,
                                           module)

    return changed
def main():
    module = ForemanAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            value=dict(),
        ),
        supports_check_mode=True,
    )

    (server_params, entity_dict) = 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)

    entity = find_setting(
        module,
        name=entity_dict['name'],
        failsafe=False,
    )

    if 'value' not in entity_dict:
        entity_dict['value'] = entity.default or ""

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed, entity = naildown_entity(Setting, entity_dict, entity, 'present', module, check_type=True)

    module.exit_json(changed=changed, foreman_setting=entity.to_json_dict())
Example #4
0
def main():
    module = ForemanAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            value=dict(),
        ),
        supports_check_mode=True,
    )

    entity_dict = module.parse_params()

    module.connect()

    entity = find_setting(
        module,
        name=entity_dict['name'],
        failsafe=False,
    )

    if 'value' not in entity_dict:
        entity_dict['value'] = entity.default or ""

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed, entity = naildown_entity(Setting,
                                      entity_dict,
                                      entity,
                                      'present',
                                      module,
                                      check_type=True)

    module.exit_json(changed=changed, foreman_setting=entity.to_json_dict())
def main(module):
    if has_import_error:
        module.fail_json(msg=import_error_msg)

    updated_name = module.params.get('updated_name')
    state = module.params.get('state')

    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': module.params.get('name')
    }

    compute_profile = cement.find_compute_profile(module, name=data.get('name'), failsafe=True)

    if state == 'present' and updated_name:
        data['name'] = updated_name

    data = cement.sanitize_entity_dict(data, name_map)

    changed = cement.naildown_entity_state(nailgun.entities.ComputeProfile, data, compute_profile, state, module)

    return changed
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            label=dict(),
        ),
        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)
    entity = find_organization(module, name=entity_dict['name'], failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

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

    module.exit_json(changed=changed)
Example #7
0
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            value=dict(),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        required_if=(
            ['state', 'present_with_defaults', ['value']],
            ['state', 'present', ['value']],
        ),
    )

    (global_parameter_dict, state) = module.parse_params()

    module.connect()

    try:
        entities = find_entities(CommonParameter,
                                 name=global_parameter_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)

    global_parameter_dict = sanitize_entity_dict(global_parameter_dict,
                                                 name_map)

    changed = naildown_entity_state(CommonParameter, global_parameter_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),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            name=dict(required=True),
            description=dict(),
            dns_proxy=dict(),
            locations=dict(type='list'),
            organizations=dict(type='list'),
            state=dict(choices=['present', 'absent'], default='present'),
        ),
        supports_check_mode=True,
    )

    if has_import_error:
        module.fail_json(msg=import_error_msg)

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

    server_url = domain_dict.pop('server_url')
    username = domain_dict.pop('username')
    password = domain_dict.pop('password')
    verify_ssl = domain_dict.pop('verify_ssl')
    state = domain_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:
        # Try to find the Domain to work on
        entity = find_domain(module, name=domain_dict['name'], failsafe=True)
    except Exception as e:
        module.fail_json(msg='Failed to find entity: %s ' % e)

    if 'dns_proxy' in domain_dict:
        domain_dict['dns_proxy'] = find_smart_proxy(module,
                                                    domain_dict['dns_proxy'])

    if 'locations' in domain_dict:
        domain_dict['locations'] = find_locations(module,
                                                  domain_dict['locations'])

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

    domain_dict = sanitize_entity_dict(domain_dict, name_map)

    changed = naildown_entity_state(Domain, domain_dict, entity, state, module)

    module.exit_json(changed=changed)
Example #9
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)
Example #10
0
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            product=dict(required=True),
            label=dict(),
            name=dict(required=True),
            content_type=dict(
                required=True,
                choices=['docker', 'ostree', 'yum', 'puppet', 'file', 'deb']),
            url=dict(),
            gpg_key=dict(),
            docker_upstream_name=dict(),
            download_policy=dict(
                choices=['background', 'immediate', 'on_demand']),
            mirror_on_sync=dict(type='bool', default=True),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        supports_check_mode=True,
    )

    (entity_dict, state) = module.parse_params()

    if entity_dict[
            'content_type'] != 'docker' and 'docker_upstream_name' in entity_dict:
        module.fail_json(
            msg=
            "docker_upstream_name should not be set unless content_type: docker"
        )

    module.connect()

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])

    entity_dict['product'] = find_product(
        module,
        name=entity_dict['product'],
        organization=entity_dict['organization'])

    if 'gpg_key' in entity_dict:
        entity_dict['gpg_key'] = find_content_credential(
            module,
            name=entity_dict['gpg_key'],
            organization=entity_dict['organization'])

    entity = find_repository(module,
                             name=entity_dict['name'],
                             product=entity_dict['product'],
                             failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

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

    module.exit_json(changed=changed)
Example #11
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),
            product=dict(required=True),
            organization=dict(required=True),
            name=dict(required=True),
            content_type=dict(required=True),
            url=dict(),
            download_policy=dict(
                choices=['background', 'immediate', 'on_demand']),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        supports_check_mode=True,
    )

    handle_no_nailgun(module, HAS_NAILGUN_PACKAGE)

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

    server_url = entity_dict.pop('server_url')
    verify_ssl = entity_dict.pop('verify_ssl')
    username = entity_dict.pop('username')
    password = entity_dict.pop('password')
    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)

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])

    entity_dict['product'] = find_product(
        module,
        name=entity_dict['product'],
        organization=entity_dict['organization'])

    entity = find_repository(module,
                             name=entity_dict['name'],
                             product=entity_dict['product'],
                             failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Repository, 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),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            name=dict(required=True),
            value=dict(),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        required_if=(
            ['state', 'present_with_defaults', ['value']],
            ['state', 'present', ['value']],
        ),
        supports_check_mode=True,
    )

    if has_import_error:
        module.fail_json(msg=import_error_msg)

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

    server_url = global_parameter_dict.pop('server_url')
    username = global_parameter_dict.pop('username')
    password = global_parameter_dict.pop('password')
    verify_ssl = global_parameter_dict.pop('verify_ssl')
    state = global_parameter_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(CommonParameter,
                                 name=global_parameter_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)

    global_parameter_dict = sanitize_entity_dict(global_parameter_dict,
                                                 name_map)

    changed = naildown_entity_state(CommonParameter, global_parameter_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),
            product=dict(required=True),
            organization=dict(required=True),
            name=dict(required=True),
            content_type=dict(required=True, choices=['docker', 'ostree', 'yum', 'puppet', 'file', 'deb']),
            url=dict(),
            docker_upstream_name=dict(),
            download_policy=dict(choices=['background', 'immediate', 'on_demand']),
            state=dict(default='present', choices=['present_with_defaults', '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')
    verify_ssl = entity_dict.pop('verify_ssl')
    username = entity_dict.pop('username')
    password = entity_dict.pop('password')
    state = entity_dict.pop('state')

    if entity_dict['content_type'] != 'docker' and 'docker_upstream_name' in entity_dict:
        module.fail_json(msg="docker_upstream_name should not be set unless content_type: docker")

    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)

    entity_dict['organization'] = find_organization(module, name=entity_dict['organization'])

    entity_dict['product'] = find_product(module, name=entity_dict['product'], organization=entity_dict['organization'])

    entity = find_repository(module, name=entity_dict['name'], product=entity_dict['product'], failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Repository, 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),
            organization=dict(required=True),
            repositories=dict(type='list'),
            state=dict(default='present',
                       choices=['present_with_defaults', '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)

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])
    if 'repositories' in entity_dict:
        entity_dict['repositories'] = find_repositories(
            module, entity_dict['repositories'], entity_dict['organization'])

    content_view_entity = find_content_view(
        module,
        name=entity_dict['name'],
        organization=entity_dict['organization'],
        failsafe=True)
    content_view_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(ContentView, content_view_dict,
                                    content_view_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,
    )

    handle_no_nailgun(module, HAS_NAILGUN_PACKAGE)

    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)
Example #16
0
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            locations=dict(type='list'),
            organizations=dict(type='list'),
            operatingsystems=dict(type='list'),
            os_family=dict(),
            path=dict(),
        ),
        supports_check_mode=True,
    )

    (server_params, medium_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)

    entity = find_installation_medium(module,
                                      name=medium_dict['name'],
                                      failsafe=True)

    if 'operatingsystems' in medium_dict:
        medium_dict['operatingsystems'] = find_operating_systems_by_title(
            module, medium_dict['operatingsystems'])
        if len(medium_dict['operatingsystems']
               ) == 1 and 'os_family' not in medium_dict and entity is None:
            medium_dict['os_family'] = medium_dict['operatingsystems'][
                0].family

    if 'locations' in medium_dict:
        medium_dict['locations'] = find_locations(module,
                                                  medium_dict['locations'])

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

    medium_dict = sanitize_entity_dict(medium_dict, name_map)

    changed = naildown_entity_state(Media, medium_dict, entity, state, module)

    module.exit_json(changed=changed)
Example #17
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            name=dict(required=True),
            value=dict(),
        ),
        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')

    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)

    entity = find_setting(
        module,
        name=entity_dict['name'],
        failsafe=False,
    )

    if 'value' not in entity_dict:
        entity_dict['value'] = entity.default or ""

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed, entity = naildown_entity(Setting, entity_dict, entity, 'present',
                                      module)

    module.exit_json(changed=changed, foreman_setting=entity.to_json_dict())
Example #18
0
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            updated_name=dict(),
            compute_attributes=dict(type='list'),
        ),
        supports_check_mode=True,
    )

    (compute_profile_dict, state) = module.parse_params()
    name = compute_profile_dict.get('name')
    updated_name = compute_profile_dict.get('updated_name')
    compute_attributes = compute_profile_dict.pop('compute_attributes', list())

    if len(compute_attributes) > 0 and state == 'absent':
        module.fail_json(
            msg='compute_attributes not allowed when state=absent')

    module.connect()

    try:
        # Try to find the compute_profile to work on
        compute_profile = find_compute_profile(
            module, name=compute_profile_dict['name'], failsafe=True)
    except Exception as e:
        module.fail_json(msg='Failed to find entity: %s ' % e)

    if state == 'present' and updated_name:
        compute_profile_dict['name'] = updated_name

    compute_profile_dict = sanitize_entity_dict(compute_profile_dict, name_map)

    (changed,
     compute_profile) = naildown_entity(ComputeProfile, compute_profile_dict,
                                        compute_profile, state, module)

    # Apply changes on underlying compute attributes only when present
    if state == 'present':
        # Update or create compute attributes
        for compute_attribute_dict in compute_attributes:
            changed |= compute_attribute(module, compute_profile,
                                         compute_attribute_dict)

    module.exit_json(changed=changed)
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            label=dict(),
            gpg_key=dict(),
            sync_plan=dict(),
            description=dict(),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        supports_check_mode=True,
    )

    (entity_dict, state) = module.parse_params()

    module.connect()

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])

    if 'gpg_key' in entity_dict:
        entity_dict['gpg_key'] = find_content_credential(
            module,
            name=entity_dict['gpg_key'],
            organization=entity_dict['organization'])

    if 'sync_plan' in entity_dict:
        entity_dict['sync_plan'] = find_sync_plan(
            module,
            name=entity_dict['sync_plan'],
            organization=entity_dict['organization'])

    entity = find_product(module,
                          name=entity_dict['name'],
                          organization=entity_dict['organization'],
                          failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

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

    module.exit_json(changed=changed)
Example #20
0
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            description=dict(),
            dns_proxy=dict(),
            locations=dict(type='list'),
            organizations=dict(type='list'),
        ),
        supports_check_mode=True,
    )

    (server_params, domain_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)
    try:
        # Try to find the Domain to work on
        entity = find_domain(module, name=domain_dict['name'], failsafe=True)
    except Exception as e:
        module.fail_json(msg='Failed to find entity: %s ' % e)

    if 'dns_proxy' in domain_dict:
        domain_dict['dns_proxy'] = find_smart_proxy(module,
                                                    domain_dict['dns_proxy'])

    if 'locations' in domain_dict:
        domain_dict['locations'] = find_locations(module,
                                                  domain_dict['locations'])

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

    domain_dict = sanitize_entity_dict(domain_dict, name_map)

    changed = naildown_entity_state(Domain, domain_dict, entity, state, module)

    module.exit_json(changed=changed)
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)
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            locations=dict(type='list'),
            organizations=dict(type='list'),
            operatingsystems=dict(type='list'),
            os_family=dict(),
            path=dict(),
        ),
        supports_check_mode=True,
    )

    (medium_dict, state) = module.parse_params()

    module.connect()

    entity = find_installation_medium(module,
                                      name=medium_dict['name'],
                                      failsafe=True)

    if 'operatingsystems' in medium_dict:
        medium_dict['operatingsystems'] = find_operating_systems_by_title(
            module, medium_dict['operatingsystems'])
        if len(medium_dict['operatingsystems']
               ) == 1 and 'os_family' not in medium_dict and entity is None:
            medium_dict['os_family'] = medium_dict['operatingsystems'][
                0].family

    if 'locations' in medium_dict:
        medium_dict['locations'] = find_locations(module,
                                                  medium_dict['locations'])

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

    medium_dict = sanitize_entity_dict(medium_dict, name_map)

    changed = naildown_entity_state(Media, medium_dict, entity, state, module)

    module.exit_json(changed=changed)
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            value=dict(),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        required_if=(
            ['state', 'present_with_defaults', ['value']],
            ['state', 'present', ['value']],
        ),
        supports_check_mode=True,
    )

    (server_params, global_parameter_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)
    try:
        entities = find_entities(CommonParameter,
                                 name=global_parameter_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)

    global_parameter_dict = sanitize_entity_dict(global_parameter_dict,
                                                 name_map)

    changed = naildown_entity_state(CommonParameter, global_parameter_dict,
                                    entity, state, module)

    module.exit_json(changed=changed)
Example #24
0
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            label=dict(),
        ),
        supports_check_mode=True,
    )

    (entity_dict, state) = module.parse_params()

    module.connect()

    entity = find_organization(module, name=entity_dict['name'], failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Organization, 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,
    )

    (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 = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            locations=dict(type='list'),
            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)

    try:
        entity = find_environment(module, name=entity_dict['name'], failsafe=True)
    except Exception as e:
        module.fail_json(msg='Failed to find entity: %s ' % e)

    # Set Locations of partition table
    if 'locations' in entity_dict:
        entity_dict['locations'] = find_locations(module, entity_dict['locations'])

    # Set Organizations of partition table
    if 'organizations' in entity_dict:
        entity_dict['organizations'] = find_organizations(module, entity_dict['organizations'])

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Environment, 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),
            audit_comment=dict(),
            kind=dict(choices=[
                'finish',
                'iPXE',
                'job_template',
                'POAP',
                'provision',
                'ptable',
                'PXELinux',
                'PXEGrub',
                'PXEGrub2',
                'script',
                'snippet',
                'user_data',
                'ZTP',
            ]),
            template=dict(),
            file_name=dict(type='path'),
            locations=dict(type='list'),
            locked=dict(type='bool', default=False),
            name=dict(),
            organizations=dict(type='list'),
            operatingsystems=dict(type='list'),
            state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']),
        ),
        supports_check_mode=True,
        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: *'!")

    handle_no_nailgun(module, HAS_NAILGUN_PACKAGE)

    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')
    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.')

    name = entity_dict['name']

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

    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:
        if affects_multiple:
            entities = find_entities(ProvisioningTemplate)
        else:
            entities = find_entities(ProvisioningTemplate, name=entity_dict['name'])
    except Exception as e:
        module.fail_json(msg='Failed to search for entities: %s ' % e)

    # Set Locations of Template
    if 'locations' in entity_dict:
        entity_dict['locations'] = find_entities_by_name(
            Location, entity_dict['locations'], module)

    # Set Organizations of Template
    if 'organizations' in entity_dict:
        entity_dict['organizations'] = find_entities_by_name(
            Organization, entity_dict['organizations'], module)

    if 'operatingsystems' in entity_dict:
        entity_dict['operatingsystems'] = [find_operating_system_by_title(module, title)
                                           for title in entity_dict['operatingsystems']]

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

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = False
    if not affects_multiple:
        if len(entities) == 0:
            entity = None
        else:
            entity = entities[0]
        changed = naildown_entity_state(
            ProvisioningTemplate, entity_dict, entity, state, module)
    else:
        entity_dict.pop('name')
        for entity in entities:
            changed |= naildown_entity_state(
                ProvisioningTemplate, entity_dict, entity, state, module)

    module.exit_json(changed=changed)
Example #29
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            # Foreman credentials
            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),
            # Entity parameter
            audit_comment=dict(),
            description_format=dict(),
            # effectice_user=dict(type='dict'),
            file_name=dict(type='path'),
            job_category=dict(),
            locations=dict(type='list'),
            locked=dict(type='bool', default=False),
            name=dict(),
            organizations=dict(type='list'),
            provider_type=dict(default='SSH'),
            snippet=dict(type='bool'),
            template=dict(),
            template_inputs=dict(type='list'),
            # Control parameter
            state=dict(default='present',
                       choices=['absent', 'present_with_defaults', 'present']),
        ),
        supports_check_mode=True,
        mutually_exclusive=[
            ['file_name', 'template'],
        ],
        required_one_of=[
            ['name', 'file_name', 'template'],
        ],
    )
    # We do not want a layout 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: *'!")

    if HAS_IMPORT_ERROR:
        module.fail_json(msg=IMPORT_ERROR)

    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')
    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 job 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.')

    name = entity_dict['name']

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

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

    # Set Locations of job template
    if 'locations' in entity_dict:
        entity_dict['locations'] = find_entities_by_name(
            Location, entity_dict['locations'], module)

    # Set Organizations of job template
    if 'organizations' in entity_dict:
        entity_dict['organizations'] = find_entities_by_name(
            Organization, entity_dict['organizations'], module)

    # TemplateInputs need to be added as separate entities later
    template_input_list = entity_dict.get('template_inputs', [])

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = False
    if not affects_multiple:
        if entities:
            entity = entities[0]
        else:
            entity = None
        changed, result = naildown_entity(JobTemplate, entity_dict, entity,
                                          state, module)

        if state in ("present", "present_with_defaults"):

            # Manage TemplateInputs here
            for template_input_dict in template_input_list:
                template_input_dict = template_input_dict.copy()

                # assign template_input to a template
                template_input_dict['template'] = result

                ti_entity = find_template_input(
                    module, str(template_input_dict['name']), result)

                changed |= naildown_entity_state(TemplateInput,
                                                 template_input_dict,
                                                 ti_entity, state, module)

            # remove template inputs if they aren't present in template_input_list
            found_tis = find_entities(
                entity_class=lambda: TemplateInput(template=result))
            template_input_names = set(ti['name']
                                       for ti in template_input_list)
            for ti in found_tis:
                if ti.name not in template_input_names:
                    changed |= naildown_entity_state(TemplateInput, None, ti,
                                                     "absent", module)

    else:
        entity_dict.pop('name')
        for entity in entities:
            changed |= naildown_entity_state(JobTemplate, entity_dict, entity,
                                             state, module)

    module.exit_json(changed=changed)
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            new_name=dict(),
            lifecycle_environment=dict(),
            content_view=dict(),
            subscriptions=dict(type='list'),
            host_collections=dict(type='list'),
            content_overrides=dict(type='list'),
            auto_attach=dict(type='bool', default=True),
            state=dict(default='present',
                       choices=[
                           'present', 'present_with_defaults', 'absent',
                           'copied'
                       ]),
        ),
        supports_check_mode=True,
        required_if=[
            ['state', 'copied', ['new_name']],
        ],
    )

    (entity_dict, state) = module.parse_params()

    module.connect()

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])
    if 'lifecycle_environment' in entity_dict:
        entity_dict['lifecycle_environment'] = find_lifecycle_environment(
            module, entity_dict['lifecycle_environment'],
            entity_dict['organization'])

    if 'content_view' in entity_dict:
        entity_dict['content_view'] = find_content_view(
            module, entity_dict['content_view'], entity_dict['organization'])

    if 'host_collections' in entity_dict:
        entity_dict['host_collections'] = find_host_collections(
            module, entity_dict['host_collections'],
            entity_dict['organization'])

    activation_key_entity = find_activation_key(
        module,
        name=entity_dict['name'],
        organization=entity_dict['organization'],
        failsafe=True)

    activation_key_dict = sanitize_entity_dict(entity_dict, name_map)

    try:
        changed, activation_key_entity = naildown_entity(
            ActivationKey, activation_key_dict, activation_key_entity, state,
            module)

        # only update subscriptions of newly created or updated AKs
        # copied keys inherit the subscriptions of the origin, so one would not have to specify them again
        # deleted keys don't need subscriptions anymore either
        if state == 'present' or (state == 'present_with_defaults'
                                  and changed):
            if 'subscriptions' in entity_dict:
                subscriptions = entity_dict['subscriptions']
                desired_subscription_ids = set(
                    s.id for s in find_subscriptions(
                        module, subscriptions, entity_dict['organization']))
                current_subscriptions = [
                    Subscription(**result)
                    for result in Subscription().search_normalize(
                        activation_key_entity.subscriptions()['results'])
                ]
                current_subscription_ids = set(s.id
                                               for s in current_subscriptions)

                if desired_subscription_ids != current_subscription_ids:
                    if not module.check_mode:
                        for subscription_id in (desired_subscription_ids -
                                                current_subscription_ids):
                            activation_key_entity.add_subscriptions(
                                data={
                                    'quantity': 1,
                                    'subscription_id': subscription_id
                                })
                        for subscription_id in (current_subscription_ids -
                                                desired_subscription_ids):
                            activation_key_entity.remove_subscriptions(
                                data={'subscription_id': subscription_id})
                    changed = True

            if 'content_overrides' in entity_dict:
                content_overrides = entity_dict['content_overrides']
                product_content = activation_key_entity.product_content()
                current_content_overrides = set(
                    (product['content']['label'],
                     product['enabled_content_override'])
                    for product in product_content['results']
                    if product['enabled_content_override'] is not None)
                desired_content_overrides = set(
                    (product['label'],
                     override_to_boolnone(product['override']))
                    for product in content_overrides)

                if desired_content_overrides != current_content_overrides:
                    if not module.check_mode:
                        for (
                                label, override
                        ) in current_content_overrides - desired_content_overrides:
                            activation_key_entity.content_override(
                                data={
                                    'content_override': {
                                        'content_label': label,
                                        'value': 'default'
                                    }
                                })
                        for (
                                label, override
                        ) in desired_content_overrides - current_content_overrides:
                            activation_key_entity.content_override(
                                data={
                                    'content_override': {
                                        'content_label': label,
                                        'value': str(override).lower()
                                    }
                                })
                    changed = True

        module.exit_json(changed=changed)
    except Exception as e:
        module.fail_json(msg=e)