0

Python newbie here. Lets say I have this:

def test_servers():
        env.user = getpass.getuser()
        env.hosts = []


And I want to do something like this:

def test_servers():
            env.user = getpass.getuser()
            system = raw_input("Enter FQDN to show (seperated by commas for multiple entries)> ")
            env.hosts = system.split(',')

??How do I make it populate as:

env.hosts = ['firsthostinput', 'secondhostinput']

I'm still learning python and I'm not sure if system split can be combined with something else to do the format of what I want it to populate as. Any help would be greatly appreciated.

EDIT:

This is for my fab file. Just doing the system.split doesn't work. And because of a specific parameter "audit" I can't just pass hosts on the command line. They must be in input_servers().

So, this is what I have in my fabfile.py:

def input_servers():
    env.user = getpass.getuser()
    system = raw_input("Enter FQDN. User Commas to seperate multiple servers > "
    env.hosts = system.split(',')

This is what happens if I try to run it:

    [me@mothership fab_files]$ fab input_servers audit
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/fabric/main.py", line 654, in main
    docstring, callables, default = load_fabfile(fabfile)
  File "/usr/lib/python2.6/site-packages/fabric/main.py", line 165, in load_fabfile
    imported = importer(os.path.splitext(fabfile)[0])
  File "/home/me/my-repo/stuff/fab_files/fabfile.py", line 17
    env.hosts = system.split(',')
      ^
SyntaxError: invalid syntax
Kryten
  • 595
  • 3
  • 10
  • 25
  • What is your exact problem? `.split(',')` works on all strings, and `raw_input()` returns a string. – Martijn Pieters Apr 30 '13 at 17:17
  • Is there actually something wrong with your code? `split` did just what you wanted it to do on my machine. – Dolphiniac Apr 30 '13 at 17:20
  • Looks fine to me. You might also want to strip any leading/trailing whitespace with `env.hosts = map(str.strip, system.split(','))` – Aya Apr 30 '13 at 17:21
  • The problem is that I want to populate env.hosts with a list that matches ['firsthostinput', 'secondhostinput']. So, user inputs 3 words: apple, orange, pizza. I want the result to look EXACTLY like this: env.hosts = ['apple', 'orange', 'pizza'] – Kryten Apr 30 '13 at 17:52

1 Answers1

1
>>> system  = raw_input("Enter FQDN to show (seperated by commas for multiple entries)
> ")
Enter FQDN to show (seperated by commas for multiple entries)> apple,oranges,lim
es,lemons
>>> system 
'apple,oranges,limes,lemons'
>>> splitted = system .split(',')
>>> splitted
['apple', 'oranges', 'limes', 'lemons']

not really sure what the problem here is?

TehTris
  • 3,139
  • 1
  • 21
  • 33
  • It finally worked. Someone opened my fabfile to edit and never closed which caused a temp file to be created with different info in it. Thought I was going crazy. Thanks! – Kryten May 01 '13 at 18:25