Discussion:
[Fab-user] Finding out which hosts are alive
Paul Hoffman
2015-09-15 23:52:04 UTC
Permalink
Greetings. I want a script to do an update to all of the hosts that are
alive, but not spend time on ones that aren't. I tried the following as a
way of filling env.hosts:

@hosts("***@host1", "***@host2")
def PickHosts():
PickAttempt = run("echo")
if PickAttempt.succeeded:
(env.hosts).append(env.host_string)
print("Adding {}".format(env.host_string))
PickHosts()

def DoTheUpdates(): ...

However, running this with a command line of "fab DoTheUpdates", prompts:
No hosts found. Please specify (single) host string for connection:

How can I get the above code to work? Or if this is too much of a kludge,
what is the proper way to have DoTheUpdates try a bunch of hosts but
gracefully stop each time it can't log into one?

--Paul Hoffman
Carlos García
2015-09-16 07:52:45 UTC
Permalink
I think --skip-bad-hosts would do the work.

Try:

fab DoTheUpdates --skip-bad-hosts -H <comma-separated list of hosts>

Regards
Post by Paul Hoffman
Greetings. I want a script to do an update to all of the hosts that are
alive, but not spend time on ones that aren't. I tried the following as a
@hosts("***@host1", "***@host2")
PickAttempt = run("echo")
(env.hosts).append(env.host_string)
print("Adding {}".format(env.host_string))
PickHosts()
def DoTheUpdates(): ...
How can I get the above code to work? Or if this is too much of a kludge,
what is the proper way to have DoTheUpdates try a bunch of hosts but
gracefully stop each time it can't log into one?
--Paul Hoffman
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Paul Hoffman
2015-09-16 16:21:46 UTC
Permalink
--skip-bad-hosts (or env.skip_bad_hosts) doesn't seem to work for this.
That is, using either (or both), DoTheUpdates says:

Warning: Timed out trying to connect to host2 (tried 1 time)
Underlying exception:
timed out

That doesn't seem like "skipping". What else do I need to do here to make
bad hosts skipped silently?

On Wed, Sep 16, 2015 at 12:52 AM, Carlos García <
Post by Carlos García
I think --skip-bad-hosts would do the work.
fab DoTheUpdates --skip-bad-hosts -H <comma-separated list of hosts>
Regards
Post by Paul Hoffman
Greetings. I want a script to do an update to all of the hosts that are
alive, but not spend time on ones that aren't. I tried the following as a
@hosts("***@host1", "***@host2")
PickAttempt = run("echo")
(env.hosts).append(env.host_string)
print("Adding {}".format(env.host_string))
PickHosts()
def DoTheUpdates(): ...
How can I get the above code to work? Or if this is too much of a kludge,
what is the proper way to have DoTheUpdates try a bunch of hosts but
gracefully stop each time it can't log into one?
--Paul Hoffman
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Carlos García
2015-09-16 16:33:20 UTC
Permalink
Hi Paul,

if you want to hide this kind of output, you can use --hide

fab DoTheUpdates --skip-bad-hosts --hide=warnings -H <comma-separated list
of hosts>

Check this out if you want to hide other kinds of output messages [
http://docs.fabfile.org/en/1.10/usage/output_controls.html]


Regards
Post by Paul Hoffman
--skip-bad-hosts (or env.skip_bad_hosts) doesn't seem to work for this.
Warning: Timed out trying to connect to host2 (tried 1 time)
timed out
That doesn't seem like "skipping". What else do I need to do here to make
bad hosts skipped silently?
On Wed, Sep 16, 2015 at 12:52 AM, Carlos García <
Post by Carlos García
I think --skip-bad-hosts would do the work.
fab DoTheUpdates --skip-bad-hosts -H <comma-separated list of hosts>
Regards
Post by Paul Hoffman
Greetings. I want a script to do an update to all of the hosts that are
alive, but not spend time on ones that aren't. I tried the following as a
@hosts("***@host1", "***@host2")
PickAttempt = run("echo")
(env.hosts).append(env.host_string)
print("Adding {}".format(env.host_string))
PickHosts()
def DoTheUpdates(): ...
How can I get the above code to work? Or if this is too much of a
kludge, what is the proper way to have DoTheUpdates try a bunch of hosts
but gracefully stop each time it can't log into one?
--Paul Hoffman
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Paul Hoffman
2015-09-16 23:46:35 UTC
Permalink
--hide hides *all* warnings, not just host-unreachable warnings, correct?
That is certainly not what I want.

To recap: I want to do an initial silent check for the host list to see
which are not up, and then only run the main procedures on the hosts known
to be up. Is this possible with Fabric?

On Wed, Sep 16, 2015 at 9:33 AM, Carlos García <
Post by Carlos García
Hi Paul,
if you want to hide this kind of output, you can use --hide
fab DoTheUpdates --skip-bad-hosts --hide=warnings -H <comma-separated
list of hosts>
Check this out if you want to hide other kinds of output messages [
http://docs.fabfile.org/en/1.10/usage/output_controls.html]
Regards
Post by Paul Hoffman
--skip-bad-hosts (or env.skip_bad_hosts) doesn't seem to work for this.
Warning: Timed out trying to connect to host2 (tried 1 time)
timed out
That doesn't seem like "skipping". What else do I need to do here to make
bad hosts skipped silently?
On Wed, Sep 16, 2015 at 12:52 AM, Carlos García <
Post by Carlos García
I think --skip-bad-hosts would do the work.
fab DoTheUpdates --skip-bad-hosts -H <comma-separated list of hosts>
Regards
Post by Paul Hoffman
Greetings. I want a script to do an update to all of the hosts that are
alive, but not spend time on ones that aren't. I tried the following as a
@hosts("***@host1", "***@host2")
PickAttempt = run("echo")
(env.hosts).append(env.host_string)
print("Adding {}".format(env.host_string))
PickHosts()
def DoTheUpdates(): ...
How can I get the above code to work? Or if this is too much of a
kludge, what is the proper way to have DoTheUpdates try a bunch of hosts
but gracefully stop each time it can't log into one?
--Paul Hoffman
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Carlos García
2015-09-17 06:44:09 UTC
Permalink
Hi,

I think your example would work if your task is called this way (link
<http://docs.fabfile.org/en/1.10/usage/execution.html#globally-via-env>):

fab PickHosts DoTheUpdates


A faster approach that also avoids the concatention of tasks, could be
this, if you don't mind to have the host list hardcoded:

# Fabric modules
from fabric.api import env,run,local,hosts, task, settings, hide, parallel


@parallel
def PickHosts():
host_list = ['host1', 'host2']
hosts_alive = []
with (settings(hide('everything'), warn_only = True)):
for host in host_list:
PickAttempt = local('ping -c 1 {host}'.format(host = host), capture = True)
if PickAttempt.succeeded:
hosts_alive.append(host)
return hosts_alive

@task
@hosts(PickHosts())
def DoTheUpdates():
run('echo')

Be aware that PickHost will be executed every time you run fab, as it
executes PickHost() when importing. Anyway, there are workarounds; append
this:

if __name__ == '__main__':

def PickHosts():
return []


If you prefer to pass the host/roles list via command-line, maybe the best
approach is to use execute
<http://docs.fabfile.org/en/1.10/api/core/tasks.html?highlight=execute#fabric.tasks.execute>,
creating the host list prior to the execute() call. I haven't tested,
though.


Regards
Post by Paul Hoffman
--hide hides *all* warnings, not just host-unreachable warnings, correct?
That is certainly not what I want.
To recap: I want to do an initial silent check for the host list to see
which are not up, and then only run the main procedures on the hosts known
to be up. Is this possible with Fabric?
On Wed, Sep 16, 2015 at 9:33 AM, Carlos García <
Post by Carlos García
Hi Paul,
if you want to hide this kind of output, you can use --hide
fab DoTheUpdates --skip-bad-hosts --hide=warnings -H <comma-separated
list of hosts>
Check this out if you want to hide other kinds of output messages [
http://docs.fabfile.org/en/1.10/usage/output_controls.html]
Regards
Post by Paul Hoffman
--skip-bad-hosts (or env.skip_bad_hosts) doesn't seem to work for this.
Warning: Timed out trying to connect to host2 (tried 1 time)
timed out
That doesn't seem like "skipping". What else do I need to do here to
make bad hosts skipped silently?
On Wed, Sep 16, 2015 at 12:52 AM, Carlos García <
Post by Carlos García
I think --skip-bad-hosts would do the work.
fab DoTheUpdates --skip-bad-hosts -H <comma-separated list of hosts>
Regards
Post by Paul Hoffman
Greetings. I want a script to do an update to all of the hosts that
are alive, but not spend time on ones that aren't. I tried the following as
@hosts("***@host1", "***@host2")
PickAttempt = run("echo")
(env.hosts).append(env.host_string)
print("Adding {}".format(env.host_string))
PickHosts()
def DoTheUpdates(): ...
How can I get the above code to work? Or if this is too much of a
kludge, what is the proper way to have DoTheUpdates try a bunch of hosts
but gracefully stop each time it can't log into one?
--Paul Hoffman
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Loading...