Saltstack load pillar in a for loop

谁说胖子不能爱 提交于 2019-12-07 12:19:57

问题


I am developing a automatic proftd installation whit Salt, i wont to get the ftp users from a template but I cant get work the pillar, i initialized the pillar whit the users data and call it into a for loop, but you don't get the pillar user data in the loop.

When i make salt-call pillar.get ftpusers in the minion, the response is:

local:

This is my pillar ftpusers.sls:

ftp-server.ftpusers:
  user:
    - user: user
    - passhash: j2k3hk134123l1234ljh!"·$ser
    - uuid: 1001
    - guid: 1001
    - home: /srv/ftp/user
    - shel: /bin/false

And this is the for loop:

{% for users in pillar.get('ftpusers', {}).items() %}

  /srv/herma-ftp/.ftpusers:
    file.managed:
      - user: root
      - group: root
      - mode: 444
      - contents:'{{ user }}:{{ args['passhash'] }}:{{args['uuid'] }}:{{ args['guid'] }}::{{ args['home'] }}:{{ args['shel'] }}'
      - require:
        - file: /srv/herma-ftp

  /srv/herma-ftp/{{user}}:
    file.directory:
      - user: nobody
      - group: nobody
      - dir_mode: 775
      - makedirs: True
      - require:
        - file: /srv/herma-ftp
      - watch:
        - file: /srv/herma-ftp
    module.run:
      - name: file.set_selinux_context
      - path: {{ args['home']}}
      - type: public_content_t
      - unless:
        - stat -c %C {{ args['home'] }} |grep -q public_content_t

{% endfor %}

When I make in the minion

salt-call -l debug state.sls herma-ftp-server saltenv=My-enviroment test=True

Don't expect this for because don't can get the pillar data.


回答1:


Your loop should also look like:

{% for user, args in pillar.get('ftpusers', {}).items() %}

Also, contents argument for a file.managed doesn't support templating. What you need to do is move /srv/herma-ftp/.ftpusers state outside of the loop, and make the loop inside the file template. The final layout of your state should look like:

/srv/herma-ftp/.ftpusers
  file.managed:
    source: salt://ftpserver/dot.ftpusers
    template: jinja
    ...
    ...

{% for user, args in pillar.get('ftpusers', {}).items() %}

/srv/herma-ftp/{{user}}:
  file.managed:
    ...

{% endfor %}

And your ftpserver/dot.ftpusers would look like:

{% for user, args in pillar.get('ftpusers', {}).items() %}
{{ user }}:{{ args['passhash'] }}:{{args['uuid'] }}:{{ args['guid'] }}::{{ args['home'] }}:{{ args['shel'] }}
{% endfor %}


来源:https://stackoverflow.com/questions/27169509/saltstack-load-pillar-in-a-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!