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.
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?
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?
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…
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 }}”