ansible - 如何解决模板运行时错误: no test named 'equalto' in Ansible?

标签 ansible ansible-2.x

Ansible v2.2.1.0

我有一个收集项目信息的任务,并且我为该任务设置了一个寄存器。比如我用jq来解析一个JSON文件,

hello.json
----------
{
    "name" : "hello file",
    "english" : "hello",
    "spanish" : "hola",
    "german" : "wie gehts"
}

- name: parse the hello.json file
  shell: |
      jq -r '.{{ item }}' < hello.json
  register: hellos
  with_items:
  - english
  - spanish
  - german

- debug: var=hellos

调试显示

ok: [localhost] => {
    "hellos": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                # snipped
                "item": "english", 
                "stdout" : "hello",
                # snipped
           },
            {
                # snipped
                "item": "spanish", 
                "stdout" : "hola",
                # snipped
           },
           {
                # snipped
                "item": "german", 
                "stdout" : "wie gehts",
                # snipped
           }
        ]
    }
}

现在,如果我想获取 hellos 寄存器的标准输出值,我尝试了这个

- name: Display hello messages
  debug: msg="{{ hellos.results | selectattr("item","equalto",item) | map(attribute="stdout") | first }} worlld"
  with_items:
  - english
  - spanish
  - german

我明白了

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TemplateRuntimeError: no test named 'equalto'
fatal: [localhost]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}

我基本上是在第二个调试任务中解析“item”的 hellos 寄存器并获取其“stdout”属性。我的错误在哪里?

最佳答案

你在这里做了一些非常奇怪的事情。我确信您原来的任务可以更容易地解决。

但是要回答您的问题“为什么 equalto 找不到过滤器?”:更新 Jinja2。

检查pip list | grep Jinja2 .
equalto是在ver.1中介绍的。 2.8.

关于ansible - 如何解决模板运行时错误: no test named 'equalto' in Ansible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44660161/

相关文章:

ubuntu - 无法使用 ansible 2.2 模块复制文件

linux - 我的 ansible 剧本 "when"条件错误

ansible - 在哪里可以获得 Ansible 预定义变量的列表?

Ansible 使用 list 项分配主机名

python-2.7 - 如何获取 ansible.module_utils 来解析我的自定义目录

Ansible - playbook 调用另一个带有变量、标签和限制的 playbook

ansible - 每个主机的 "pause"

Ansible:仅对某些主机执行角色

ansible - 在ansible中使用chown命令?

Ansible 获取 list 主机组的主机名和 IP