Tom Bodet
Tom Bodet

Reputation: 1

Fabric sudo call - Cannot hide fatal error

Running Fabric 1.10, Python 2.7 on RHEL6.5. We have a cloud environment where we provide OS layer support and there is no central LDAP so everything is done via scripting, OS level accounts and RSA keys. One thing you have to do is keep an eye on your account to ensure that your key is working but also that your password on each system is correct so you can sudo. I devised a small test specifically for this....

def sudotest(cmd="echo TEST"):
    with hide('status','warnings','stderr', 'stdout','running','everything','status'), settings(warn_only=True):
            env.abort_on_prompts = True
            try:
                result=sudo(cmd)
                if (result.succeeded):
                    print(env.host + ":SUCCESS")
                else:
                    print(env.host + ":FAIL")
            except SystemExit:
                print(env.host + ":FAIL")
            except:
                print(env.host + ":FAIL - catchall")

You'll notice I've been trying all kinds of settings and even doubling up as I've seen odd behavior with hiding output. For example without the double 'status' I still see 'disconnecting' messages. So I've tried throwing 'everything' and 'stderr' all over with no change in output. I added the inclusive except: just to see if it would help, no change. Also no change when trying an else:

In a good run I see this output:

[host1] Executing task 'sudotest'
host1    :SUCCESS
host2    :SUCCESS

So first observation is I'm still getting that first running message. Otherwise it's exactly what I want.

On an intentionally failed run of the same host list I get:

[host1] Executing task 'sudotest'
[host1] out: Sorry, try again.
[host1] out: sudo password:
Fatal error: Needed to prompt for a connection or sudo password (host: host1), but abort-on-prompts was set to True

Aborting.
host1    :FAIL
[host2] out: Sorry, try again.
[host2] out: sudo password:
Fatal error: Needed to prompt for a connection or sudo password (host: host2), but abort-on-prompts was set to True

Aborting.
host2    :FAIL

I've got the following env settings:

env.disable_known_hosts = True
env.skip_bad_hosts = True
env.remote_interupt = True
env.warn_only = True
env.eagerly_disconnect = True

I'm running it through fab, not python, i.e.: fab sudotest

Usually with me it's because I'm missing something simple. I can live with the first running message, it's the fatal error and the extra stderr output that turns a nice clean output into a wreck.

So what's missing? Thanks.

Upvotes: 0

Views: 1216

Answers (2)

etreus
etreus

Reputation: 51

try with:

import fabric.api as fab
fab.output['aborts'] = False

I looked at fabric abort function and there is a check on "output.aborts"

more info here output_controls

Upvotes: 0

Gabriel Bull
Gabriel Bull

Reputation: 349

You can try with quiet=True:

output = sudo(cmd, quiet=True):

Upvotes: 1

Related Questions