Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:41:22+00:00 2026-06-11T14:41:22+00:00

When I run this fabfile.py … from fabric.api import env, run, local, cd def

  • 0

When I run this fabfile.py…

from fabric.api import env, run, local, cd

def setenv(foo):
  env.hosts = ['myhost']

def mycmd(foo):
  setenv(foo)
  print(env.hosts)
  run('ls')

with this command fab mycmd:bar. I get this output…

['myhost']
No hosts found. Please specify (single) host string for connection:

What, what?! I don’t get it? I’ve set the env.hosts and it seems to be valid “inside” the mycmd function, but for some reason that run command doesn’t know about the hosts I’ve specified.

Color me confused. Any help would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:41:23+00:00Added an answer on June 11, 2026 at 2:41 pm

    @Chris, the reason you’re seeing this behavior is because the host list is constructed before the task function is called. So, even though you’re changing env.hosts inside the function, it is too late for it to have any effect.

    Whereas the command fab setenv:foo mycmd:bar, would have resulted in something you would have expected:

    $ fab setenv:foo mycmd:bar
    [myhost] Executing task 'mycmd'
    ['myhost']
    [myhost] run: ls
    

    This is the same as the accepted answer, but because of the way setenv is defined, an argument is needed.

    Another example:

    from fabric.api import env, run, local, cd
    
    env.hosts = ['other_host']
    
    def setenv(foo):
        env.hosts = ['myhost']
    
    def mycmd(foo):
        setenv(foo)
        print('env.hosts inside mycmd: %s' % env.hosts)
        run('ls')
    

    The output of this is:

    $ fab mycmd:bar
    [other_host] Executing task 'mycmd'
    env.hosts inside mycmd: ['myhost']
    [other_host] run: ls
    
    Fatal error: Name lookup failed for other_host
    
    Underlying exception:
        (8, 'nodename nor servname provided, or not known')
    Aborting.
    

    As you can see, the host-list is already set to ['other_host', ] when fabric starts to execute mycmd.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following fabfile.py: from fabric.api import env, run host1 = '192.168.200.181' host2
I run this db2 IMPORT FROM C:\my.csv OF DEL MODIFIED BY COLDEL, LOBSINFILE DATEFORMAT=\D/MM/YYYY\
i run this sql in mysql 4.0.27: select * from table where contains(name,foo); report
I run this command : select * from LIST where JCODE = 8 and
I run this query: SHOW INDEX FROM `tbl_clients` I get back these: Table, Non_unique,
i run this script from the command line: check_databse_exist=`mysql -u root --password=root -Bse 'show
I'm trying to run this on OSX 10.7: /** * simple.d */ import std.stdio;
I am trying to run this query on my SQLite database: SELECT * FROM
I run this query $result1 = mysql_query(SELECT * FROM 'departments'); while($row1 = mysql_fetch_assoc($result1)) {
I run this query: SELECT u.user_id, u.fname, u.lname, n.title, n.news_id, n.post, n.zip, z.city,z.state_abbr FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.