Pass hostname as argument into Fabric task
I'm trying to put together a fabric script that creates a virtual instance via an API and then runs puppet on that instance. I've got a task that creates the VM, and a task that can 'bootstrap' the VM. However, I'm having difficulty linking these together, because I'm not sure how to pass the some data generated in the first task into the second task as the hostname. E.g.
def createVM():
newhostname = local('/usr/bin/createVM')
bootstrap(newhostname)
def bootstrap(hostname):
env.hosts = [hostname]
run('puppet agent -t')
This doesn't appear to work, and I get prompted for the hostname to run the fabric script on, if I just execute fab createVM
.
What's the best way of doing this?
Solution 1:
You can have a look at the execute() function. You can use it to override on which hosts you run a task and pass extra arguments.
You probably need something along the lines of
def createVM():
newhostname = local('/usr/bin/createVM')
execute(bootstrap, hosts=[newhostname])
def bootstrap():
run('puppet agent -t')
Solution 2:
You may want to set it on the environment dictionary (env
), e.g:
env.newhostname = local('/usr/bin/createVM')
Or just set env.hosts
:
env.hosts = [local('/usr/bin/createVM')]
Then execute fab createVM bootstrap
You can see more details here:
http://docs.fabfile.org/en/1.10/usage/env.html