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


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

最佳答案

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

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

pip list | grep Jinja2检查。
在中引入了equalto。 2.8。

10-01 13:20