def insert_metadata(module_data, new_metadata, insertion_line, targets=('ANSIBLE_METADATA',)):
 assignments = ' = '.join(targets)
 pretty_metadata = pformat(new_metadata, width=1).split('\n')
 new_lines = []
 new_lines.append('{} = {}'.format(assignments, pretty_metadata[0]))
 if (len(pretty_metadata) > 1):
  for line in pretty_metadata[1:]:
   new_lines.append('{}{}'.format((' ' * ((len(assignments) - 1) + len(' = {'))), line))
 old_lines = module_data.split('\n')
 lines = (((old_lines[:insertion_line] + new_lines) + ['']) + old_lines[insertion_line:])
 return '\n'.join(lines)

def insert_metadata ( module_data , new_metadata , insertion_line , targets = ( 'ANSIBLE_METADATA' , ) ) : assignments = ' = ' . join ( targets ) pretty_metadata = pformat ( new_metadata , width = 1 ) . split ( '\n' ) new_lines = [ ] new_lines . append ( '{} = {}' . format ( assignments , pretty_metadata [ 0 ] ) ) if ( len ( pretty_metadata ) > 1 ) : for line in pretty_metadata [ 1 : ] : new_lines . append ( '{}{}' . format ( ( ' ' * ( ( len ( assignments ) - 1 ) + len ( ' = {' ) ) ) , line ) ) old_lines = module_data . split ( '\n' ) lines = ( ( ( old_lines [ : insertion_line ] + new_lines ) + [ '' ] ) + old_lines [ insertion_line : ] ) return '\n' . join ( lines )
insert a new set of metadata at a specified line .