# delegate\_to /one member/ of a group

**URL:** <https://forum.ansible.com/t/delegate-to-one-member-of-a-group/19702>\
**Category:** Ansible Project\
**Created:** [November 5, 2015, 11:59pm UTC](https://forum.ansible.com/t/delegate-to-one-member-of-a-group/19702 "2015-11-05T23:59:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elliott\_Barrere](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@Elliott\_Barrere](https://forum.ansible.com/u/Elliott_Barrere)\
**Post date:** [November 5, 2015, 11:59pm UTC](https://forum.ansible.com/t/delegate-to-one-member-of-a-group/19702/1 "2015-11-05T23:59:14Z")

</div>

Hi there -

I’m trying to do something I think should be fairly easy, but I can’t seem to find the right magic. I’d like to run a set of commands on behalf of my server, against _any one of another group of servers_. Basically I need to run a script on _any one domain controller_ as part of the server setup process.

If I do something like this:

`  
delegate\_to: “{{ item }}”  
with\_items: groups.domain\_controllers

`

it will run on _all_ the servers in that group. I’ve tried adding run\_once: true and that doesn’t seem to help, and I’ve also tried using delegate\_to: [“dc1”, “dc2”], but it doesn’t seem to want a list.

I’ve also tried:

`retries: 1 register: result until: result.rc == 0`

which at least seems to execute the test, but I can’t seem to find a conditional check that works here…

The set of commands will all need to execute on the _same server_ (it’s copying a batch file up, executing it and returning the results). Is there a standard way to do this in ansible?

Any help is appreciated, and thanks in advance!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 6, 2015, 5:07pm UTC](https://forum.ansible.com/t/delegate-to-one-member-of-a-group/19702/2 "2015-11-06T17:07:42Z")

</div>

to delegate to a random machine in a group:

delegate\_to: "{{ groups.domain\_controllers|random}}"

for the 'first one':

delegate\_to: "{{ groups.domain\_controllers[0] }}"

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

---

<div class="post-metadata">

**Author:** ![Elliott\_Barrere](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@Elliott\_Barrere](https://forum.ansible.com/u/Elliott_Barrere)\
**Post date:** [November 6, 2015, 7:05pm UTC](https://forum.ansible.com/t/delegate-to-one-member-of-a-group/19702/3 "2015-11-06T19:05:20Z")

</div>

Ah, thanks! So I suppose I should assign the output of {{ groups.domain\_controllers|random }} to a variable to ensure all tasks run on the same machine?

---

<div class="post-metadata">

**Author:** ![Elliott\_Barrere](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@Elliott\_Barrere](https://forum.ansible.com/u/Elliott_Barrere)\
**Post date:** [November 18, 2015, 11:35pm UTC](https://forum.ansible.com/t/delegate-to-one-member-of-a-group/19702/4 "2015-11-18T23:35:51Z")

</div>

Okay, just getting back to this. I have this in my site.yaml file in group\_vars:

`rwdc_server: '{{groups.rwdcs|random}}'`

but it appears that the rwdc\_server variable gets a different domain controller from the group each time it’s accessed (sometimes it’s the same one, but not always). I need the value to be consistent so I can run a series of commands on the same box. Is this possible?

I’d rather not pin it to a single box (with e.g. {{groups.rwdcs[0]}}), for basic load balancing & HA…

---

<div class="post-metadata">

**Author:** ![Zarko\_Aleksic](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@Zarko\_Aleksic](https://forum.ansible.com/u/Zarko_Aleksic)\
**Post date:** [March 16, 2018, 12:52am UTC](https://forum.ansible.com/t/delegate-to-one-member-of-a-group/19702/5 "2018-03-16T00:52:45Z")

</div>

I was working on the similar thing and stumbled across your post. Might be too late for you, but it might help someone else. You can achieve this easily by setting random server as a fact once at the beginning of the playbook. Then referencing that (now static) fact as part of delagate\_to.

I used this:

`

- name: Choosing random DB server to use  
set\_fact:  
random\_server: “{{ groups.groupname|random}}”

- name: executing the script on random server  
shell: /path/to/script  
delegate\_to: “{{ random\_server }}”

`

---

<div class="post-metadata">

**Author:** ![rashmi\_nair](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@rashmi\_nair](https://forum.ansible.com/u/rashmi_nair)\
**Post date:** [August 7, 2019, 6:42pm UTC](https://forum.ansible.com/t/delegate-to-one-member-of-a-group/19702/6 "2019-08-07T18:42:06Z")

</div>

Hi All,

I have a task which has multiple with\_items and hence it picks the latest defined item (arbiter) in the delegate which is not the expected result:

````auto

~~~
  run_once: true
  delegate_to: "{{ item }}"
  with_items:
     - "{{ groups['mongodb-active'] }}"
  shell: /usr/bin/mongo --eval 'printjson(rs.add("{{ item }}:27017"))'
  with_items:
    - "{{ groups['mongodb-arbiter'] }}"```
~~~

~~~

````
