尝试在SLS中使用Jinja-没有运气

时间:2019-03-01 10:09:57

标签: jinja2 salt-stack

我希望您能帮助我找出问题所在...

我的sls文件:

cat /srv/salt/test.sls
{% set serverproxy = ['194.110.220.91'] %}
/tmp/motd.xml:
  file.managed:
    - source: salt://motd.jinja
    - template: jinja

我的Jinja文件:

cat /srv/salt/motd.jinja

Welcome to {{ grains['fqdn'] }}


    Server Stats at a Glance:
    ------------------------


    OS: {{ grains['osfullname'] }}
    Kernel: {{ grains['kernelrelease'] }}
    Memory: {{ grains['mem_total'] }} MB
    Proxy IP: {{ serverproxy }}

还有错误...

salt 'bbg-proxy' state.apply test
bbg-proxy:
----------
          ID: /tmp/motd.xml
    Function: file.managed
      Result: False
     Comment: Unable to manage file: Jinja variable 'serverproxy' is undefined
     Started: 10:47:04.060399
    Duration: 64.421 ms
     Changes:

Salt Version:
           Salt: 2018.3.3

如果我不使用{% set serverproxy = ['194.110.220.91'] %},一切正常。 我想念的是什么?

TIA!

1 个答案:

答案 0 :(得分:0)

我认为您需要在文件托管命令中指定上下文

作为一个例子,这是我在使用模板时拥有的东西:

disable akonadi for {{ user }}:
  file.managed:
    - name: "/home/{{ user }}/.config/akonadi/akonadiserverrc"
    - source: salt://users/files/akonadiserverrc.jinja
    - template: jinja
    - user: {{ user }}
    - group: {{ user }}
    - replace: True
    - dir_mode: 755
    - mode: 644
    - makedirs: True
    - clean: True
    - context:
      user: {{ user }}

在akonadiserverrc.jinja中,我有这个:

[%General]
Driver=QMYSQL

[QMYSQL]
Name=akonadi
Host=
Options="UNIX_SOCKET=/home/{{ user }}/.local/share/akonadi/socket-add3dev01/mysql.socket"
ServerPath=/usr/libexec/mysqld
StartServer=false

[Debug]
Tracer=null

jinja文件中的{{用户}}得到正确替换。

Salt doc