예제 #1
0
def main():

    module = ForemanAnsibleModule(
        argument_spec=dict(
            resource=dict(choices=nailgun_entites(), required=True),
            search=dict(default=""),
        ),
        supports_check_mode=True,
    )

    (server_params, module_params) = module.parse_params()
    entity = module_params['resource']
    search = module_params['search']

    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_class = getattr(entities, entity)
    response = search_entities_json(entity_class, search)['results']

    module.exit_json(changed=False, resources=response)
def main():
    module = ForemanAnsibleModule(argument_spec=dict(
        organization=dict(required=True),
        product=dict(required=True),
        repository=dict(),
        synchronous=dict(type='bool', default=True),
    ), )

    params = module.parse_params()

    module.connect()

    try:
        organization = find_organization(module, params['organization'])
        product = find_product(module, params['product'], organization)
        if 'repository' in params:
            repository = find_repository(module, params['repository'], product)
        else:
            repository = None
    except Exception as e:
        module.fail_json(msg='Failed to find entity: %s ' % e)

    try:
        if repository is None:
            changed = product.sync(params['synchronous'])
        else:
            changed = repository.sync(params['synchronous'])
        module.exit_json(changed=changed)
    except Exception as e:
        module.fail_json(msg=e)
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())
예제 #4
0
def main():

    module = ForemanAnsibleModule(
        argument_spec=dict(
            resource=dict(type='str', required=True),
            search=dict(default=""),
            full_details=dict(type='bool', aliases=['info'], default='false'),
            params=dict(type='dict'),
            organization=dict(),
        ),
    )

    module_params = module.clean_params()
    resource = module_params['resource']
    search = module_params['search']
    params = module_params.get('params', {})

    module.connect()

    if resource not in module.foremanapi.resources:
        msg = "Resource '{0}' does not exist in the API. Existing resources: {1}".format(resource, ', '.join(sorted(module.foremanapi.resources)))
        module.fail_json(msg=msg)
    if 'organization' in module_params:
        params['organization_id'] = module.find_resource_by_name('organizations', module_params['organization'], thin=True)['id']

    response = module.list_resource(resource, search, params)

    if module_params['full_details']:
        resources = []
        for found_resource in response:
            resources.append(module.show_resource(resource, found_resource['id']))
    else:
        resources = response

    module.exit_json(resources=resources)
예제 #5
0
def main():
    module = ForemanAnsibleModule(
        argument_spec=dict(
            src=dict(required=True, type='path', aliases=['file']),
            repository=dict(required=True),
            product=dict(required=True),
            organization=dict(required=True),
        ),
        supports_check_mode=True,
    )

    entity_dict = module.parse_params()

    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'])
    entity_dict['repository'] = find_repository(module,
                                                name=entity_dict['repository'],
                                                product=entity_dict['product'])

    content_unit = None
    if entity_dict['repository'].content_type == "yum":
        name, version, release, arch = check_output(
            "rpm --queryformat '%%{NAME} %%{VERSION} %%{RELEASE} %%{ARCH}' -qp %s"
            % entity_dict['src'],
            shell=True).decode('ascii').split()
        query = "name = \"{}\" and version = \"{}\" and release = \"{}\" and arch = \"{}\"".format(
            name, version, release, arch)
        content_unit = find_package(module,
                                    query,
                                    repository=entity_dict['repository'],
                                    failsafe=True)
    elif entity_dict['repository'].content_type == "file":
        h = hashlib.sha256()
        with open(entity_dict['src'], "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                h.update(chunk)
        checksum = h.hexdigest()
        name = os.path.basename(entity_dict['src'])
        query = "name = \"{}\" and checksum = \"{}\"".format(name, checksum)
        content_unit = find_file(module,
                                 query,
                                 repository=entity_dict['repository'],
                                 failsafe=True)

    changed = False
    if not content_unit:
        try:
            changed = upload(module, entity_dict['src'],
                             entity_dict['repository'])
        except Exception as e:
            module.fail_json(msg=to_native(e))

    module.exit_json(changed=changed)
def main():
    module = ForemanAnsibleModule(argument_spec=dict(
        name=dict(required=True),
        organization=dict(required=True),
        interval=dict(required=True),
        enabled=dict(required=True),
        sync_date=dict(required=True),
        products=dict(type='list', default=[]),
    ),
                                  supports_check_mode=True)

    (server_params, module_params) = module.parse_params()

    name = module_params['name']
    organization = module_params['organization']
    interval = module_params['interval']
    enabled = module_params['enabled']
    sync_date = datetime.strptime(module_params['sync_date'],
                                  '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.UTC)
    products = module_params['products']

    (server_url, username, password, verify_ssl) = server_params
    server = ServerConfig(url=server_url,
                          auth=(username, password),
                          verify=verify_ssl)
    ng = NailGun(server, entities, module)

    # Lets make an connection to the server with username and password
    try:
        org = entities.Organization(server)
        org.search()
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    try:
        changed = ng.sync_plan(name,
                               organization,
                               interval=interval,
                               enabled=enabled,
                               sync_date=sync_date,
                               products=products)
        module.exit_json(changed=changed)
    except Exception as e:
        module.fail_json(msg=e)
def main():
    module = ForemanAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            description=dict(),
            repositories=dict(type='list', default=[]),
            inclusion=dict(type='bool', default=False),
            content_view=dict(required=True),
            filter_type=dict(
                required=True,
                choices=['rpm', 'package_group', 'erratum', 'docker']),
            organization=dict(required=True),
            filter_state=dict(default='present', choices=['present',
                                                          'absent']),
            rule_state=dict(default='present', choices=['present', 'absent']),
            rule_name=dict(aliases=['package_name', 'package_group', 'tag']),
            date_type=dict(default='updated', choices=['issued', 'updated']),
            end_date=dict(),
            errata_id=dict(),
            max_version=dict(),
            min_version=dict(),
            start_date=dict(),
            types=dict(default=["bugfix", "enhancement", "security"],
                       type='list'),
            version=dict(),
        ),
        supports_check_mode=False,
    )

    (server_params, entity_dict) = module.parse_params()
    filter_state = entity_dict.pop('filter_state')
    rule_state = entity_dict.pop('rule_state')

    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)

    organization = find_organization(module,
                                     name=entity_dict.pop('organization'))
    entity_dict['content_view'] = find_content_view(
        module, name=entity_dict['content_view'], organization=organization)
    if len(entity_dict['repositories']) > 0:
        entity_dict['repositories'] = find_repositories(
            module, entity_dict['repositories'], organization)

    content_view_filter_entity = find_content_view_filter(
        module,
        name=entity_dict['name'],
        content_view=entity_dict['content_view'],
        failsafe=True)
    content_view_filter_dict = sanitize_entity_dict(entity_dict,
                                                    content_filter_map)

    content_view_filter_changed = naildown_entity_state(
        AbstractContentViewFilter, content_view_filter_dict,
        content_view_filter_entity, filter_state, module)

    if entity_dict['filter_type'] == 'erratum':
        entity_dict['rule_name'] = None
    elif 'rule_name' not in entity_dict:
        entity_dict['rule_name'] = entity_dict['name']

    # Find content_view_filter again as it may have just been created
    entity_dict['content_view_filter'] = find_content_view_filter(
        module,
        name=entity_dict['name'],
        content_view=entity_dict['content_view'],
        failsafe=True)

    if entity_dict['content_view_filter'] is not None:
        if 'errata_id' in entity_dict:
            rule_map = content_filter_rule_erratum_id_map
            entity_dict['errata'] = find_errata(module,
                                                id=entity_dict['errata_id'],
                                                organization=organization)
            content_view_filter_rule_entity = find_content_view_filter_rule(
                module,
                content_view_filter=entity_dict['content_view_filter'],
                errata=entity_dict['errata'],
                failsafe=True)
        else:
            rule_map = globals()['content_filter_rule_%s_map' %
                                 (entity_dict['filter_type'])]
            content_view_filter_rule_entity = find_content_view_filter_rule(
                module,
                content_view_filter=entity_dict['content_view_filter'],
                name=entity_dict['rule_name'],
                failsafe=True)

        if entity_dict['filter_type'] == 'package_group':
            entity_dict['uuid'] = find_package_group(
                module, name=entity_dict['rule_name']).uuid

        content_view_filter_rule_dict = sanitize_entity_dict(
            entity_dict, rule_map)
        check_missing = [
            'min_version', 'max_version', 'version', 'start_date', 'end_date',
            'architecture', 'date_type'
        ]
        content_view_filter_rule_changed = naildown_entity_state(
            ContentViewFilterRule, content_view_filter_rule_dict,
            content_view_filter_rule_entity, rule_state, module, check_missing)
        changed = content_view_filter_changed or content_view_filter_rule_changed
    else:
        changed = content_view_filter_changed

    module.exit_json(changed=changed)