I'm using a jinja one-to-many key-value dictionary in a SaltStack state:
{% for key in {'bash history': ['bash_history-backup.sh', 'History backup'],
'empty trash': ['delete-trash-files.sh', 'Delete trash files'],
'keepass backup': ['keepass-backup.sh', 'Backup KeePass'],
'db backup': ['mysql-backup.sh', 'Backup MySQL']} %}
{{ [key][0] }}:
cron.present:
- name: /home/vplagov/scripts/{{ [key][0] }}
- user: vplagov
- minute: 20
- hour: 1
- comment:
- require:
- git: checkout latest bash_history backup
{% endfor %}
What I want to implement is to iterate not only through keys in this dictionary but through values assigned to this key as well. For example, use first value in name
, second in comment
and so on. Currently, I've managed to iterate through keys using [key][0]
statement as shown above.
Do I need to use a second loop inside to iterate through values for each of a key? And how can I make it?
Have you tried to set an items variable?
from jinja2 import Template
tmpl = """
{% set items = {'bash history': ['bash_history-backup.sh', 'History backup'],
'empty trash': ['delete-trash-files.sh', 'Delete trash files'],
'keepass backup': ['keepass-backup.sh', 'Backup KeePass'],
'db backup': ['mysql-backup.sh', 'Backup MySQL']} %}
{% for key in items %}
{{ [key][0] }}:
cron.present:
- name: /home/vplagov/scripts/{{ items[key][0] }}
- user: vplagov
- minute: 20
- hour: 1
- comment: {{items[key][1]}}
- require:
- git: checkout latest bash_history backup
{% endfor %}
"""
template = Template(tmpl)
print(template.render())