我正在尝试将一行回显到 sudoers 文件中,但我无法弄清楚该特定命令所需的正确语法。
gwconfig:
cmd.run:
{% if grains['ip4_gw'] == '192.168.1.1' %}
- name: echo "%account_name ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
{% elif grains['ip4_gw'] == '192.168.1.2' %}
- name: echo "not working" >> /tmp/gwtest.txt
{% endif %}
这失败了
渲染 SLS‘base:linux.oshardening’失败:此处不允许映射值;第 12 行
我可以手动运行此命令并且它运行良好。
如果我用其他类似的内容替换 sudoers 行,那么它也能正常工作:
gwconfig:
cmd.run:
{% if grains['ip4_gw'] == '192.168.1.1' %}
- name: echo "working" >> /tmp/gwtest.txt
{% elif grains['ip4_gw'] == '192.168.1.2' %}
- name: echo "not working" >> /tmp/gwtest.txt
{% endif %}
vmname:
----------
gwconfig:
----------
__env__:
base
__sls__:
linux.test
cmd:
|_
----------
name:
echo "working" >> /tmp/gwtest.txt
- run
|_
----------
order:
10000
答案1
将其作为多行值运行似乎有效:
gwconfig:
cmd.run:
{% if grains['ip4_gw'] == '192.168.1.1' %}
- name: |
echo "%account_name ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
{% elif grains['ip4_gw'] == '192.168.1.2' %}
- name: echo "not working" >> /tmp/gwtest.txt
{% endif %}