# delegate\_to with --extra-vars usage

**URL:** <https://forum.ansible.com/t/delegate-to-with-extra-vars-usage/27535>\
**Category:** Ansible Project\
**Created:** [May 15, 2018, 3:51pm UTC](https://forum.ansible.com/t/delegate-to-with-extra-vars-usage/27535 "2018-05-15T15:51:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![deewon](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@deewon](https://forum.ansible.com/u/deewon)\
**Post date:** [May 15, 2018, 3:51pm UTC](https://forum.ansible.com/t/delegate-to-with-extra-vars-usage/27535/1 "2018-05-15T15:51:24Z")

</div>

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](https://groups.google.com/forum/#!topic/ansible-project/wfsS1b8Qq64)

I’m assuming this is definitely possible in ansible?

Thanks  
Dayo

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 15, 2018, 4:02pm UTC](https://forum.ansible.com/t/delegate-to-with-extra-vars-usage/27535/2 "2018-05-15T16:02:07Z")

</div>

do --extra-vars="v\_grp=testgrp"

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 15, 2018, 5:03pm UTC](https://forum.ansible.com/t/delegate-to-with-extra-vars-usage/27535/3 "2018-05-15T17:03:20Z")

</div>

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

&nbsp;&nbsp;&nbsp;delegate\_to: {{ groups[v\_grp] | random }}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 15, 2018, 5:28pm UTC](https://forum.ansible.com/t/delegate-to-with-extra-vars-usage/27535/4 "2018-05-15T17:28:15Z")

</div>

quotes!:

delegate\_to: "{{ groups[v\_grp] | random }}"

---

<div class="post-metadata">

**Author:** ![deewon](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@deewon](https://forum.ansible.com/u/deewon)\
**Post date:** [May 17, 2018, 8:14am UTC](https://forum.ansible.com/t/delegate-to-with-extra-vars-usage/27535/5 "2018-05-17T08:14:18Z")

</div>

Thanks for the hint

---

<div class="post-metadata">

**Author:** ![deewon](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@deewon](https://forum.ansible.com/u/deewon)\
**Post date:** [May 17, 2018, 8:15am UTC](https://forum.ansible.com/t/delegate-to-with-extra-vars-usage/27535/6 "2018-05-17T08:15:23Z")

</div>

Thanks Brian.

This pretty much nailed it

Dayo
