我无法完成以下工作
柱:
rabbitmq: version: '3.5.6' users: tmpchq_user: password: RvdeXurQ vhosts: tmpchq: - user: tmpchq_user permission: '".*" ".*" ".*"' - user: default_user permissions: '".*" ".*" ".*"' default: - user: temp permission: '".*" ".*" ".*"'
状态:
{% for vhost in salt['pillar.get']('rabbitmq:vhosts') %} {% for items in salt['pillar.get']('rabbitmq:vhosts:{{ vhost }}') %} config_rabbitmq: cmd.run: - name: | rabbitmqctl add_vhost {{ vhost }} user {{ items['user'] }} permissions {{ items['permissions'] }} - cwd: / - shell: /bin/bash {% endfor %} {% endfor %}
现在,如果我在python中解决这个问题,它可以正常工作:
>>> x = {'rabbitmq': {'users': {'tmpchq_user': {'password': 'RvdeXurQ'}}, ... 'version': '3.5.6', ... 'vhosts': {'default': [{'permission': '".*" ".*" ".*"', ... 'user': 'temp'}], ... 'tmpchq': [{'permission': '".*" ".*" ".*"', ... 'user': 'tmpchq_user'}, ... {'permission': '".*" ".*" ".*"', ... 'user': 'default_user'}]}}} >>> for vhost in x['rabbitmq']['vhosts']: ... for items in x['rabbitmq']['vhosts'][vhost]: ... print(vhost, items['user'], items['permission']) ... ... ... default temp ".*" ".*" ".*" tmpchq tmpchq_user ".*" ".*" ".*" tmpchq default_user ".*" ".*" ".*"
时髦的部分是我不会因为盐而失败或成功。我什么都没得到:
[root@salt-master srv]# salt 'client01p' state.sls rabbitmq.install_exp client01p: Summary for client01p ----------- Succeeded: 0 Failed: 0 ----------- Total states run: 0 Total run time: 0.000 ms
任何想法?还要忽略这个命令只使用组合指令,我只是试图让嵌套工作,这只是一个虚构的例子。
答案 0 :(得分:0)
{% for items in salt['pillar.get']('rabbitmq:vhosts:{{ vhost }}') %}
这是错误的。您在{{ }}
块内不需要{% %}
。请改用jinja的~
字符串连接运算符:
{% for items in salt['pillar.get']('rabbitmq:vhosts:'~vhost) %}
此外,您应该知道,如果支柱中的'rabbitmq:vhosts:'~vhost
为空,您将收到错误,因为您无法迭代None
。
另外,我会像这样编写你的嵌套for循环:
{% for vhost, items in salt['pillar.get']('rabbitmq:vhosts', {}).items() %}
{% for item in items %}