迭代 SaltStack 中的一对多键值 jinja 字典
Iterate through a one-to-many key-value jinja dictionary in SaltStack
我在 SaltStack 状态下使用 jinja 一对多键值字典:
{% 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 %}
我想要实现的是不仅遍历该字典中的键,而且还遍历分配给该键的值。例如,使用 name
中的第一个值,comment
中的第二个值,依此类推。目前,我已经设法使用 [key][0]
语句遍历键,如上所示。
我是否需要在内部使用第二个循环来遍历每个键的值?我怎样才能做到?
您是否尝试过设置项目变量?
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())
从字面上看,你可以这样做:
{%- set cronjobs = {'key1': [val1, val2], 'key2': [val1, val2]} %}
{%- for key, values in cronjobs.items() %}
{{ values[0] }}:
cron.present:
- name: /home/vplagov/scripts/{{ values[0] }}
- comment: {{ values[1] }}
# fixed values here...
{%- endfor %}
但是如果你的例子接近你的实际用例,我认为你只需要一个扁平的字典。看看这是否更符合您的需求:
{%- load_yaml as cronjobs %}
bash-history-backup.sh: History backup
delete-trash-files.sh: Delete trash files
keepass-backup.sh: Backup keepass
# ...more here...
{%- endload %}
{%- for script, comment in cronjobs.items() %}
cronjob-{{ script }}:
cron.present:
- name: /home/vplagov/scripts/{{ script }}
- comment: {{ comment }}
- minute: 20
# etc
{%- endfor %}
我在 SaltStack 状态下使用 jinja 一对多键值字典:
{% 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 %}
我想要实现的是不仅遍历该字典中的键,而且还遍历分配给该键的值。例如,使用 name
中的第一个值,comment
中的第二个值,依此类推。目前,我已经设法使用 [key][0]
语句遍历键,如上所示。
我是否需要在内部使用第二个循环来遍历每个键的值?我怎样才能做到?
您是否尝试过设置项目变量?
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())
从字面上看,你可以这样做:
{%- set cronjobs = {'key1': [val1, val2], 'key2': [val1, val2]} %}
{%- for key, values in cronjobs.items() %}
{{ values[0] }}:
cron.present:
- name: /home/vplagov/scripts/{{ values[0] }}
- comment: {{ values[1] }}
# fixed values here...
{%- endfor %}
但是如果你的例子接近你的实际用例,我认为你只需要一个扁平的字典。看看这是否更符合您的需求:
{%- load_yaml as cronjobs %}
bash-history-backup.sh: History backup
delete-trash-files.sh: Delete trash files
keepass-backup.sh: Backup keepass
# ...more here...
{%- endload %}
{%- for script, comment in cronjobs.items() %}
cronjob-{{ script }}:
cron.present:
- name: /home/vplagov/scripts/{{ script }}
- comment: {{ comment }}
- minute: 20
# etc
{%- endfor %}