Brian Carpio
Brian Carpio

Reputation: 491

Python Fabric Task Doesn't Set Variable?

I'm not sure why this is happening but in fabric when I try and set a variable like this:

@task
def deploy_small_ec2_ami(name):
    a = local("/usr/bin/ldapsearch -x -w secret -D 'cn=admin,dc=example,dc=net' -b 'dc=example,dc=net' -h ldap-01  -LLL 'cn=%s'" %(name))
    if a:
        sys.exit(1)

No matter what the variable 'a' is never set even if the name exists in ldap so the exit never take effect. I have tried to add 'print a' and I don't see anything either, but I know the command works because I get output as follows (host that does exist in ldap):

user:~/Projects/fabtask$ fab deploy_small_ec2_ami:bob,ldap
[localhost] local: /usr/bin/ldapsearch -x -w secret -D 'cn=admin,dc=example,dc=net' -b 'dc=example,dc=net' -h ldap-01  -LLL 'cn=bob'
dn: cn=bob,ou=hosts,dc=example,dc=net
cn: bob
environment: production
ipHostNumber:
objectClass: device
objectClass: ipHost
objectClass: puppetClient
objectClass: top
puppetClass: cassandra      

I use var = local('some_command') in other scripts and it works fine.

Upvotes: 2

Views: 1064

Answers (1)

kindall
kindall

Reputation: 184091

I don't see a capture=True on your local() call; that seems to be necessary to capture the output of your command. Otherwise local() always returns None, which evaluates to False in an if statement. See docs here.

Upvotes: 4

Related Questions