首页 > 解决方案 > 此上下文盐堆栈中不允许映射值

问题描述

无法在盐堆栈中的 for 循环下分配变量

{% set prli = ['httpd','rhnsd'] %}
{% for usr in prli %}
{% set pro = salt['cmd.shell'](' ps -eaf | grep -i {{ usr }} | wc -l') %}
{{ usr }}:
  cmd.run:
    - name: echo "{{ usr }} {{ pro }}"
{% endfor %}

~

出错

数据编译失败:

Rendering SLS 'failed: mapping values are not allowed in this context

标签: yamljinja2salt-stack

解决方案


{% set prli = ['httpd','rhnsd'] %}
{% for usr in prli %}
{% set pro = salt['cmd.shell']('ps -eaf | grep -i' ~ usr ~ '| wc -l') %}
{{ usr }}:
  cmd.run:
    - name: echo "{{ usr }} {{ pro }}"
{% endfor %}

您不能{{ }}在 jinja 语句中使用。


推荐阅读