salt pillar 常用设置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了salt pillar 常用设置相关的知识,希望对你有一定的参考价值。
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 %}
以上是关于salt pillar 常用设置的主要内容,如果未能解决你的问题,请参考以下文章