1、设置pillar
cat /srv/pillar/vars.sls
{% if grains['fqdn'] == 'saltminion' %}
webserver: httpd
state: test01
{% elif grains['fqdn'] == 'saltminion02' %}
webserver: nginx
state: test02
{% endif %}
[root@salttest pillar]# cat /srv/pillar/top.sls
base:
'*':
- vars
2、查看pillar的值
salt '*' pillar.items
3、刷新pillar设置
salt '*' saltutil.refresh_pillar
4、根据pillar值执行命令
salt -I 'state:test02' test.ping
salt -I 'saltminion:devnginx:devnginx1' test.ping
案例1 如下 ,案例2 可参考for循环
cat /srv/pillar/top.sls # 设置top.sls 文件,
base:
'*':
- role
[root@salttest host]# cat /srv/pillar/role.sls # 配置需要top执行的文件(根据主机名)
saltminion:
devnginx:
- devnginx1
- devnginx2
devredis:
- devredis1
- devredis2
saltminion02:
devnginx:
- devnginx1
- devnginx2
devmysql:
- devmysql1
- devmysql2
cat /srv/salt/base/role/newinit7.sls # 配置调取pillar的sls文件
{% set server_name = salt['grains.get']('host') %} # 根据主机名
{% for user,aaa in pillar[ server_name ].items() %} # 根据主机名获取pillar内容,注意格式
{% set bbb = aaa %}
testfile1_{{ user }}:
file.append:
- name: /tmp/testhostpillar.txt
- text: |
Thou hadst better eat salt with the Philosophers of Greece,
than sugar with the Courtiers of Italy.
- Benjamin Franklin
- user {{ user }}
- host {{ server_name }}
- aaa {{ aaa }}
- aaa11111 {{ aaa.0 }}
- aaa2222 {{ aaa.1 }}
- bbb {{ bbb }}
- bbb1 {{ bbb.0 }}
- bbb2 {{ bbb.1 }}
{% for ccc in bbb %}
testfile1_{{ ccc }}:
file.append:
- name: /tmp/testhostpillar.txt
- text: |
Thou hadst better eat salt with the Philosophers of Greece,
- ccc {{ ccc }}
{% endfor %}
{% endfor %}