-1

I have this yml files inside an ansible project with templating and vars :

custom_values:
      postgresql:
        postgresqlDatabase: "{{ secrets.db_name }}"
        postgresqlPassword: "{{ secrets.postgres_password }}"

I search a solution to generate the same yml file without the templating like :

custom_values:
      postgresql:
        postgresqlDatabase: "mydatabase"
        postgresqlPassword: "mypassword"

Do you know an existing software to do that automatically ?

DevOpsAddict
  • 61
  • 2
  • 6

2 Answers2

0

You already have a set of ansible templates, that are rendered by a render engine like Jinja2. The easiest way to convert them would be to actually use the render engine to render the templates, supplying the correct values to it. You'll end up with a bunch of templates that have the {{ something }} blocks, replaced with the values you want.

Since this looks to be simple Jinja2 templating, please refer to: https://jinja.palletsprojects.com/en/2.10.x/

You'll end up with something like this:

>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')

Please also refer to this stackoverflow post: How to load jinja template directly from filesystem

That explains how to load templates from files

J. Meijers
  • 949
  • 8
  • 14
0

This python code is OK for me :

#import necessary functions from Jinja2 module
from jinja2 import Environment, FileSystemLoader

#Import YAML module
import yaml

#Load data from YAML into Python dictionary
config_data = yaml.load(open('./my_vars.txt'))

#Load Jinja2 template
env = Environment(loader = FileSystemLoader('./templates'), trim_blocks=True, lstrip_blocks=True)
template = env.get_template('my_template.yml')

#Render the template with data and print the output
print(template.render(config_data))

thanks :)

DevOpsAddict
  • 61
  • 2
  • 6