convert python dictionary to tfvars(hcl lang) formatted string
Project description
Pytfvars
Convert python dictionary object to hcl syntax string.
This is useful for manipulate with tfvars values with python.
Use python-hcl2
to load tfvars file and do something in your code then use this package to convert dictionary object to generate hcl syntax string.
You can do write result string to tfvars file to trigger terraform plan.
Document
Install
python3 -m pip install pytfvars -U
OR
pip3 install pytfvars
Usage
import hcl2
from pytfvars import tfvars
# use hcl2 to load tfvars file(or string)
def load(file_path: str):
with open(file_path, 'r') as fp:
dict_tf_values = hcl2.load(fp)
return dict_tf_values
# convert dictionary to tfvars string
def convert(dict_tf_values: dict):
return tfvars.convert(dict_tf_values)
# do some business logic
def do_something(dict_tf_values: dict):
del dict_tf_values["key_to_delete"]
dict_tf_values["add_something"] = "do_something"
return dict_tf_values
if __name__ == '__main__':
file_path = 'YOUR_FILE_PATH/sample.tfvars'
# read file
contents = load(file_path)
# business logic
contents_mod = do_something(contents)
# convert
contents_mod_str = convert(contents_mod)
# write to tfvars file
w_file_path = 'YOUR_FILE_PATH_TO/result.tfvars'
with open(w_file_path, 'w') as fp:
fp.write(contents_mod_str)
Core Concept
I wanted to make a web page which get input form and generate terraform resource with the values.
I found some packages which load values from hcl file but none of this which do write hcl(tfvars) with python.
Release
v1.0.0
initial release
v1.0.1
fix README
v1.0.2
add indent to result string
Contribute
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytfvars-1.0.3.tar.gz
.
File metadata
- Download URL: pytfvars-1.0.3.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a28a7a0cd4cd82dcd6cb62fafba8eab76a897e6f13e0f45d6b62e1702786e07f |
|
MD5 | 4155c8b3ceaf3791a6f7efe5a49b6143 |
|
BLAKE2b-256 | e5d84ec8eff28b7c8ce185fd204db36076e313aa955bbb43ccdf62705371f513 |
File details
Details for the file pytfvars-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: pytfvars-1.0.3-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0ed79baa3b04a82fcce7d67a38475669ff33f92611a0128f471116235eeb4cf |
|
MD5 | bcacdf8c8959f3e6111bdd3f3e76cda8 |
|
BLAKE2b-256 | 8b3ce9d2b2dd856b9545cfe58c94211661fac3ebc007d10e3fa55ca09ea907da |