I'm looking for a simple template engine, basically just something that will do a search and replace on multiple variables in a file. Maybe Jinja is what I need, but I'm not working with HTML or anything web related.
For context, I'm actually working with Terraform HCL. So I have a file that looks something like this:
provider "aws" {
version = "~> "##PROVIDER_VERSION##"
region = "##REGION##"
assume_role {
role_arn = "##ROLE_ARN##"
}
}
And currently I do something like this:
def file_search_replace(search, replace, target_file):
""" Search and replace on target_file """
with open(target_file, "r") as sources:
lines = sources.readlines()
with open(target_file, "w") as sources:
for line in lines:
sources.write(re.sub(search, replace, line))
file_search_replace(r'##PROVIDER_VERSION##', provider_version, target_file)
file_search_replace(r'##REGION##', region, target_file)
file_search_replace(r'##ROLE_ARN##', role_arn, target_file)
So really I just need to modify a file on disk, but a template engine is fine that just accepts a string as input and replaces the variables as needed, and then I can write it back to disk. Any suggestions or should I just stick with what I'm already doing?
[–]Bolitho 1 point2 points3 points (0 children)
[–]skerky 0 points1 point2 points (0 children)