Exemplo n.º 1
0
def find_operating_system_by_title(module, title, failsafe=False):
    response = OperatingSystem().search(set(),
                                        {'search': 'title~"{}"'.format(title)})
    return handle_find_response(
        module,
        response,
        message="No unique Operating System found with title %s" % title,
        failsafe=failsafe)
Exemplo n.º 2
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),
            operatingsystem=dict(required=True),
            template_kind=dict(required=True),
            provisioning_template=dict(required=False),
            state=dict(required=True,
                       choices=['present', 'present_with_defaults', 'absent']),
        ),
        required_if=(
            ['state', 'present', ['provisioning_template']],
            ['state', 'present_with_defaults', ['provisioning_template']],
        ),
        supports_check_mode=True,
    )

    handle_no_nailgun(module, HAS_NAILGUN_PACKAGE)

    entity_dict = dict([(k, v) for (k, v) in module.params.iteritems()
                        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)
    os_list = OperatingSystem().search(
        set(), {'search': 'title~"{}"'.format(entity_dict['operatingsystem'])})
    if len(os_list) == 0:
        module.fail_json(msg='Operating system "{}" not found.'.format(
            entity_dict['operatingsystem']))
    if len(os_list) > 1:
        module.fail_json(msg='Provided operating system ({}) is not unique.'.
                         format(entity_dict['operatingsystem']))

    entity_dict['operatingsystem'] = os_list[0]
    entity_dict['template_kind'] = find_entities_by_name(
        TemplateKind, [entity_dict['template_kind']], module)[0]

    entity = find_os_default_template(
        module,
        operatingsystem=entity_dict['operatingsystem'],
        template_kind=entity_dict['template_kind'],
        failsafe=True,
    )

    # Find Provisioning Template
    if 'provisioning_template' in entity_dict:
        if state == 'absent':
            module.fail_json(
                msg='Provisioning template must not be specified for deletion.'
            )
        entity_dict['provisioning_template'] = find_entities_by_name(
            ProvisioningTemplate, [entity_dict['provisioning_template']],
            module)[0]
        if entity_dict['provisioning_template'].template_kind.id != entity_dict[
                'template_kind'].id:
            module.fail_json(msg='Provisioning template kind mismatching.')

    entity_dict = sanitize_os_default_template_dict(entity_dict)

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

    module.exit_json(changed=changed)