-2
ENV=raw_input("Enter Environment (QA/Prod):")
print(ENV) 
os.system('aws ec2 describe-instances --filters "Name=tag:Environment,Values=ENV" "Name=instance-state-code, Values=16" > FilteredOP')

Hi, I am quite noob to Python. Here, I cannot able to call ENV variable in os.system command. Is there anything wrong with the syntax.

Sekhar
  • 499
  • 2
  • 6
  • 15

3 Answers3

1

The value of ENV won't get magically substituted in the string like so.

Consider the following:

>>> ENV = 'debug'
>>>
>>> 'Value=ENV'
'Value=ENV'
>>>
>>> 'Value={}'.format(ENV)
'Value=debug'

So instead of hardcoding ENV as part of the string, you want to insert the value of ENV using string formatting:

os.system('...Values=.{}...'.format(ENV))

However, subprocess.check_output is a cleaner alternative you may want to check out.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
0

Read about formatting output, In yours ENV considered as string "ENV".

ENV=raw_input("Enter Environment (QA/Prod):")
print(ENV) 
os.system('aws ec2 describe-instances --filters "Name=tag:Environment,Values={}" "Name=instance-state-code, Values=16" > FilteredOP'.format(ENV))
Siva Shanmugam
  • 662
  • 9
  • 19
0

Try this

    ENV=raw_input("Enter Environment (QA/Prod):")
    print(ENV) 
    os.system('aws ec2 describe-instances --filters "Name=tag:Environment,Values=%s" "Name=instance-state-code, Values=16" > FilteredOP'%(ENV))
Raghav
  • 1