Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct variable from another variables #66556

Open
AGirin opened this issue May 18, 2024 · 2 comments
Open

Construct variable from another variables #66556

AGirin opened this issue May 18, 2024 · 2 comments
Labels
Documentation Relates to Salt documentation needs-triage

Comments

@AGirin
Copy link

AGirin commented May 18, 2024

{% load_yaml as yaml %}

test:
  dev: 'xxxxx'
test_2:
  dev: 'yyyyy'

{% endload %}

{% set proj = slspath.split('/')[0] %}
{% set env = slspath.split('/')[1] %}
{% set service = slspath.split('/')[2] %}

{% set a = yaml ~ '.' ~ proj ~ '.' ~ env %}

test:
  cmd.run:
    - name: echo {{ a }}

Output:

test:
  cmd.run:
    - name: echo {'test': {'dev': 'xxxxx'}, 'test_2': {'dev': 'yyyyy'}}.test.dev    <======================

Is it possible to access test yaml from dynamically created variable?

As I really want to return yaml.test.dev value no entire yaml + test.dev...

@AGirin AGirin added Documentation Relates to Salt documentation needs-triage labels May 18, 2024
@bdrx312
Copy link
Contributor

bdrx312 commented May 18, 2024

yaml in your example is a dictionary/hashmap. You just access the value in a dictionary like you would any value in jinja.
So your code should be

{%- set a = yaml[proj][env] %}

All of your example is just plain jinja (https://jinja.palletsprojects.com/en/3.0.x/templates/#variables) and really has nothing to with salt specifically except that it uses jinja for the default renderer.

@AGirin
Copy link
Author

AGirin commented May 20, 2024

Thanks! It works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Relates to Salt documentation needs-triage
Projects
None yet
Development

No branches or pull requests

2 participants