That was exactly it, thanks!


From: Todd DeLuca [mailto:[email protected]]
Sent: Wednesday, January 23, 2013 4:41 PM
To: Stroehmann, James
Cc: [email protected]
Subject: Re: [Fab-user] execute command depending on role

This should work for you.  I think the main problem is how you check the that 
the host_string is in the roledefs dict.

from fabric.api import env, run, task

env.roledefs = {
    'prodmail': ['[email protected]', '[email protected]'],
    'prodweb': ['[email protected]', '[email protected]']
}

@task
def OOpsBroke():
    if env.host_string in env.roledefs['prodmail']:
        print 'uname -n', env.host_string
    elif env.host_string in env.roledefs['prodweb']:
        print 'uname -a', env.host_string
    else:
        print 'uname -v', env.host_string


On Wed, Jan 23, 2013 at 12:18 PM, Stroehmann, James 
<[email protected]<mailto:[email protected]>> wrote:
[Cut and paste error corrected]

I am trying to get fabric to execute different commands based on which role 
they are placed in, but my if statement is not working as expected. In the 
fabfile below, the first task works (OOpsWorks) but the second only executes 
the "else":

---
from fabric.api import *

env.roledefs = {
    'prodmail': ['[email protected], '[email protected]'],
    'prodweb': ['[email protected]', '[email protected]']
}

@task
def OOpsWorks():
    run('uname -n')

@task
def OOpsBroke():
    if env.host_string in env.roledefs == 'prodmail':
       run('uname -n')
    elif env.host_string in env.roledefs == 'prodweb':
       run('uname -a')
    else:
       run('uname -v')

----

# fab -R prodmail OOpsWorks
[[email protected]] Executing task 'OOpsWorks'
[[email protected]] run: uname -n
[[email protected]] out: mail1.tld

[[email protected]] Executing task 'OOpsWorks'
[[email protected]] run: uname -n
[[email protected]] out: mail2.tld

Done.
Disconnecting from [email protected]<mailto:[email protected]>... done.
Disconnecting from [email protected]<mailto:[email protected]>... done.

---

# fab -R prodmail OOpsBroke
[[email protected]] Executing task 'OOpsBroke'
[[email protected]] run: uname -v
[[email protected]] out: #1 SMP Sun Jul 31 16:44:56 EDT 2011

[[email protected]] Executing task 'OOpsBroke'
[[email protected]] run: uname -v
[[email protected]] out: #1 SMP Sun Jul 31 16:44:56 EDT 2011

Done.
Disconnecting from [email protected]<mailto:[email protected]>... done.
Disconnecting from [email protected]<mailto:[email protected]>... done.

---

_______________________________________________
Fab-user mailing list
[email protected]<mailto:[email protected]>
https://lists.nongnu.org/mailman/listinfo/fab-user



--
Todd DeLuca
http://todddeluca.com
http://wall.hms.harvard.edu/

_______________________________________________
Fab-user mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/fab-user

Reply via email to