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

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!

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

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!