delegate_to with --extra-vars usage

20 views
Skip to first unread message

deewon

unread,
May 15, 2018, 11:51:24 AM5/15/18
to Ansible Project
Trying to do something that seems straightforward but can't work out why its failing.

I'm simply trying to pass a random server within a  group whose name is supplied at execution time to a task using "delegate_to" and  "--extra-vars" but it doesn't like it i.e


  delegate_to: {{ groups.v_grp | random }}

  

followed by:

 
ansible-playbook playbook.yml --extra-vars="{{ v_grp=testgrp }}"



Execution throws up the below error. 

fatal: [machine1]: FAILED! => {"msg": "'dict object' has no attribute 'v_grp'"}


The group does exist and the playbook actually executes when the group name testgrp is hard coded. Unfortunately, the idea is to be able to pass group names in dynamically at runtime

I've trawled through the web for any hints around getting this working but there's been no luck. The closes I found was a suggestion on another  thread (dated 2014) but it didn't work either https://groups.google.com/forum/#!topic/ansible-project/wfsS1b8Qq64

I'm assuming this is definitely possible in ansible?


Thanks
Dayo

Brian Coca

unread,
May 15, 2018, 12:02:24 PM5/15/18
to Ansible Project
do --extra-vars="v_grp=testgrp"


--
----------
Brian Coca

Kai Stian Olstad

unread,
May 15, 2018, 1:03:45 PM5/15/18
to ansible...@googlegroups.com
On 15.05.2018 17:51, 'deewon' via Ansible Project wrote:
> Trying to do something that seems straightforward but can't work out
> why
> its failing.
>
> I'm simply trying to pass a random server within a group whose name is
> supplied at execution time to a task using "delegate_to" and
> "--extra-vars" but it doesn't like it i.e
>
>
> delegate_to: {{ groups.v_grp | random }}

In addition to Brian answer you also need to write this as

delegate_to: {{ groups[v_grp] | random }}

--
Kai Stian Olstad

Brian Coca

unread,
May 15, 2018, 1:28:25 PM5/15/18
to Ansible Project
quotes!:

delegate_to: "{{ groups[v_grp] | random }}"





--
----------
Brian Coca

deewon

unread,
May 17, 2018, 4:14:18 AM5/17/18
to Ansible Project
Thanks for the hint

deewon

unread,
May 17, 2018, 4:15:24 AM5/17/18
to Ansible Project
Thanks Brian.

This pretty much nailed it

Dayo
Reply all
Reply to author
Forward
0 new messages