# Problem trying to use a specific string with templates

**URL:** <https://forum.ansible.com/t/problem-trying-to-use-a-specific-string-with-templates/20092>\
**Category:** Ansible Project\
**Created:** [December 29, 2015, 1:41pm UTC](https://forum.ansible.com/t/problem-trying-to-use-a-specific-string-with-templates/20092 "2015-12-29T13:41:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guido](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Guido](https://forum.ansible.com/u/Guido)\
**Post date:** [December 29, 2015, 1:41pm UTC](https://forum.ansible.com/t/problem-trying-to-use-a-specific-string-with-templates/20092/1 "2015-12-29T13:41:12Z")

</div>

Hi everyone,

I’m trying to put a string by replacing a macro in a template but the problem is that the string itself it’s being interpreted as a macro and I need to avoida that. Here are config vars and the template:

vars.yml:  
log\_format: “[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s”

expire: 1000

template (setting.py):  
formatter = logging.Formatter(“{{ api\_log\_format }}”)

expire = {{expire}}

When I execute the playbook I’m getting the following error:

{‘msg’: ‘AnsibleError: ERROR! Failed to template [%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s: ERROR! template error while templating string: tag name expected’, ‘failed’: True}

If I change {%(pathname)s:%(lineno)d} with for example [%(pathname)s:%(lineno)d] the problem it’s solved and the playbook continues but I can’t do that because it’s a developers request that the { and } exists in the log format.

Is there any way that I could tell ansible to ignore that specific line?

Any other idea?

I’m using ansible v1.9.2

Thanks!

---

<div class="post-metadata">

**Author:** ![Guido](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Guido](https://forum.ansible.com/u/Guido)\
**Post date:** [December 29, 2015, 1:42pm UTC](https://forum.ansible.com/t/problem-trying-to-use-a-specific-string-with-templates/20092/2 "2015-12-29T13:42:53Z")

</div>

> Hi everyone,
> 
> &nbsp;&nbsp;&nbsp;I'm trying to put a string by replacing a macro in a template but the  
> problem is that the string itself it's being interpreted as a macro and I  
> need to avoida that. Here are config vars and the template:
> 
> vars.yml:  
> log\_format: "[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s -  
> %(message)s"  
> expire: 1000
> 
> template (setting.py):  
> formatter = logging.Formatter("{{ api\_log\_format }}")  
> expire = {{expire}}
> 
> When I execute the playbook I'm getting the following error:
> 
> {'msg': 'AnsibleError: ERROR! Failed to template [%(asctime)s]  
> {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s: ERROR! template  
> error while templating string: tag name expected', 'failed': True}
> 
> If I change {%(pathname)s:%(lineno)d} with for example  
> [%(pathname)s:%(lineno)d] the problem it's solved and the playbook  
> continues but I can't do that because it's a developers request that the {  
> and } exists in the log format.
> 
> Is there any way that I could tell ansible to ignore that specific line?
> 
> Any other idea?
> 
> I'm using ansible v1.9.2
> 
> Thanks!

There is a typo in my first email,

This line log\_format: "[%(asctime)s] {%(pathname)s:%(lineno)d}  
%(levelname)s - %(message)s"

should be

api\_log\_format: "[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s -  
%(message)s"

Sorry

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 29, 2015, 2:54pm UTC](https://forum.ansible.com/t/problem-trying-to-use-a-specific-string-with-templates/20092/3 "2015-12-29T14:54:49Z")

</div>

What you need to do is tell jinja2 that the value should not be parsed. To do this you can use `{% raw %} ... {% endraw %}` such as:

api\_log\_format: “{% raw %}[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s{% endraw %}”

---

<div class="post-metadata">

**Author:** ![Guido](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Guido](https://forum.ansible.com/u/Guido)\
**Post date:** [December 30, 2015, 12:05pm UTC](https://forum.ansible.com/t/problem-trying-to-use-a-specific-string-with-templates/20092/4 "2015-12-30T12:05:46Z")

</div>

Thank you very much, I worked, that was exactly what I needed.
