# Pass variable to hosts: in playbook

**URL:** <https://forum.ansible.com/t/pass-variable-to-hosts-in-playbook/17328>\
**Category:** Ansible Project\
**Created:** [January 20, 2015, 1:37pm UTC](https://forum.ansible.com/t/pass-variable-to-hosts-in-playbook/17328 "2015-01-20T13:37:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Smallegange](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@Peter\_Smallegange](https://forum.ansible.com/u/Peter_Smallegange)\
**Post date:** [January 20, 2015, 1:37pm UTC](https://forum.ansible.com/t/pass-variable-to-hosts-in-playbook/17328/1 "2015-01-20T13:37:03Z")

</div>

`

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [January 20, 2015, 2:39pm UTC](https://forum.ansible.com/t/pass-variable-to-hosts-in-playbook/17328/2 "2015-01-20T14:39:00Z")

</div>

Assuming your first task returns an ip address, if you want to reference it, you’d need to use {{ ip.stdout }}. Alternately, you could use the [http://docs.ansible.com/uri\_module.html](http://docs.ansible.com/uri_module.html) instead of using curl, and you could just use {{ ip }}.

You’ll also have to add it to the inventory before you could use it on the following play, see the [http://docs.ansible.com/add\_host\_module.html](http://docs.ansible.com/add_host_module.html) module.

If you want extra credit, write a module for your API that provisions instances and a dynamic inventory script :).

- James

---

<div class="post-metadata">

**Author:** ![Peter\_Smallegange](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@Peter\_Smallegange](https://forum.ansible.com/u/Peter_Smallegange)\
**Post date:** [January 20, 2015, 3:58pm UTC](https://forum.ansible.com/t/pass-variable-to-hosts-in-playbook/17328/3 "2015-01-20T15:58:23Z")

</div>

`---`  
`- hosts: all`

`pre_tasks:`

`# Task creates a vm through an api. Register output with vm name`  
`- name: Create vm from emplate`  
`command: 'curl [https://vm.example.nl/api/create](https://vm.example.nl/api/create)'`  
`delegate_to: localhost`  
`register: vm`

`# Filter out exact vm name`  
`- name: Define vm variable`  
`set_fact: vmname="{{ (vm.stdout.split(' ')[2]) | regex_replace('},$','') }}"`  
`delegate_to: localhost`

`- name: Wait for running vm`  
`shell: 'curl https://vm.example.nl/api/{{ vmname }}'`  
`delegate_to: localhost`  
`register: result`  
`until: result.stdout.find("running") != -1`  
`retries: 15`  
`delay: 20`

`# This tasks makes sure the newly created vm is in the inventory file`  
`- name: Regenerate vm inventory`  
`shell: /scripts/generate_vm_invantory.sh`  
`delegate_to: localhost`

`# Continue play on newly created vm, the problem is here. vmname is not getting passed to hosts:`  
`- hosts: '{{ vmname }}'`

`# Roles to configure newly created vm`  
`roles:`  
`- common`  
`- apt-updates`  
`- webserver`  
``

Ok, the above is more like my actual playbook. I’m using a shell script to regenerate the inventory file which I use to run the above playbook. The variable vmname is set and contains the vmname but it won’t get passed to: `hosts: '{{ vmname }}'`

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [January 20, 2015, 4:04pm UTC](https://forum.ansible.com/t/pass-variable-to-hosts-in-playbook/17328/4 "2015-01-20T16:04:43Z")

</div>

See my original response. You’ll need to use the add\_host module to add {{ vmname }} to the inventory if you want to use it on the next play. Also, you playbook will be much cleaner if you use the URI module rather than shelling out to curl.

- James

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 22, 2015, 8:53am UTC](https://forum.ansible.com/t/pass-variable-to-hosts-in-playbook/17328/5 "2015-01-22T08:53:53Z")

</div>

Agreed with James, dynamically adding hosts to groups is the way to go. I believe that the hosts: lines are evaluated before the playbook is run, so the only variables you can use there are ones set with `-e vmname=something`
