Portable variable entries in the YAML file

I have a specific variable structure that I want to get from the repository to the yaml file on my hosts.

Let's assume this structure:

secrets: psp1: username: this password: that secret_key: 123 ... 

I need something like a โ€œgenericโ€ pattern to output any โ€œsecretsโ€ at the moment, as the content changes almost entirely based on the current environment.

The easiest solution I can think of is to display the entire structure in the template as follows:

 # config/secrets.yml {{ secrets | to_yaml }} 

But the jinja2 to_yaml filter only performs โ€œyamlifyโ€ at the first level, deeper attachments are output in json.

Can I somehow work around this problem? Is there an easier way to achieve what I want?

Thanks for any help!

+11
source share
1 answer
  1. As jwodder said, it really is.
  2. If you use to_yaml (instead of to_nice_yaml ), you have a rather old installation of ansible, it's time to upgrade.
  3. Use to_nice_yaml
  4. You can pass your own kwargs to the filter functions, which usually pass them to a call to the main python module. Like this one for your case. So something like:

     {{ secrets | to_nice_yaml( width=50, explicit_start=True, explicit_end=True) }} 

    The only problem is that you cannot override indent=4, allow_unicode=True, default_flow_style=False

Note that indent can now be redefined at least starting with Ansible 2.2.0 (I use it to indent 2 spaces to follow the coding standards for one project).

+18
source

Source: https://habr.com/ru/post/983592/


All Articles