Feb-14-2018, 03:46 PM
Hi,
i'm new to python. I created a script to iterate through JSON file to find field email address, and remove part after @. This value is username for EC2 instance which will be created using terraform.For every user number of instances will be prompted interactively. (Terraform will be invoked from python script). Everything works fine when only one email is specified in JSON,but if more than one user is specified, code will be execured only once, I need when one iteration is finishes, run again for new user (ask for number of instances), create machine then run for as many users found in JSON file
Here is my code:
i'm new to python. I created a script to iterate through JSON file to find field email address, and remove part after @. This value is username for EC2 instance which will be created using terraform.For every user number of instances will be prompted interactively. (Terraform will be invoked from python script). Everything works fine when only one email is specified in JSON,but if more than one user is specified, code will be execured only once, I need when one iteration is finishes, run again for new user (ask for number of instances), create machine then run for as many users found in JSON file
Here is my code:
#!/bin/python import json from pprint import pprint from python_terraform import * def myfunc(int): tf = Terraform(working_dir='/home/ja/terraform-course/demo-2b', variables={'count':enter,'INSTANCE_USERNAME':user}) tf.plan(no_color=IsFlagged, refresh=False, capture_output=True) approve = {"auto-approve": True} print(tf.plan()) print(tf.apply(**approve)) return json_data=open('./my.json') data = json.load(json_data) json_data.close() for i in range (0, len (data['customers'])): #print data['customers'][i]['email'] k=data['customers'][i]['email'] #print(k.split('@')[0]) user=k.split('@')[0] if (i) !=0: continue #print(user) enter = int(input('Enter number of instances: ')) myfunc(enter)