首页 > 解决方案 > hostvars 'list object' 没有属性

问题描述

我试图从 hostvars 获得一些结果,但失败了。

剧本:

- debug:
    msg: "{{hostvars[item]['a']}}"
  run_once: true
  delegate_to: localhost
  with_items: "{{ groups['all'] }}"

结果:

ok: [ansible8] => (item=ansible8) => {
    "msg": [
        {
            "ansible_job_id": "921062922128.33800",
            "attempts": 1,
            "end": "2021-09-16 05:39:47.335977",
             blah blah
            "start": "2021-09-16 05:39:46.323621"
        }
    ]
}
ok: [ansible8] => (item=ansible7) => {
    "msg": [
        {
            "ansible_job_id": "688007320452.57867",
            "attempts": 1,
            "end": "2021-04-03 22:12:40.249637",
             blah blah
            "start": "2021-04-03 22:12:39.241665"
        }
    ]
}

现在,我想得到 a.start 和 a.end,所以我的剧本是这样的:

- debug:
    msg: "{{hostvars[item]['a']['start']}}"
  run_once: true
  delegate_to: localhost
  with_items: "{{ groups['all'] }}"

但显然它没有用,并得到错误消息

“该任务包括一个带有未定义变量的选项。错误是:'列表对象'没有属性'开始'

感谢任何帮助,谢谢

标签: ansibleansible-factshostvars

解决方案


推荐阅读