delegate_to /one member/ of a group

4,283 views
Skip to first unread message

Elliott Barrere

unread,
Nov 5, 2015, 7:30:12 PM11/5/15
to Ansible Project
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!

Brian Coca

unread,
Nov 6, 2015, 12:07:49 PM11/6/15
to Ansible Project
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

Elliott Barrere

unread,
Nov 6, 2015, 2:05:20 PM11/6/15
to Ansible Project
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?

Elliott Barrere

unread,
Nov 18, 2015, 6:35:51 PM11/18/15
to Ansible Project
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...

Žarko Aleksić

unread,
Mar 15, 2018, 8:52:45 PM3/15/18
to Ansible Project
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 }}"

rashmi nair

unread,
Aug 7, 2019, 2:42:06 PM8/7/19
to Ansible Project

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:


```

  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'] }}"```
```

I tried using below too, but failed - "ERROR! Unexpected Exception, this is probably a bug: unhashable type: 'list'":



- name: Choosing active server

  set_fact:

    active_server: "{{ groups['mongodb-active'] }}"

    

- name: Add secondaries 

  run_once: true

  delegate_to: "{{ active_server }}"

  shell: /usr/bin/mongo --eval 'printjson(rs.add("{{ item }}:27017"))'

  with_items:

    - "{{ groups['mongodb-arbiter'] }}"

Reply all
Reply to author
Forward
0 new messages