template error: Encountered unknown tag 'Y'

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

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
        Racke

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

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

(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

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 !!

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:

  {% raw %}
  LogFormat "%v:%p %{%F %T}t.%{msec_frac}t %h %u \"%r\" %>s %O \"%{Referer}i\"\
    \"%{User-Agent}i\"" vhost_federated_combined
  {% 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