I want to make a task use a different set of hosts (role) depending on which network I’m currently in. If I’m in the same network of my servers, I don’t need to go through the gateway.
Here’s a snippet from my fabfile.py:
env.use_ssh_config = True
env.roledefs = {
'rack_machines': ['rack4', 'rack5', 'rack6', 'rack7'],
'external_rack_machines': ['erack4', 'erack5', 'erack6', 'erack7']
}
@roles('rack_machines')
def host_type():
run('uname -s')
So, for my task host_type(), I’d like its role to be rack_machines if I’m in the same network as rack4, rack5, etc. Otherwise, I’d like its role to be external_rack_machines, therefore going through the gateway to access those same machines.
Maybe there’s a way to do this with ssh config alone. Here’s a snippet of my ssh_config file as well:
Host erack4
HostName company-gw.foo.bar.com
Port 2261
User my_user
Host rack4
HostName 10.43.21.61
Port 22
User my_user
Role definitions are taken into account after module has been imported. So you can place some code in your
fabfilewhich executes on import, detects network and set appropriateroledefs.Second way to achieve a goal is to use “flag-task”. This is a task which does nothing but set appropriate roledefs. I.e.:
And invoke that way:
fab set_hosts:external_rack business