title: How to handle special characters to get result as "%{%s}t" using Ansible/yml?
body: <p>Currently working on Ansible to port config of older system to multiple <code>IBM</code> <code>ISAM</code> 9 systems using <code>ISAM-Ansible-Roles</code> (<a href="https://github.com/IBM-Security/isam-ansible-roles" rel="nofollow noreferrer">https://github.com/IBM-Security/isam-ansible-roles</a>) and having issue with a specific string.</p>
  
  <p>The result I need is like this:</p>
  
  <pre><code>request-log-format = { "seconds": "%{%s}t" }
  </code></pre>
  
  <p>The <code>.yml</code> file I got is as below:</p>
  
  <pre><code>---
  - name: Run some commands
    hosts: all
    connection: local
    vars:
      username: "admin"
      password: "password1234"
      lmi_port: "443"
      log_level: "CRITICAL"
      force: True
      start_config_wait_time: 120
    roles:
      - role: update_reverseproxy_conf
        update_reverseproxy_conf_reverseproxy_id: "TestInstance"
        update_reverseproxy_conf_entries:
        - stanza_id: "logging"
          entry_id: "request-log-format"
          value_id: " { \"seconds\": \"%{%s}t\" } "
  </code></pre>
  
  <p>The last line <code>value_id:</code> is where I am having problem. Tried various methods with single/double quotes, and different escape characters <code>("\" and "%")</code> but all failed. Some of them listed below.</p>
  
  <pre><code>1) " { \"seconds\": \"%{%s}t\" } "  error: Encountered unknown tag 's'
  2) ' { \"seconds\": \"%{%s}t\" } '  error: Encountered unknown tag 's'
  3) ' { \"seconds\": \"%{\%s}t\" } ' no error but with incorrect result extra back slash before %s)  request-log-format = { "seconds": "%{\%s}t" }
  4) ' { \"seconds\": \"\%{%s}t\" } ' error: Encountered unknown tag 's'
  5) " { \"seconds\": \"%{%%s}t\" } "  error: tag name expected
  6) " { \"seconds\": \"%%{%s}t\" } "  error: tag name expected
  </code></pre>
  
  <p>Did some searches on the net but can't find any solution. Please help, any suggestions or ideas welcomed. Thanks!</p>
  
  <p><strong>[Update 2018.01.05]</strong> </p>
  
  <p>I use docker to run ansible. The file was saved as "testOneLine.yml", the command to run is: </p>
  
  <pre><code>sudo docker run -it --rm -v /home/user1/Documents/myplatform:/ansible/playbooks --name test mludocker/isam-ansible -i hosts testOneLine.yml
  </code></pre>
  
  <p>Also tried a few more below as suggested, but all failed:</p>
  
  <pre><code>7) '{% raw %}{ "seconds": "%{%s}t" }{% endraw %}'  error: Encountered unknown tag 's'
  8) !unsafe ' { "seconds": "%{%s}t" } '  error: SyntaxError: invalid syntax MODULE FAILURE
  </code></pre>
  
  <p>Tried <a href="https://stackoverflow.com/users/783524/rob-h">Rob H</a>'s suggestion and rewrite as below, with extra regex_replace at the end, but it also failed.</p>
  
  <pre><code>---
  - name: Run some commands
    hosts: all
    connection: local
    vars:
      username: "admin"
      password: "password1234"
      lmi_port: "443"
      log_level: "CRITICAL"
      force: True
      start_config_wait_time: 120
      starter_value: "{ \"seconds\": \"${$s}t\" }"        
    roles:
      - role: update_reverseproxy_conf
        update_reverseproxy_conf_reverseproxy_id: "TestInstance"
        update_reverseproxy_conf_entries:
        - stanza_id: "logging"
          entry_id: "request-log-format"
          value_id: "{{ starter_value | regex_replace('\\$','%') }}"
  </code></pre>
  
  <p>Can see the "regex_replace" part is working as the final "value_id" string has the "$" correctly changed to "%". The error message shown below is the same as before (Encountered unknown tag 's'). </p>
  
  <pre><code>FAILED! =&gt; {
  "failed": true, 
  "msg": "{u'entry_id': u'request-log-format', u'stanza_id': u'logging', u'value_id': u' { \"seconds\": \"%{%s}t\" } '}: template error while templating string: Encountered unknown tag 's'.. String:  { \"seconds\": \"%{%s}t\" } "
  }
  </code></pre>
  
  <p>I also raised a new <a href="https://github.com/IBM-Security/isam-ansible-roles/issues/53" rel="nofollow noreferrer">issue #53 with IBM security</a></p>