# template error: Encountered unknown tag 'Y'

**URL:** <https://forum.ansible.com/t/template-error-encountered-unknown-tag-y/34119>\
**Category:** Ansible Project\
**Created:** [October 27, 2020, 6:21pm UTC](https://forum.ansible.com/t/template-error-encountered-unknown-tag-y/34119 "2020-10-27T18:21:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy\_Lenferink](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_Lenferink](https://forum.ansible.com/u/Roy_Lenferink)\
**Post date:** [October 27, 2020, 6:21pm UTC](https://forum.ansible.com/t/template-error-encountered-unknown-tag-y/34119/1 "2020-10-27T18:21:09Z")

</div>

Hello everyone,

I am having a problem with defining a string variable in Ansible (v2.9.9)

The string I want to define (for the geerlingguy.apache role):  
apache\_global\_vhost\_settings: |  
LogFormat “\<%%JSON:httpd\_access%%\> { "time": "%{%Y}t" }” combined

CustomLog logs/access\_log combined

This doesn’t work and I am getting the following error message:  
_template error while templating string: Encountered unknown tag ‘Y’… String: LogFormat_

It basically comes down to the following part: "%{%Y}t" }

How can I achieve the specified string in Ansible ? I tried escaping several properties however wasn’t able to find out a solution.

Best regards,  
Roy

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 27, 2020, 6:35pm UTC](https://forum.ansible.com/t/template-error-encountered-unknown-tag-y/34119/2 "2020-10-27T18:35:53Z")

</div>

> Hello everyone,
> 
> I am having a problem with defining a string variable in Ansible (v2.9.9)
> 
> The string I want to define (for the geerlingguy.apache role):  
> apache\_global\_vhost\_settings: |  
> LogFormat "\<%%JSON:httpd\_access%%\> { \"time\": \"%\{%Y}t\" }" combined  
> CustomLog logs/access\_log combined
> 
> This doesn't work and I am getting the following error message:  
> /template error while templating string: Encountered unknown tag 'Y'.. String: LogFormat// \<snip\>/
> 
> It basically comes down to the following part: \"%\{%Y}t\" }
> 
> How can I achieve the specified string in Ansible ? I tried escaping several properties however wasn't able to find out  
> a solution.

Well, how should the expected LogFormat line look like?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 27, 2020, 7:00pm UTC](https://forum.ansible.com/t/template-error-encountered-unknown-tag-y/34119/3 "2020-10-27T19:00:44Z")

</div>

By the var name I guess you're using the jeff geerling's apache role?  
I use that too and also ran into this issue.  
You need to escape the jinja looking part.  
My snippet:

apache\_global\_vhost\_settings: |  
&nbsp;&nbsp;# Sortable log format, with federated username.  
&nbsp;&nbsp;LogFormat "%v:%p %{{'{%'}}F %T}t.%{msec\_frac}t %h %u \"%r\" %\>s %O  
\"%{Referer}i\"\  
&nbsp;&nbsp;&nbsp;&nbsp;\"%{User-Agent}i\"" vhost\_federated\_combined

---

<div class="post-metadata">

**Author:** ![Roy\_Lenferink](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_Lenferink](https://forum.ansible.com/u/Roy_Lenferink)\
**Post date:** [October 27, 2020, 8:16pm UTC](https://forum.ansible.com/t/template-error-encountered-unknown-tag-y/34119/4 "2020-10-27T20:16:05Z")

</div>

> > Hello everyone,
> > 
> > I am having a problem with defining a string variable in Ansible (v2.9.9)
> > 
> > The string I want to define (for the geerlingguy.apache role):  
> > apache\_global\_vhost\_settings: |  
> > LogFormat “\<%%JSON:httpd\_access%%\> { "time": "%{%Y}t" }” combined  
> > CustomLog logs/access\_log combined
> > 
> > This doesn’t work and I am getting the following error message:  
> > /template error while templating string: Encountered unknown tag ‘Y’… String: LogFormat// /
> > 
> > It basically comes down to the following part: "%{%Y}t" }
> > 
> > How can I achieve the specified string in Ansible ? I tried escaping several properties however wasn’t able to find out  
> > a solution.
> 
> Well, how should the expected LogFormat line look like?

What I want to achieve is the following:  
[https://pastebin.com/0DqXksdq](https://pastebin.com/0DqXksdq)

(I put it on pastebin since the full string can be messed up in e-mail)

But to get there I first tried to add the following line to be part of the Ansible variable (which throws the error already):  
_LogFormat “\<%%JSON:httpd\_access%%\> { "time": "%{%Y-%m-%dT%H:%M:%S%z}t" }” combined_

Roy

---

<div class="post-metadata">

**Author:** ![Roy\_Lenferink](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_Lenferink](https://forum.ansible.com/u/Roy_Lenferink)\
**Post date:** [October 27, 2020, 8:47pm UTC](https://forum.ansible.com/t/template-error-encountered-unknown-tag-y/34119/5 "2020-10-27T20:47:25Z")

</div>

> By the var name I guess you’re using the jeff geerling’s apache role?  
> I use that too and also ran into this issue.  
> You need to escape the jinja looking part.  
> My snippet:
> 
> apache\_global\_vhost\_settings: |
> 
> # Sortable log format, with federated username.
> 
> LogFormat “%v:%p %{{‘{%’}}F %T}t.%{msec\_frac}t %h %u "%r" %\>s %O  
> "%{Referer}i"  
> "%{User-Agent}i"” vhost\_federated\_combined

Well great! This was the exact problem. After substituting %{%Y with %{{‘{%’}}Y it worked !!

To still answer your first question; yes I am indeed using Jeff Geerling’s Apache role.

Many thanks for the help !!

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 28, 2020, 10:01am UTC](https://forum.ansible.com/t/template-error-encountered-unknown-tag-y/34119/6 "2020-10-28T10:01:06Z")

</div>

It may be even easier if you wrap the entire thing in 'raw'. This  
should be fine as you don't use jinja inside that LogFormat statement.  
Hence the value will be just in the YAML as-is, i.e. easier to debug:

&nbsp;&nbsp;{% raw %}  
&nbsp;&nbsp;LogFormat "%v:%p %{%F %T}t.%{msec\_frac}t %h %u \"%r\" %\>s %O \"%{Referer}i\"\  
&nbsp;&nbsp;&nbsp;&nbsp;\"%{User-Agent}i\"" vhost\_federated\_combined  
&nbsp;&nbsp;{% endraw %}

Both methods yield the same result, so use which one is easier in your case.  
IIRC I didn't use 'raw' because it messed up the yaml syntax  
highlighting in my editor.

Dick
