Solved after few days:
JSON:
JSON:
Output: {
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"customers": [
{
"name": "Molecule Man",
"age": 29,
"email": "[email protected]",
"instances": 1,
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut",
"age": 39,
"email": "[email protected]",
"instances": 1,
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name": "Eternal Flame",
"age": 1000000,
"email": "[email protected]",
"instances": 1,
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}
script:#!/bin/python import sys import json import os.path import shutil from os import mkdir from pprint import pprint from python_terraform import * #open JSON file json_data=open('./my.json') data = json.load(json_data) json_data.close() #Function which will create instances,parameters are username and count of instances fetched from JSON file def myfunc(): tf = Terraform(working_dir=final_path, variables={'count':count,'INSTANCE_USERNAME':user}) tf.plan(no_color=IsFlagged, refresh=True, capture_output=False) approve = {"auto-approve": True} print(tf.init(reconfigure=True)) print(tf.plan()) print(tf.apply(**approve)) return # sweep through JSON file and store username and number of instances into user and count variables 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] #print(user) count=data['customers'][i]['instances'] #print(count) #enter = int(input('Enter number of instances: ')) #define "root" directory start_path="/home/ja/terraform-course/demo-2b/" #in order to avoid instance recreation,folder for each user needs to be created #define subdirectories named by user and create it it folder doesn't exist final_path=os.path.join(start_path,user) if not os.path.exists(final_path): os.makedirs(final_path) #copy terraform files to each newly created folder for user shutil.copy2('./vars.tf', final_path) shutil.copy2('./sg.tf', final_path) shutil.copy2('./windows.tf', final_path) shutil.copy2('./provider.tf', final_path) shutil.copy2('./test.txt', final_path) shutil.copy2('./output.tf', final_path) #for each user new security group needs to be created.Name of SG will be username final=os.path.join(final_path,'sg.tf') final1=os.path.join(final_path,'windows.tf') #replace current name (allow-all) with variable username in sg.tf and windows.tf files with open(final, 'r') as file : filedata = file.read() filedata = filedata.replace('allow-all', user) with open(final, 'w') as file: file.write(filedata) with open(final1, 'r') as file : filedata = file.read() filedata = filedata.replace('allow-all', user) with open(final1, 'w') as file: file.write(filedata) #call function for running terraform myfunc() #in each user folder open terraform.tfstate file and extract public IP to variable ip final2=os.path.join(final_path,'terraform.tfstate') json_data=open(final2) data1 = json.load(json_data) json_data.close() #write Public IP,username and password to /home/ja/terraform-course/demo-2b/<username>.txt file filename="/home/ja/terraform-course/demo-2b/"+user+".txt" print(filename) for i in range (0, len (data1['modules'])): ip=','.join(data1['modules'][i]['outputs']['id']['value']) sys.stdout = open(filename,'wt') print("Username is:"+" "+ user+".Password is Passw0rd01234.IP addrress is:"+ip)