Search code examples
pythonfabric

Python Fabric inserting a variable to the run command


I'm trying to add a variable from my config file config.PATH to this Fabric run command:

run('cd $(config.PATH); mkdir releases; mkdir shared; mkdir packages;', fail='ignore')

But I get the error:

typeError: run() got an unexpected keyword argument 'fail'

How can I achieve that I'm trying to do? or is there a better way?


Solution

  • What the error is telling you is that run doesn't accept an argument named fail, and indeed, if you look at the fabric docs for the run() function, you will find no mention of such parameter.

    This has nothing to do with trying to inject a variable in your command.

    If your purpose is to ignore errors, you should use either warn_only or quiet. From the documentation:

    To ignore non-zero return codes, specify warn_only=True. To both ignore non-zero return codes and force a command to run silently, specify quiet=True.

    As for the injection of the config variable, I do not believe it will work. In general, you can use python's regular string formatting facilities, but for your use case you're best to use fabric's cd context manager:

    with cd(config.PATH):
        run('YOUR COMMAND HERE')
    

    This will change the remote working directory for the duration of the command (or commands) in the block.