this post was submitted on 11 Jun 2024
4 points (83.3% liked)

Ansible

232 readers
2 users here now

# TODO

founded 1 year ago
MODERATORS
 

I have an inventory of hosts, and from them, one of the tasks should choose a leader by running a command on each until one of the machines produces an expected output (json value.)

I want some code to run on that leader to initialize it, and then I want some of the other roles to delegate some tasks to that leader.

Not sure how to do this?

Should I use dynamic inventory to analyze a group of hosts, and create a new group (can you run built_in.command in dynamic inventory? Should I write a role task that runs the identifying command on each host, capturing the result globally if it returns what I want (but then running on each host even if I have found my leader?)

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 3 months ago (2 children)

Me either, but we have a role to bootstrap galara mysql and we just use the first host in a group

[–] [email protected] 1 points 3 months ago

sounds like the same idea. So you use run_once on a task?

[–] [email protected] 1 points 3 months ago (1 children)

I thought that there might be a way to a custom facts (gather_facts) to all machines, and indicate leader/joiner status there, but I can't get the data to stick.

[–] [email protected] 1 points 3 months ago* (last edited 3 months ago)

I think the idea to register the leader status during a dedicated task like Matt said is the better move.

You got this module if you want to create dynamic group during play :

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html

And during gather_facts, you could add your own custom facts :

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html#adding-custom-facts

Bash script example returning json :

#!/bin/bash
leader_status=$(curl http://127.0.0.1/leader_status)
echo {\"leader\" : \"${leader_status}\"}