Generate an icinga2 ticket on the master.

    name
        The domain name for which this ticket will be generated

    output
        grain: output in a grain
        other: the file to store results
        None:  output to the result comment (default)

    grain:
        grain to store the output (need output=grain)

    key:
        the specified grain will be treated as a dictionary, the result
        of this state will be stored under the specified key.

    overwrite:
        The file or grain will be overwritten if it already exists (default)

def generate_ticket(name, output=None, grain=None, key=None, overwrite=True):
    '''
    Generate an icinga2 ticket on the master.

    name
        The domain name for which this ticket will be generated

    output
        grain: output in a grain
        other: the file to store results
        None:  output to the result comment (default)

    grain:
        grain to store the output (need output=grain)

    key:
        the specified grain will be treated as a dictionary, the result
        of this state will be stored under the specified key.

    overwrite:
        The file or grain will be overwritten if it already exists (default)
    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}

    # Checking if execution is needed.
    if output == 'grain':
        if grain and not key:
            if not overwrite and grain in __salt__['grains.ls']():
                ret['comment'] = 'No execution needed. Grain {0} already set'.format(grain)
                return ret
            elif __opts__['test']:
                ret['result'] = None
                ret['comment'] = 'Ticket generation would be executed, storing result in grain: {0}'.format(grain)
                return ret
        elif grain:
            if grain in __salt__['grains.ls']():
                grain_value = __salt__['grains.get'](grain)
            else:
                grain_value = {}
            if not overwrite and key in grain_value:
                ret['comment'] = 'No execution needed. Grain {0}:{1} already set'.format(grain, key)
                return ret
            elif __opts__['test']:
                ret['result'] = None
                ret['comment'] = 'Ticket generation would be executed, storing result in grain: {0}:{1}'.format(grain, key)
                return ret
        else:
            ret['result'] = False
            ret['comment'] = "Error: output type 'grain' needs the grain parameter\n"
            return ret
    elif output:
        if not overwrite and os.path.isfile(output):
            ret['comment'] = 'No execution needed. File {0} already set'.format(output)
            return ret
        elif __opts__['test']:
            ret['result'] = None
            ret['comment'] = 'Ticket generation would be executed, storing result in file: {0}'.format(output)
            return ret
    elif __opts__['test']:
        ret['result'] = None
        ret['comment'] = 'Ticket generation would be executed, not storing result'
        return ret

    # Executing the command.
    ticket_res = __salt__['icinga2.generate_ticket'](name)
    ticket = ticket_res['stdout']
    if not ticket_res['retcode']:
        ret['comment'] = six.text_type(ticket)

    if output == 'grain':
        if grain and not key:
            __salt__['grains.setval'](grain, ticket)
            ret['changes']['ticket'] = "Executed. Output into grain: {0}".format(grain)
        elif grain:
            if grain in __salt__['grains.ls']():
                grain_value = __salt__['grains.get'](grain)
            else:
                grain_value = {}
            grain_value[key] = ticket
            __salt__['grains.setval'](grain, grain_value)
            ret['changes']['ticket'] = "Executed. Output into grain: {0}:{1}".format(grain, key)
    elif output:
        ret['changes']['ticket'] = "Executed. Output into {0}".format(output)
        with salt.utils.files.fopen(output, 'w') as output_file:
            output_file.write(salt.utils.stringutils.to_str(ticket))
    else:
        ret['changes']['ticket'] = "Executed"

    return ret