运维开发网

salt pillar 常用设置

运维开发网 https://www.qedev.com 2020-10-29 12:21 出处:51CTO 作者:星辰111
1、设置pillarcat/srv/pillar/vars.sls{%ifgrains['fqdn']=='saltminion'%}webserver:httpdstate:test01{%elifgrains['fqdn']=='saltminion02'%}webserver:nginxstate:test02{%endif%}[[email protected]]#cat/srv/pi
1、设置pillar
cat /srv/pillar/vars.sls

{% if grains['fqdn'] == 'saltminion' %}
webserver: httpd
state: test01
{% elif grains['fqdn'] == 'saltminion02' %}
webserver: nginx
state: test02
{% endif %}
[[email protected] 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
[[email protected] 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 %}

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号