Skip to content

Construct variable from another variables #66556

Closed as not planned
Closed as not planned

Description

@AGirin
{% 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...

Activity

  1. added
    documentation
    on May 18, 2024
  2. bdrx312 commented on May 18, 2024

    @bdrx312
    Contributor

    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.

  3. AGirin commented on May 20, 2024

    @AGirin
    Author

    Thanks! It works.

  4. dwoz commented on Oct 28, 2026

    @dwoz
    Contributor

    Closing — consolidated into #70585. The checkbox for this item is in the appropriate theme section of that tracker. Please pick up the sub-item there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions