delegate_to with --extra-vars usage

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

do --extra-vars="v_grp=testgrp"

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

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

quotes!:

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

Thanks for the hint

Thanks Brian.

This pretty much nailed it

Dayo