Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run python-terraform multiple times (in loop) #34

Closed
dragan1979 opened this issue Feb 14, 2018 · 8 comments
Closed

Run python-terraform multiple times (in loop) #34

dragan1979 opened this issue Feb 14, 2018 · 8 comments

Comments

@dragan1979
Copy link

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:

`#!/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)`

@beelit94
Copy link
Owner

@dragan1979 I think this is an issue relative to your python script not about this module. More appropriate place for this question would be stackoverflow. But I would love to help you look into it as long as you can provide

  1. well formatted python script(above is not appropriate formatted)
  2. example json data
  3. expected output on your console
  4. actual output on your console

Cheers

@dragan1979
Copy link
Author

dragan1979 commented Feb 14, 2018 via email

@dragan1979
Copy link
Author

It's on terraform side, i had to create keypair for every new user, after that issue gone away, is this only way to use same tf file for multiple instance creations ?

@beelit94
Copy link
Owner

@dragan1979 no, you actually don't need to use python loop to create multiple instances. You can simply use "count" variable in terraform
https://github.com/terraform-providers/terraform-provider-aws/tree/master/examples/count
https://www.terraform.io/docs/configuration/resources.html#using-variables-with-count

@dragan1979
Copy link
Author

i know it, but we're using json file to get username and number of instances, so python script reads that file,and for every user creates x instances, script works fine but facing issues that if changing something in tf file (username variable) instance got recreated, i created a module https://1drv.ms/f/s!AizscpxS0QM4ae6fU8I48xN-oF0 but same issue,if i run same code in different zone no issuues, but that's not a solution.
Is there any way not to destroy instance if variable value is different ?

@beelit94
Copy link
Owner

beelit94 commented Feb 15, 2018

@dragan1979
tweaking your tf file for ec2 instance to update data of instance instead of recreating is tricky. I've spent hours as well on my tf file. You can see
hashicorp/terraform#2423
hashicorp/terraform#1887
From your tf file I think if only username changed and count not changed, it shouldn't destory after the above issue fixed(terraform 0.8.8 and after)
I would suggest you try to move user_data to template file
https://www.terraform.io/docs/providers/template/d/file.html
and try again.
If still not working, you should file a bug on terraform github.

@dragan1979
Copy link
Author

dragan1979 commented Feb 15, 2018 via email

@ghost
Copy link

ghost commented Feb 19, 2018

finally got it working:turned out i had to create subfolder for each user (./terraform/user1,./terraform/user2....), copy all tf files to these folders,create new security group for every user and only that machines stopped recreating, for every user new machine has been created without destroying it for previous user

#!/bin/python
import json
import os.path
import shutil
from os import mkdir
from pprint import pprint
from python_terraform import *


json_data=open('./my.json')
data = json.load(json_data)

json_data.close()



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







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: '))
    start_path="/home/ja/terraform-course/demo-2b/"
    final_path=os.path.join(start_path,user)
    if not os.path.exists(final_path):
       os.makedirs(final_path)
    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)
    final=os.path.join(final_path,'sg.tf')
    final1=os.path.join(final_path,'windows.tf')
    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)
    myfunc()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants