How do I use information from Dynamic Inventory Dictionary _meta when executing on localhost?

52 views
Skip to first unread message

Isaac Kane Egglestone

unread,
Feb 11, 2018, 2:07:33 AM2/11/18
to Ansible Project
Excuse me if this has been covered in another post, I have searched diligently in documentation and on this group however the wording could be very different depending on the interpretation.
If this is covered anywhere already then I apologise and  just let me know where so I can read about it there.

I would like to execute a playbook on localhost but use the host information from dynamic inventory to make conditions about how a role or playbook operates.
I'm executing a playbook on localhost because I'm just doing tasks in azure that doesn't affect the hosts directly.

If I set the hosts to "azure" for example then it tries of course to execute my playbook with_items loop on every single host in azure, which of course is madness and not what I want.

When  I set hosts to localhost however the hostvars variable of course no longer contains information about the dynamic inventory hosts so doing things like:
    when: hosts_vars[inventory_hostname].os_disk.operating_system_type == "Windows"
or 
when: hosts_vars[item].os_disk.operating_system_type == "Windows"

Doesn't work of course because host_vars is undefined.

Now of course I could use tags to only select windows servers however if the tags are not applied to the windows machines yet I would want to use the ansible azure module to tag them.

But then I can't make a decision on how to tag them without being able to access the dynamic inventory information.
So I'm in a catch 22 situation.

Now I can of course write some python or powershell to go tag my hosts appropriately and then come back to ansible and then run my tasks, however this is a workaround and no longer an ansible solution.

Now you can also say when I create the machines in azure I should tag them appropriately, but then this is not always possible if I come into an environment with everything already built and not yet having devops mechanisms  fully implemented and still need to make mass changes to systems in the meantime while I'm converting everything.

I tried setting a role variable to {{ _meta['hostsvars'] }} to try to capture the original dictionary in the azure_rm.py output however its also undefined.

Is there some way to get the incredibly useful information from that dictionary without having to set hosts to something other than localhost 
Is there some other way to do this that I'm just not clueing into?  Seems like  someone should have run into this problem previously, yet I don't see it on this forum.



 

Reply all
Reply to author
Forward
0 new messages