ansible

hostvars failing with 'ansible.vars.hostvars.HostVarsVars object has no element'


I have a playbook that's running on two hosts, pulling data from host1 in a registered variable and then using those data in a loop on host2 using hostvars but it's failing with ansible.vars.hostvars.HostVarsVars object has no element.

I'm specifying the hosts at run time using -l "host1,host2" then using when to specify which task to run on which host. (There may well be a better way of doing this).

- command: /some/command
  loop: "{{ hostvars['host1.example.com'][registered_var][stdout_lines] }}"
  when: "'host2' in inventory_hostname"
  register: output

- debug: var=output.results

Here's the first task that runs on host1:

- command: /some/command
  when: "'host1' in inventory_hostname"
  register: results

- debug: var=results.stdout_lines 

and here's the error

skipping: [host1.example.com]
fatal: [host2.example.com]: FAILED! => {}

MSG:

ansible.vars.hostvars.HostVarsVars object has no element {'skip_reason': u'Conditional result was False', 'skipped': True, 'changed': False}

Solution

  • Apologies for the delay in responding. Thanks for the suggestions. After some additional thought and testing I decided ansible may not be the right tool for the job but I now have some additional approaches I can consider for future projects. Thanks again