def generate_ticket(name, output=None, grain=None, key=None, overwrite=True):
 ret = {'name': name, 'changes': {}, 'result': True, 'comment': ''}
 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
 ticket = __salt__['icinga2.generate_ticket'](name).strip()
 if ticket:
  ret['comment'] = str(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.fopen(output, 'w') as output_file:
   output_file.write(str(ticket))
 else:
  ret['changes']['ticket'] = 'Executed'
 return ret

def generate_ticket ( name , output = None , grain = None , key = None , overwrite = True ) : ret = { 'name' : name , 'changes' : { } , 'result' : True , 'comment' : '' } 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 ticket = __salt__ [ 'icinga2.generate_ticket' ] ( name ) . strip ( ) if ticket : ret [ 'comment' ] = str ( 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 . fopen ( output , 'w' ) as output_file : output_file . write ( str ( ticket ) ) else : ret [ 'changes' ] [ 'ticket' ] = 'Executed' return ret
generate and save an icinga2 ticket .