SaltStack State Files Written In Python

breakfast:
egg: scrambled
meats:
- sausage
- bacon
sides:
spam: gross
beans: baked

dinner:
egg: poached
seafood: lobster
other:
- truffle pate
- mornay sauce
sides:
spam: delicious
egg: over easy
meats:
- steak
{% set application_pillar = ['breakfast', 'dinner'] %}

{%- for app_pillar in application_pillar %}
serialize key { }:
file.serialize:
- name: /tmp/serialize_test.txt
- user: root
- group: root
- mode: 400
- formatter: yaml
- dataset_pillar: {{ app_pillar }}
- merge_if_exists: True
{% endfor %}
#!py

def _get_data():
keys = ['breakfast', 'dinner']
return keys

def run():
config = {}

for key in _get_data():
config['serialize key '.format(key)] = {
'file.serialize': [
{'name': '/tmp/serialize_test.txt'},
{'user': 'root'},
{'group': 'root'},
{'mode': '400'},
{'formatter': 'yaml'},
{'dataset_pillar': key}
]
}

return config

A few notes on the python version.

  • The function name run is required.
  • The run function must return a dictionary.
  • The shebang, must be !py

Bonus

#!jinja|py

{% set application_pillar = ['breakfast', 'dinner'] %}
def run():
config = {}

{% for key in application_pillar %}
config['serialize key { }'] = {
'file.serialize': [
{'name': '/tmp/serialize_test.txt'},
{'user': 'root'},
{'group': 'root'},
{'mode': '400'},
{'formatter': 'yaml'},
{'dataset_pillar': '{ }' }
]
}
{% endfor %}

return config

Update

Bonus Bonus

#!jinja|json

{% set application_pillar = ['breakfast', 'dinner'] %}
{
{%- for app_pillar in application_pillar %}
"serialize key { }": {
"file.serialize": [
{"name": "/tmp/serialize_test.txt"},
{"user": "root"},
{"group": "root"},
{"mode": 400},
{"formatter": "yaml"},
{"dataset_pillar": "{ }" },
{"merge_if_exists": "True"}
]
}{% if not loop.last %},{% endif %}
{%- endfor %}
}

--

--

Climber, surfer, yogi, dad who does some IT on the side to get by.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store