string - Ansible:意外的模板类型错误:预期的字符串或缓冲区

标签 string ansible conditional buffer templating

我有一个包含以下内容的寄存器:

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”时,我想触发另一个任务:
  - name: Starting Application As Requested
    shell: /tmp/start.sh
    when: operation_status.stdout | search('not')

但是,我在触发的任务中看到此错误
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条件时,我只会看到此错误。没有它,我的剧本就成功了。我在这里做错了什么?

版本详细信息:
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脚本可能不会写入stdout。

您是否可以将调试任务放到任务前面,并用“when”打印此变量的值?

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

您也可以尝试将when条件修改为:
  - name: Starting Application As Requested
    shell: /tmp/start.sh
    when: "'not' in operation_status.stdout"

关于string - Ansible:意外的模板类型错误:预期的字符串或缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53529978/

相关文章:

java - 不返回预期的连接字符串

postgresql - Ansible postgresql_db 任务在长时间暂停后失败

php - Laravel 条件语句 : is when() efficient?

Java 在字符串中看不到空格

c - 如何访问嵌套结构中的指针

javascript - 在 JavaScript 中将字符串作为数学表达式求值

amazon-web-services - 获取使用 Ansible 创建的 AMI 的 AMI ID

roles - 根据环境定制 ansible 角色

sql-server - 条件选择查询

regex - 如何忽略部分文本并在剩余部分进行搜索和替换?