ansible 迭代来自多个主机的结果以提取特定的键值

ansible iterate on results from multiple hosts to pull specific key value

团队,

我正在遍历特定清单组中的所有主机,以检查文件上 stat 输出的键值响应,但无法映射它。 谁能提示如何映射它?

      - debug:
          var: result
      - debug:
          msg: "FOUND: /etc/cachefilesd.conf exists..."
        when: result.results.stat.exists
        delegate_to: "{{ item }}"
        with_items: "{{ groups['gpu_node'] }}"

输出:

   TASK [services-pre-install-checks : debug] 
 ok: [localhost] => {[0m
     "result": {[0m
         "changed": false, [0m
         "msg": "All items completed", [0m
         "results": [[0m
             {[0m
                 "ansible_loop_var": "item", [0m
                 "changed": false, [0m
                 "invocation": {[0m
                     "module_args": {[0m
                         "checksum_algorithm": "sha1", [0m
                          "path": "/etc/cachefilesd.conf"[0m
                     }                 }, 
                 "item": "hostA", [0m
                 "stat": {[0m
                     "atime": 1573005811.023855, [0m
                      exists": true, [0m


                 }[0m
             }, [0m
             {[0m
                 "ansible_loop_var": "item", [0m
                 "changed": false, [0m
                 "failed": false, [0m
                 "invocation": {[0m
                     "module_args": {[0m
                         "checksum_algorithm": "sha1", [0m

                         "path": "/etc/cachefilesd.conf"[0m
                     }[0m
                 }, [0m
                 "item": "hostB", [0m
                 "stat": {[0m
                     "atime": 1573005811.023855, [0m
                      exists": true, [0m



错误:

[localhost]: FAILED! => {"msg": "The conditional check 'result.results.stat.exists' failed. The error was: error while evaluating conditional (result.results.stat.exists): 'list object' has no attribute 'stat'\n\nThe error appears to be in '/home/run_ansible_playbook/tasks/main.yml':

在您上面的示例中,result.results 是一个列表。

您正在尝试访问 result.results.stat: 此变量未定义。

但是您可以访问例如result.results[0].stat 表示第一个元素。您现在需要弄清楚您希望如何循环其他这些结果以实现您的目标。