发生渲染异常:Jinja变量'dict object'没有属性'data'

时间:2017-09-23 14:36:05

标签: nginx jinja2 salt

在nginx.conf中更改内容时出错

/etc/salt/master.d/reactor.conf

reactor:
  - 'salt/beacon/*/inotify//etc/nginx/nginx.conf':
    - /srv/reactor/web-remediate.sls

/srv/reactor/web-remediate.sls

{% if data['data']['id'] %}
  web_state_run:
    local.state.apply:
      - tgt: {{ data['data']['id'] }}
      - arg:
        - webserver.nginx
{% endif %}

salt-master -l debug

登录

[DEBUG   ] Initializing new IPCClient for path: /var/run/salt/master/master_event_pull.ipc
[DEBUG   ] Sending event: tag = salt/beacon/minion1.vagrant.net/inotify//etc/nginx/nginx.conf; data = {'path': '/etc/nginx/nginx.conf', '_stamp': '2017-09-23T12:21:17.586647', 'id': 'minion1.vagrant.net', 'change': 'IN_MODIFY'}
[DEBUG   ] Gathering reactors for tag salt/beacon/minion1.vagrant.net/inotify//etc/nginx/nginx.conf
[DEBUG   ] Compiling reactions for tag salt/beacon/minion1.vagrant.net/inotify//etc/nginx/nginx.conf
[DEBUG   ] compile template: /srv/reactor/web-remediate.sls
[DEBUG   ] Jinja search path: ['/var/cache/salt/master/files/base']
[ERROR   ] Rendering exception occurred: Jinja variable 'dict object' has no attribute 'data'
[ERROR   ] Failed to render "/srv/reactor/web-remediate.sls": 
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/salt/utils/reactor.py", line 71, in render_reaction
    data=data)
  File "/usr/lib/python2.7/dist-packages/salt/state.py", line 329, in render_template
    **kwargs)
  File "/usr/lib/python2.7/dist-packages/salt/template.py", line 95, in compile_template
    ret = render(input_data, saltenv, sls, **render_kwargs)

  File "/usr/lib/python2.7/dist-packages/salt/renderers/jinja.py", line 70, in render
    **kws)
  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 164, in render_tmpl
    output = render_str(tmplstr, context, tmplpath)
  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 428, in render_jinja_tmpl
    buf=tmplstr)
SaltRenderError: Jinja variable 'dict object' has no attribute 'data'

1 个答案:

答案 0 :(得分:1)

我已将{reactor if data [' data'] [' id']%}修改为{%if data [&%39}条件行/srv/reactor/web-remediate.sls中的#39; id']%}现在反应堆工作正常。

我假设下面一行的数据字典包含' id'仅限密钥,所以我跳过['数据']

 [DEBUG   ] Sending event: tag = salt/beacon/minion1.vagrant.net/inotify//etc/nginx/nginx.conf; data = {'path': '/etc/nginx/nginx.conf', '_stamp': '2017-09-23T12:21:17.586647', 'id': 'minion1.vagrant.net', 'change': 'IN_MODIFY'}

修饰:

 From:

  {% if data['data']['id'] %}
   web_state_run:
     local.state.apply:
       - tgt: {{ data['data']['id'] }}
       - arg:
         - webserver.nginx
 {% endif %}


 To:

 {% if data['id'] %}
 web_state_run:
   local.state.apply:
     - tgt: {{ data['id'] }}
     - arg:
       - webserver.nginx
 {% endif %}

输出:

 Sending event: tag = salt/beacon/ranjith3.salt.az/inotify//etc/nginx            /nginx.conf; data = {'path': '/etc/nginx/nginx.conf', '_stamp': '2017-09-28T11:03:55.261016', 'id': 'ranjith3.salt.az', 'change': 'IN_IGNORED'}
 [DEBUG   ] Gathering reactors for tag salt/beacon/ranjith3.salt.az/inotify//etc/nginx/nginx.conf
 [DEBUG   ] Compiling reactions for tag salt/beacon/ranjith3.salt.az/inotify//etc/nginx/nginx.conf
 [DEBUG   ] compile template: /srv/reactor/web-remediate.sls
 [DEBUG   ] Jinja search path: ['/var/cache/salt/master/files/base']
 [PROFILE ] Time (in seconds) to render '/srv/reactor/web-remediate.sls' using 'jinja' renderer: 0.00302600860596
 [DEBUG   ] Rendered data from file: /srv/reactor/web-remediate.sls:

 web_state_run:
   local.state.apply:
     - tgt: ranjith3.salt.az
     - arg:
       - webserver.nginx