# A "first time" unexpected templating error - what did I do?

**URL:** <https://forum.ansible.com/t/a-first-time-unexpected-templating-error-what-did-i-do/23058>\
**Category:** Ansible Project\
**Created:** [November 20, 2016, 7:06pm UTC](https://forum.ansible.com/t/a-first-time-unexpected-templating-error-what-did-i-do/23058 "2016-11-20T19:06:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [November 20, 2016, 7:06pm UTC](https://forum.ansible.com/t/a-first-time-unexpected-templating-error-what-did-i-do/23058/1 "2016-11-20T19:06:46Z")

</div>

Hello - Assistance requested.

I believe I’ve coded a nasty bug into my ansible playbook and I’m not sure how to unravel it. In my observations, the code throws an “Unexpected templating type error occurred” whenever I first run a task. All subsequent runs do not throw the error again, even if I backout the changes to redo them. Some code snippets below.

role1/tasks/main.yml:

`

- name: database param\_def select  
tags: db  
include: roles/role\_utility\_sqlplus/tasks/main.yml  
vars:  
query\_type: “select”  
table: “param\_def”  
filename: “dbselect\_paramdef\_mhe.sql”  
db\_logname: “{{ WMS\_Schema }}_{{ WMS\_Servicename }}db\_paramdef\_mhe{{ lookup(‘pipe’, 'date +%Y-%m-%d_%H%M%S’) }}.log”  
db\_absolute\_logpath: “/dev/null”

- name: set\_fact database select output  
tags: db  
set\_fact:  
db\_select\_output: “{{ expect\_output }}”

- name: database param\_def insert  
tags: db  
include: roles/role\_utility\_sqlplus/tasks/main.yml  
vars:  
query\_type: “insert”  
table: “param\_def”  
filename: “dbinsert\_paramdef\_mhe.sql”  
db\_logname: “{{ WMS\_Schema }}_{{ WMS\_Servicename }}db\_paramdef\_mhe{{ lookup(‘pipe’, 'date +%Y-%m-%d_%H%M%S’) }}.log”  
db\_absolute\_logpath: “{{ log\_dir }}/{{ db\_logname }}”  
when: db\_select\_output.stdout | search(“no rows selected”)  
`

roles/role\_utility\_sqlplus/tasks/main.yml:

`

- name: Database - Stage {{ table }} {{ query\_type }} sql from template  
tags: db  
template:  
src={{ filename }}.j2  
dest=“{{ staged\_config\_dir }}/{{ inventory\_hostname\_short }}/{{ filename }}”  
backup=yes  
changed\_when: false

- name: Database - Execute {{ table }} {{ query\_type }}  
tags: db  
expect:  
command: “/bin/bash”  
responses:  
$ $:

- . ~/.profile

- “sqlplus {{ WMS\_Schema }}/$PASSWORD@{{ WMS\_Servicename }}”

- exit

> $:

- set echo on
- spool {{ db\_absolute\_logpath }}
- “@{{ staged\_config\_dir }}/{{ inventory\_hostname\_short }}/{{ filename }}”
- spool off
- quit  
timeout: 5  
echo: yes  
register: expect\_output  
changed\_when: query\_type != “select”  
failed\_when: expect\_output.stdout | search(“(ORA|SP2)-[0-9]+”)  
`

Occasional ‘first time’ error:

> TASK [role\_config\_mhe : Database - Execute param\_def include] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [ptl01a0fap006]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘expect\_output.stdout | search("(ORA|SP2)-[0-9]+")’ failed. The error was: Unexpected templating type error occurred on ({% if expect\_output.stdout | search("(ORA|SP2)-[0-9]+") %} True {% else %} False {% endif %}): expected string or buffer”}

Essentially, I have a role that includes a utility role for sqlplus work. It somestimes trips on the ‘failed\_when’ statement in the included sqlplus role. I believe this has something to do with use an include (with conditional) and then that include also has conditionals, such that they are mashing together and sometimes breaking the ‘template’ behind the scenes. It could also be the scope of registered variable ‘expect\_output’, which I believe I resolved by declaring ‘db\_select\_output’ in role1.

Any thoughts? It is working right now because I let the ‘first time’ failures pass. Now I can’t reproduce. I’m worried I have a nasty bug that will bite me later. Any advice is helpful.

Thanks!

---

<div class="post-metadata">

**Author:** ![Greg\_Langford](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Greg\_Langford](https://forum.ansible.com/u/Greg_Langford)\
**Post date:** [November 21, 2016, 12:28pm UTC](https://forum.ansible.com/t/a-first-time-unexpected-templating-error-what-did-i-do/23058/2 "2016-11-21T12:28:22Z")

</div>

What happens when you run the command manually the first time round? What is the exit code and what is the output, as it says a templating type error occurred I would check these things first. It may be the command when first run does not output what you expect it to.

Kind Regards

---

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [November 28, 2016, 11:24pm UTC](https://forum.ansible.com/t/a-first-time-unexpected-templating-error-what-did-i-do/23058/3 "2016-11-28T23:24:49Z")

</div>

Thank you for the response!

For separate reasons, I ended up changing this to a local\_action and then the issue never resurfaced. Still unsure why…and I’ve been monitoring it for awhile now. If the error reproduces I’ll post the output and other info.

Thanks for the help!
