Use hostvars as hosts names in playbooks

20 views
Skip to first unread message

Emmanuel Lacour

unread,
Sep 2, 2015, 8:24:11 AM9/2/15
to Ansible Project
Dear ansible users,

I try to write a paybook where I first get a host name from a host, then would like to do thinks on this host name.

Here is a try:

---
- name: Get VM inventory
  hosts: "{{vm}}"
  gather_facts: False
  tasks:
    - debug: msg="{{hostvars[vm]['cluster']}}"

- name: Get free hypervisor
  hosts: '{{hostvars[vm]["cluster"]}}'
  gather_facts: False
  tasks:
    - debug: msg='{{hostvars[vm]["cluster"]}}'


Which I call with ansible-playbook myplaybook.yml -e vm=foo

debug on "Get VM inventory" print the right cluster name
"Get free hypervisor" doesn't match any host. Thought if I force the cluster name in hosts, the debug properly display the vm cluster name.


Is this unsupported? A syntax error?

Brian Coca

unread,
Sep 2, 2015, 8:34:02 AM9/2/15
to Ansible Project
hostvars are not usable in 'hosts:' clause, use group_by if you want
to create adhoc groups for use in subsequent plays.



--
Brian Coca

Emmanuel Lacour

unread,
Sep 2, 2015, 3:33:18 PM9/2/15
to Ansible Project


Le mercredi 2 septembre 2015 14:34:02 UTC+2, Brian Coca a écrit :
hostvars are not usable in 'hosts:' clause, use group_by if you want
to create adhoc groups for use in subsequent plays.


thanks, thought I discovered delegate_to which seems to be exacly what I need, i.e. a playbook on the new vm with preliminary tasks executed on hypervisors to setup the vm, then tasks on the vm, then I may add tasks delegated_to backup servers to register host for example. Nice!
Reply all
Reply to author
Forward
0 new messages