Pass hostname as argument into Fabric task

fabricpuppet

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?

Best Answer

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')