3422饿111111 发表于 2017-2-22 10:45:22

saltstack pillar

1、关于Pillar
Pillar:
存储位置:Master端

数据类型:动态数据
数据采集更新方式:在Master端定义,指定给对应的Minion,可以用saltutil.refresh_pillar刷新
应用:存储Master指定的数据,只有指定的Minion可以看到。用于敏感数据保存


Pillar的使用Grains不同,需要手动设置。配置方法与State管理。需要先在master配置文件中定义pillar_roots.

同时,默认情况下,master配置文件中的所有数据都添加到Pillar中,且对所有minion可用。如果要禁用这一默认值,可以在master配置文件中添加如下数据,重启服务后生效:

2、配置pillar
a)定义pillar_roots:


1
2
3
4
5
# vim /etc/salt/master
pillar_roots:
base:
    - /etc/salt/pillar
# mkdir -p /etc/salt/pillar




b)定义top.sls:


1
2
3
4
# vim /etc/salt/pillar/top.sls
base:
'*':
    - init.rsyslog




备注:所有的minion端都匹配init目录下的rsyslog.sls文件

默认情况下,master配置文件中的所有数据都添加到Pillar中,且对所有minion可用。
c)编辑Pillar下的SLS文件:


1
2
3
4
5
6
7
8
# mkdir -p /etc/salt/pillar/init
# cd /etc/salt/pillar/init
# vim rsyslog.sls
{% if grains['osfinger'] == 'CentOS-6' %}
syslog: rsyslog
{% elif grains['osfinger'] == 'CentOS-5' %}
syslog: syslog
{% endif %}




说明:这个是使用Jiajia模板定义的Pillar。同时使用了Grains来进行条件判断。

注意:key与value之间需要用冒号加空格分隔,没有空格的话会出现报错
d)刷新Pillar数据:

在master上修改pillar文件后,需要用以下命令刷新minion上的数据:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# salt '*' saltutil.refresh_pillar
node01.saltstack.com:
    True
node02.saltstack.com:
    True
# salt '*' pillar.item syslog
node01.saltstack.com:
    ----------
    syslog:
      rsyslog
node02.saltstack.com:
    ----------
    syslog:
      rsyslog
# salt '*' pillar.raw
node01.saltstack.com:
    ----------
    syslog:
      rsyslog
node02.saltstack.com:
    ----------
    syslog:
      rsyslog




使用pillar.raw查看的是目前正在使用的,存在于minion端的Pillar数据,使用pillar.items会刷新后重新加载的Pillar。所以你如果添加了Pillar,可以使用该方法查询。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# salt -I 'syslog:rsyslog' test.ping
node01.saltstack.com:
    True
node02.saltstack.com:
    True
# salt -I 'syslog:rsyslog' cmd.run 'df -h'
node02.saltstack.com:
    Filesystem      SizeUsed Avail Use% Mounted on
    /dev/sda5      14G8.4G4.5G66% /
    tmpfs         932M   12K932M   1% /dev/shm
    /dev/sda1       190M   42M139M23% /boot
    /dev/sda3       2.0G   18M1.8G   1% /tmp
node01.saltstack.com:
    Filesystem      SizeUsed Avail Use% Mounted on
    /dev/sda5      14G8.4G4.5G66% /
    tmpfs         932M   84K932M   1% /dev/shm
    /dev/sda1       190M   42M139M23% /boot
    /dev/sda3       2.0G   18M1.8G   1% /tmp




案例2:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# cd /etc/salt/pillar/
# mkdir -p packages
# cd packages/
# vim packages.sls
{% if grains['os'] == 'CentOS' %}
apache: httpd
git: git
{% elif grains['os'] == 'Debian' %}
apache: apache2
git: git-core
{% endif %}
# salt '*' grains.get os
node01.saltstack.com:
    CentOS
node02.saltstack.com:
    CentOS
# cd ..
# ls
initpackagestop.sls
# vim top.sls
base:
'*':
    - init.rsyslog
    - packages.packages




说明:salt使用-I选项表示使用Pillar来匹配minion.


1
2
3
4
5
6
7
8
9
10
# salt -I 'apache:httpd' test.ping
node01.saltstack.com:
    True
node02.saltstack.com:
    True
# salt -I 'git:git' test.ping
node02.saltstack.com:
    True
node01.saltstack.com:
    True






页: [1]
查看完整版本: saltstack pillar