def _escape_backslashes(data, jinja_env):
 if (('\\' in data) and ('{{' in data)):
  new_data = []
  d2 = jinja_env.preprocess(data)
  in_var = False
  for token in jinja_env.lex(d2):
   if (token[1] == 'variable_begin'):
    in_var = True
    new_data.append(token[2])
   elif (token[1] == 'variable_end'):
    in_var = False
    new_data.append(token[2])
   elif (in_var and (token[1] == 'string')):
    new_data.append(token[2].replace('\\', '\\\\'))
   else:
    new_data.append(token[2])
  data = ''.join(new_data)
 return data

def _escape_backslashes ( data , jinja_env ) : if ( ( '\\' in data ) and ( '{{' in data ) ) : new_data = [ ] d2 = jinja_env . preprocess ( data ) in_var = False for token in jinja_env . lex ( d2 ) : if ( token [ 1 ] == 'variable_begin' ) : in_var = True new_data . append ( token [ 2 ] ) elif ( token [ 1 ] == 'variable_end' ) : in_var = False new_data . append ( token [ 2 ] ) elif ( in_var and ( token [ 1 ] == 'string' ) ) : new_data . append ( token [ 2 ] . replace ( '\\' , '\\\\' ) ) else : new_data . append ( token [ 2 ] ) data = '' . join ( new_data ) return data
double backslashes within jinja2 expressions a user may enter something like this in a playbook:: debug: msg: "test case 13; {{ test1_name | regex_replace(^_name$ .