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?