Pass variable to hosts: in playbook

361 views
Skip to first unread message

Piet83

unread,
Jan 20, 2015, 8:37:03 AM1/20/15
to ansible...@googlegroups.com
---
- 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'
    delegate_to
: localhost
   
register: ip

# Continue play on newly created vm
- hosts: '{{ ip }}'

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

I'm trying to create a playbook to provision a virtual machine and apply some roles to that vm without knowing name or ip. When provisioning the vm through our API it spits out the vmname and ipadress to the stdout. When I register the stdout to a variable I can't pass it as you see above to: - hosts: '{{ ip }}'
it always says: skipping: no hosts matched

Is the above possible? Or is it possible to use delegate_to for a role. Maybe there a other ways?

James Martin

unread,
Jan 20, 2015, 9:39:00 AM1/20/15
to ansible...@googlegroups.com
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 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 module.


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

- James

Piet83

unread,
Jan 20, 2015, 10:58:23 AM1/20/15
to ansible...@googlegroups.com
---
- 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'
    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 }}'





Op dinsdag 20 januari 2015 15:39:00 UTC+1 schreef James Martin:

James Martin

unread,
Jan 20, 2015, 11:04:44 AM1/20/15
to ansible...@googlegroups.com
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

Tom Bamford

unread,
Jan 22, 2015, 3:54:16 AM1/22/15
to ansible...@googlegroups.com

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

On 20 January 2015 at 18:04, James Martin <jma...@ansible.com> wrote:

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


On Tuesday, January 20, 2015 at 10:58:23 AM UTC-5, Piet83 wrote:
---
- 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'
    delegate_to
: localhost
   
register:
vm


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

,'') }}"

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/357a7e9c-f468-47cc-b038-8b7a228e2584%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages