# template error while templating string: unexpected '.'

**URL:** <https://forum.ansible.com/t/template-error-while-templating-string-unexpected/25844>\
**Category:** Ansible Project\
**Created:** [October 27, 2017, 3:56pm UTC](https://forum.ansible.com/t/template-error-while-templating-string-unexpected/25844 "2017-10-27T15:56:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmoullia](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@cmoullia](https://forum.ansible.com/u/cmoullia)\
**Post date:** [October 27, 2017, 3:56pm UTC](https://forum.ansible.com/t/template-error-while-templating-string-unexpected/25844/1 "2017-10-27T15:56:24Z")

</div>

Hi

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

Task

`

```auto
- 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

`

```auto
  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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 27, 2017, 4:09pm UTC](https://forum.ansible.com/t/template-error-while-templating-string-unexpected/25844/2 "2017-10-27T16:09:31Z")

</div>

> 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](https://docs.ansible.com/ansible/latest/playbooks_variables.html#what-makes-a-valid-variable-name)

> Task
> 
> - name: Get Service Account Token  
> &nbsp;&nbsp;&nbsp;shell: |  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/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 -  
> &nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![cmoullia](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@cmoullia](https://forum.ansible.com/u/cmoullia)\
**Post date:** [October 27, 2017, 4:26pm UTC](https://forum.ansible.com/t/template-error-while-templating-string-unexpected/25844/3 "2017-10-27T16:26:28Z")

</div>

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

---

<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:** [October 27, 2017, 4:29pm UTC](https://forum.ansible.com/t/template-error-while-templating-string-unexpected/25844/4 "2017-10-27T16:29:38Z")

</div>

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 %}

See [http://jinja.pocoo.org/docs/dev/templates/#escaping](http://jinja.pocoo.org/docs/dev/templates/#escaping)

---

<div class="post-metadata">

**Author:** ![user35](https://avatars.discourse-cdn.com/v4/letter/u/d07c76/32.png) [@user35](https://forum.ansible.com/u/user35)\
**Post date:** [December 17, 2022, 12:36pm UTC](https://forum.ansible.com/t/template-error-while-templating-string-unexpected/25844/5 "2022-12-17T12:36:29Z")

</div>

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:
