I want to replace the variables in the python template file and mass-produce it in another file.

I had to add a lot of resources with terraform, so it was hard to copy and paste, so that's a memo.

First, prepare a template file. Set the place you want to make a variable to @ NAME @ @ PATH @ @ HOST @.

template.tf


resource "google_monitoring_uptime_check_config" "@NAME@" {
  display_name = "@NAME@"
  timeout      = "10s"

  http_check {
    use_ssl = true
    path    = "@PATH@"
    port    = "443"
  }

  monitored_resource {
    type = "uptime_url"

    labels = {
      project_id = var.project_id
      host       = "@HOST@"
    }
  }
}

Create the value you want to pass to the variable with csv.

source.csv


aaa_bbb_com,/,aaa.bbb.com
aaa_bbb_jp,/hoge,aaa.bbb.jp
aaa_bbb_co_jp,/hage,aaa.bbb.co.jp

Run python


# python mkfile.py source.csv template.txt

It is one of the created files

resource "google_monitoring_uptime_check_config" "aaa_bbb_co_jp" {
  display_name = "aaa_bbb_co_jp"
  timeout      = "10s"

  http_check {
    use_ssl = true
    path    = "/hage"
    port    = "443"
  }

  monitored_resource {
    type = "uptime_url"

    labels = {
      project_id = var.project_id
      host       = "aaa.bbb.co.jp"
    }
  }
}

If you want to combine the files,

cat create* > result.tf

Code executed

mkfile.py


# -*- coding: utf-8 -*-
import os, sys ,csv

def mkFile(srcFname, tmplFname):
    with open(srcFname) as c:
        reader = csv.reader(c)
        for row in reader:
            f1 = open(tmplFname, "r")
            f2 = open("create" + row[0] + ".txt", "w")
            for row2 in f1:
                temp = row2.replace("@NAME@", row[0]) \
                          .replace("@PATH@", row[1]) \
                          .replace("@HOST@", row[2]) 
                f2.write(temp)
            f1.close
            f2.close

    
if __name__ == '__main__': 
    if(len(sys.argv)!=3):
        print("usage: python mkfile.py source.csv template.tf")
    
    srcFname = sys.argv[1]
    tmplFname = sys.argv[2]

    #start func
    mkFile(srcFname, tmplFname)
    
    print("finished")

point

                temp = row2.replace("@NAME@", row[0]) \
                          .replace("@PATH@", row[1]) \
                          .replace("@HOST@", row[2]) 

Recommended Posts

I want to replace the variables in the python template file and mass-produce it in another file.
I want to get the file name, line number, and function name in Python 3.4
[Python] When you want to use all variables in another file
I want to display the progress in Python!
The file name was bad in Python and I was addicted to import
I want to randomly sample a file in Python
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
[Python] I want to know the variables in the function when an error occurs!
I want to know the features of Python and pip
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to create a pipfile and reflect it in docker
I want to connect remotely to another computer, and the nautilus command
I was addicted to confusing class variables and instance variables in Python
I set the environment variable with Docker and displayed it in Python
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I made a program to check the size of a file in Python
I want to do Dunnett's test in Python
I want to create a window in Python
To dynamically replace the next method in python
I want to write to a file with Python
The file edited with vim was readonly but I want to save it
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to make a music player and file music at the same time
I want to write an element to a file with numpy and check it.
Note that I understand the least squares algorithm. And I wrote it in Python.
I want to write in Python! (1) Code format check
I want to see the file name from DataLoader
I tried to graph the packages installed in Python
I want to easily implement a timeout in python
Template of python script to read the contents of the file
I want to handle optimization with python and cplex
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
Read the csv file and display it in the browser
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I was able to repeat it in Python: lambda
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I just want to find the 95% confidence interval for the difference in population ratios in Python
I want to write a triple loop and conditional branch in one line in python
It is easy to execute SQL with Python and output the result in Excel
Download the file in Python
Try to make it using GUI and PyQt in Python
I want to initialize if the value is empty (python)
I want to specify another version of Python with pyvenv
maya Python I want to fix the baked animation again.
I want to do something like sort uniq in Python
Change the standard output destination to a file in Python
Get the MIME type in Python and determine the file format
Display numbers and letters assigned to variables in python print
I tried to illustrate the time and time in C language
[Python] I want to use the -h option with argparse
[Python] I installed the game from pip and played it
I tried programming the chi-square test in Python and Java.
I wrote it in Go to understand the SOLID principle
I tried to implement the mail sending function in Python
I tried to enumerate the differences between java and python
I want to make the Dictionary type in the List unique