Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

salt stack - Embed dictionary data in YAML file into a state file

I'm looking to embed dictionary data in a YAML file into a state file as i have access to execute only state files.

Below is the dictionary data in YAML file.

---
var1:
  values:
    val1: "string1"
    val2: "string2"

Below is the state file in which i have written YAML dictionary data as below.

{%- load_yaml as var %}
var1:
  values:
    val1: "string1"
    val2: "string2"
{%- endload %}

I'm not sure if this is right, can anyone help me out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you are looking for import_yaml functionality.

YAML file var.yml with some variables:

---
var1:
  values:
    val1: "string1"
    val2: "string2"

An example.sls state file:

{% import_yaml 'path/to/var.yml' as var_data %}

show-imported-variables:
  module.run:
  - name: test.echo
  - text: {{ var_data['var1']['values'] }}

Now that the YAML file has been imported as var_data we can access the values within the dictionary.

When the state file is run:

----------
ret:
    ----------
    val1:
        string1
    val2:
        string2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...