Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Unexpected templating type error: expected string or buffer

I have a register with the following contents:

ok: [hostname] => {
    "changed": false,
    "msg": {
        "changed": true,
        "cmd": "cd /tmp\n ./status.sh dev",
        "delta": "0:00:00.023660",
        "end": "2018-11-28 17:46:05.838934",
        "rc": 0,
        "start": "2018-11-28 17:46:05.815274",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "application is not running. no pid file found",
        "stdout_lines": [
            "application is not running. no pid file found"
         ]
    }
}

When i see the substring "not" in the register's stdout, i want to trigger another task:

  - name: Starting Application As Requested
    shell: /tmp/start.sh
    when: operation_status.stdout | search('not')

However, i see this error in my triggered task

fatal: [host]: FAILED! => {
"failed": true,
"msg": "The conditional check 'operation_status.stdout | search('not')' failed. The error was: Unexpected templating type error occurred on ({% if operation_status.stdout | search('not') %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/path/to/ansible_playbook.yml': line 46, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Starting Application As Requested\n    ^ here\n"

I only see this error when adding the when condition. Without it, my playbook succeeds. What am i doing wrong here?

Version details:

ansible 2.3.0.0
python version = 2.6.6 (r266:84292, Aug  9 2016, 06:11:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
like image 329
Jimit Raithatha Avatar asked Nov 29 '18 00:11

Jimit Raithatha


1 Answers

This error occurs when the variable (in your case operation_status.stdout) is undefined. Maybe the status.sh script doesn't write to stdout when the service is running.

Can you put a debug task and print the value of this variable before the task with "when"?

- name: Debug print value of operation_status.stdout
  debug:var=operation_status.stdout

You can also try and modify the when condition to be:

  - name: Starting Application As Requested
    shell: /tmp/start.sh
    when: "'not' in operation_status.stdout"
like image 83
Anant Naugai Avatar answered Nov 14 '22 08:11

Anant Naugai