Ansible:意外的模板类型错误:预期的字符串或缓冲区 [英] Ansible: Unexpected templating type error: expected string or buffer

查看:37
本文介绍了Ansible:意外的模板类型错误:预期的字符串或缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个寄存器,内容如下:

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"
         ]
    }
}

当我在寄存器的标准输出中看到子字符串not"时,我想触发另一个任务:

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"

我只在添加 when 条件时看到此错误.没有它,我的剧本就成功了.我在这里做错了什么?

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

版本详情:

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

推荐答案

当变量(在您的情况下为 operation_status.stdout)未定义时,会发生此错误.也许 status.sh 脚本在服务运行时没有写入标准输出.

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.

能不能放一个调试任务,在任务前用when"打印这个变量的值?

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

您也可以尝试将 when 条件修改为:

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"

这篇关于Ansible:意外的模板类型错误:预期的字符串或缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆