Ansible: Unexpected templating type error: expected string or buffer

為{幸葍}努か 提交于 2020-08-07 05:25:40

问题


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)]

回答1:


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"


来源:https://stackoverflow.com/questions/53529978/ansible-unexpected-templating-type-error-expected-string-or-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!