0

I am trying to get some result from the hostvars , but fails.

playbook:

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

results:

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

Now, i would like to get the a.start and a.end, so my playbook like this:

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

But apparently it didn't work, and got the error msg

"The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'start'

Appreciate any help, thanks

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
sloweriang
  • 308
  • 4
  • 19
  • 5
    The error message is clear `'list object' has no attribute 'start'`. The variable `a` keeps a list. If you want the attribute `start` of the first element use `hostvars[item].a.0.start` – Vladimir Botka Apr 03 '21 at 15:08
  • Thanks, is that a must to use 0 ? can i use a wildcard to make sure it loop through all the list? – sloweriang Apr 03 '21 at 17:15
  • 2
    Yes. Use `hostvars[item].a|json_query('[].start')`. If you have other issues either [edit] the question, or open new one, and make it [mre]. – Vladimir Botka Apr 03 '21 at 17:29

0 Answers0