0

I'd like to run a Fabric command that would look something like this:

fab show_users:web01

where show_users would be defined like this:

def show_users(webserver):
  if webserver == 'web01':
    DB_NAME = 'db01'
  elif webserver == 'web02':
    DB_NAME = 'db02'
  elif webserver == 'web03':
    DB_NAME = 'db03'
  else:
    print "Error: invalid webserver."

  cmd = "psql -h {0} -U <role> -d <database> -c 'SELECT id, username FROM auth_user;'".format(DB_NAME)
  run(cmd)

When I run the command, I get this error:

No hosts found. Please specify (single) host string for connection: web01.

If I enter 'web01', which is defined in my /etc/hosts file, the command will then work. What is the correct way to do this?

Jim
  • 13,430
  • 26
  • 104
  • 155

1 Answers1

0

I figured it out based on this SO question:

# usage: fab show_users:[dev|stage|prod]
def show_users(tier):
    env.tier = tier
    if env.tier=="dev":
        env.host_string = 'web01'
        DB_HOST = 'db01'
    elif env.tier=="stage":
        env.host_string = 'web02'
        DB_HOST = 'db02'
    elif env.tier=="prod":
        env.host_string = 'web03'
        DB_HOST = 'db03'
    else:
        print "Error: Tier %s is not a valid server tier name." % (tier)
        exit(1)

    cmd = "psql -h {0} -U {1} -d {2} -c 'SELECT id, username FROM auth_user;'".format(DB_HOST, env.me, env.db)
    run(cmd)
Jim
  • 13,430
  • 26
  • 104
  • 155