如何解决TemplateRuntimeError:Ansible中没有名为'equalto'的测试?

时间:2017-06-20 18:07:22

标签: 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寄存器的stdout值,我试过这个

- 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”属性。哪里是我的错误?

1 个答案:

答案 0 :(得分:4)

你在这里做了一些非常奇怪的事情。我相信你的原始任务可以更轻松地解决。

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

查看pip list | grep Jinja2 equalto在版本中引入。 2.8。