template error while templating string: unexpected '.'

6,377 views
Skip to first unread message

cmou...@redhat.com

unread,
Oct 27, 2017, 11:56:24 AM10/27/17
to Ansible Project
Hi

How can I avoid that the template engine throws this error ?

Task

- name: Get Service Account Token
  shell
: |
   
/usr/local/bin/oc get sa/jenkins -n {{ namespace }} --template='{{range .secrets}}{{ .name }} {{end}}' | xargs -n 1 oc get secret --template='{{ if .data.token }}{{ .data.token }}{{end}}' | head -n 1 | base64 -D -
 
register: sa_jenkins_token

Error

  fatal: [10.8.244.145]: FAILED! => {"failed": true, "msg": "template error while templating string: 
 
  unexpected '.'. String: /usr/local/bin/oc get sa/jenkins -n {{ namespace }} --template='{{range .secrets}}{{ .name }} {{end}}' | xargs -n 1 oc get secret --template='{{ if .data.token }}{{ .data.token }}{{end}}' | head -n 1 | base64 -D -"
}

Regards

Charles

Kai Stian Olstad

unread,
Oct 27, 2017, 12:09:50 PM10/27/17
to ansible...@googlegroups.com
On 27. okt. 2017 17:56, cmou...@redhat.com wrote:
> Hi
>
> How can I avoid that the template engine throws this error ?

Don't use . in variable name.

https://docs.ansible.com/ansible/latest/playbooks_variables.html#what-makes-a-valid-variable-name


> Task
>
> - name: Get Service Account Token
> shell: |
> /usr/local/bin/oc get sa/jenkins -n {{ namespace }} --template='{{range .secrets}}{{ .name }} {{end}}' | xargs -n 1 oc get secret --template='{{ if .data.token }}{{ .data.token }}{{end}}' | head -n 1 | base64 -D -
> register: sa_jenkins_token

This looks very strange and I don't understand what you are trying to do.
{{ }} is only used for variables not Jinja logic.


--
Kai Stian Olstad

cmou...@redhat.com

unread,
Oct 27, 2017, 12:26:28 PM10/27/17
to Ansible Project
The shell command that I want to execute is 

oc get sa/jenkins -n infra --template='{{range .secrets}}{{ .name }} {{end}}' | xargs -n 1 oc get secret --template='{{ if .data.token }}{{ .data.token }}{{end}}' | head -n 1 | base64 -D -

and I will get as response a token

So the {{ }} characters that you see within my command are used to extract info using openshift oc template

I will change my syntax to use jsonpath

Matt Martz

unread,
Oct 27, 2017, 12:30:33 PM10/27/17
to ansible...@googlegroups.com
The problem is that you seem to be using templating that the `oc` command will use that overlaps with template syntax of jinja.

You will likely need to wrap your command in {% raw %}command with {{ here }}{% endraw %}


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/de427462-21a7-4a8c-8000-3479de745ecb%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Денис Милованов

unread,
Dec 17, 2022, 7:36:29 AM12/17/22
to Ansible Project
Has the same error, by trying send this way:

An unhandled exception occurred while templating 'Alertname: {{ .GroupLabels.alertname }}\n Severity: {{ .CommonLabels.severity }}\n {{ range .Alerts }}{{ .Annotations.description }}\n{{ end }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: unexpected '.'. String: Alertname: {{ .GroupLabels.alertname }}\n Severity: {{ .CommonLabels.severity }}\n {{ range .Alerts }}{{ .Annotations.description }}\n{{ end }}"
}

?
Guys, make some advice, please.

пятница, 27 октября 2017 г. в 19:30:33 UTC+3, Matt Martz:
The problem is that you seem to be using templating that the `oc` command will use that overlaps with template syntax of jinja.

You will likely need to wrap your command in {% raw %}command with {{ here }}{% endraw %}


On Fri, Oct 27, 2017 at 11:26 AM, <cmou...@redhat.com> wrote:
The shell command that I want to execute is 

oc get sa/jenkins -n infra --template='{{range .secrets}}{{ .name }} {{end}}' | xargs -n 1 oc get secret --template='{{ if .data.token }}{{ .data.token }}{{end}}' | head -n 1 | base64 -D -

and I will get as response a token

So the {{ }} characters that you see within my command are used to extract info using openshift oc template

I will change my syntax to use jsonpath



On Friday, October 27, 2017 at 5:56:24 PM UTC+2, cmou...@redhat.com wrote:
Hi

How can I avoid that the template engine throws this error ?

Task

- name: Get Service Account Token
  shell
: |
   
/usr/local/bin/oc get sa/jenkins -n {{ namespace }} --template='{{range .secrets}}{{ .name }} {{end}}' | xargs -n 1 oc get secret --template='{{ if .data.token }}{{ .data.token }}{{end}}' | head -n 1 | base64 -D -
 
register: sa_jenkins_token

Error

  fatal: [10.8.244.145]: FAILED! => {"failed": true, "msg": "template error while templating string: 
 
  unexpected '.'. String: /usr/local/bin/oc get sa/jenkins -n {{ namespace }} --template='{{range .secrets}}{{ .name }} {{end}}' | xargs -n 1 oc get secret --template='{{ if .data.token }}{{ .data.token }}{{end}}' | head -n 1 | base64 -D -"
}

Regards

Charles

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages