I need to sort the hosts from a roledef so they can run those tasks in a certain order.
I'm implementing a PostgreSQL BDR (http://2ndquadrant.com/en-us/resources/bdr/) deployer. In order to succeed, you need to create a BDR group in a host and only then join the BDR group in all other hosts.
The user need to choose which would be the first host from the list.
----EDITED----
I try to set env.hosts dynamically but it's not working.
env.roledefs = {
'array1': [],
}
env.hostsdefs = {
'array1': [
{'host': 'data-03', 'server': 'root@data-03'},
{'host': 'data-01', 'server': 'root@data-01'},
],
}
@serial
def sort_and_echo(default_host):
sort_host(default_host)
echoing()
@serial
def sort_host(default_host):
hostnames = env.hostsdefs[env.roles[0]]
new_hosts = []
for host in (hostnames):
if (host['host'] != default_host):
new_hosts.append(host['server'])
else:
new_hosts = [host['server']] + new_hosts
env.hosts = new_hosts
@serial
def echoing():
print env.hosts
print('current host: %s' % (env.host_string))
This way, if I try:
fab -R array1 sort_and_echo:default_host=data-03
['root@data-03', 'root@data-01']
current host: None
Done.
It will not run echoing for each server in the list.
But if I try one sort and then echoing in the same command:
fab -R array1 sort_host:default_host=data-03 echoing
It will provide the expected output:
[root@data-03] Executing task 'echoing'
['root@data-03', 'root@data-01']
current host: root@data-03
[root@data-01] Executing task 'echoing'
['root@data-03', 'root@data-01']
current host: root@data-01
Done.
How can I change the hosts list in runtime?
After a while I solve my problem. It was easier than expected. No hacks needed.
Passing the hosts parameter would make the host to be added at the begging of the array. As the standard behaviour of fabric is to throw out deduplication (check Host list deduplication in http://docs.fabfile.org/en/1.10/usage/execution.html#combining-host-lists), it solves the problem.
If you don't pass the parameter, it will use the first one of the array
So when I try:
It will run in the correct order: