Saltstack 数据系统

作者: print("") 分类: linux 发布时间: 2017-12-27 23:07


Saltstack 数据系统

两种数据系统
Grains
Pillar
一、Grains
Grains  1. 可以用来信息收集  2. 匹配命令 3. 通过top 匹配命令
查看grains 名称列出来

[root@agent salt]# salt 'liang' grains.ls
liang:
- SSDs
- biosreleasedate
- biosversion
- cpu_flags
- cpu_model
- cpuarch
- domain
- fqdn
- fqdn_ip4
- fqdn_ip6
- gpus
- host
- hwaddr_interfaces
- id
- init
- ip4_interfaces
- ip6_interfaces
- ip_interfaces
- ipv4
- ipv6
- kernel
- kernelrelease
- locale_info
- localhost
- lsb_distrib_codename
- lsb_distrib_id
- lsb_distrib_release
- machine_id
- manufacturer
- master
- mdadm
- mem_total
- nodename
- num_cpus
- num_gpus
- os
- os_family
- osarch
- oscodename
- osfinger
- osfullname
- osmajorrelease
- osrelease
- osrelease_info
- path
- productname
- ps
- pythonexecutable
- pythonpath
- pythonversion
- saltpath
- saltversion
- saltversioninfo
- selinux
- serialnumber
- server_id
- shell
- virtual
- zmqversion

显示所有的信息

[root@agent salt]# salt 'liang' grains.items

显示单个

salt 'liang' grains.item fqdn

或者使用这个

salt 'liang' grains.get fqdn

查询所有的ip地址

[root@agent salt]# salt '*' grains.get ip_interfaces:eth0
liang:
- 192.168.57.130
- fe80::20c:29ff:fea7:7ec4
dome_1:
- 192.168.57.131
- fe80::20c:29ff:fe26:746c

只允许是Centos的系统执行命令

[root@agent salt]# salt -G os:CentOS cmd.run 'w'
liang:
16:13:39 up 21:26,  3 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     :0               12Nov17 25days 46.24s 46.24s /usr/bin/Xorg :
root     pts/0    :0.0             12Nov17  3:26m  0.28s  0.28s /bin/bash
root     pts/1    192.168.57.1     13:34    1:02m  0.34s  0.34s -bash
dome_1:
02:40:02 up 22:03,  3 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     :0               Thu05   22:03m 24.85s 24.85s /usr/bin/Xorg :
root     pts/0    :0.0             Thu06    3:26m  0.06s  0.06s /bin/bash
root     pts/1    192.168.57.1     00:01    1:01m  0.18s  0.18s –bash
G 代表的是使用grains
Os:Centos 系统为Centos
Cmd.run   系统执行命令
'w'   执行w命令

只允许memcache的主机执行命令,修改minion的配置文件

vim /etc/salt/minion
88 grains:
89   roles:
90     - webserver
91     - memcache

重启

[root@liang minion]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [确定]
Starting salt-minion daemon: [确定]
[root@liang minion]#

在master测试一下

[root@agent salt]# salt -G 'roles:memcache' cmd.run 'echo hehe'
liang:
hehe

如果觉得加入到minion的配置文件中很麻烦,可以新建一个目录默认的位置是在
/etc/salt/grains  

[root@liang minion]#
[root@liang minion]# cat /etc/salt/grains
roles: nginx
[root@liang minion]#

 
在master中测试一下

root@agent salt]# salt '*' grains.item roles
liang:
----------
roles:
- webserver
- memcache
dome_1:
----------
roles:
nginx
[root@agent salt]#

这里没有显示liang主机的nginx 就是因为名字不能冲突
那么改成

roles2: nginx

再次master测试一下

[root@agent salt]# salt '*' grains.item roles2
liang:
----------
roles2:
nginx
dome_1:
----------
roles2:
nginx
[root@agent salt]#

 
 
也可以用top.sls 来做一个命令匹配

[root@agent salt]# cat top.sls
base:
'roles2:nginx':           #如果roles2=nginx
- match: grain          # 利用 grain 去匹配
- apache                # 匹配成功就执行apache.sls
[root@agent salt]#

 
测试一下

[root@agent salt]# salt '*' state.highstate
liang:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: Package httpd is already installed.
Started: 16:51:22.730219
Duration: 1076.813 ms
Changes:
----------
ID: apache-install
Function: pkg.installed
Name: httpd-devel
Result: True
Comment: Package httpd-devel is already installed.
Started: 16:51:23.807325
Duration: 0.474 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is in the desired state
Started: 16:51:23.808331
Duration: 32.294 ms
Changes:
Summary
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
dome_1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: Package httpd is already installed.
Started: 03:17:46.193116
Duration: 1069.815 ms
Changes:
----------
ID: apache-install
Function: pkg.installed
Name: httpd-devel
Result: True
Comment: Package httpd-devel is already installed.
Started: 03:17:47.263148
Duration: 0.526 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is in the desired state
Started: 03:17:47.264431
Duration: 32.991 ms
Changes:
Summary
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
[root@agent salt]#

 
 
二、Pillar
 
默认pillar 是关闭的

[root@agent salt]# salt '*' pillar.items
liang:
----------
dome_1:
----------

Pillar 是在master端设置的

vim /etc/salt/master
552 pillar_opts: True
重启
[root@agent salt]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [确定]
Starting salt-master daemon: [确定]

测试建立独立的文件。需要把pillar_opts: True  改为false

529 pillar_roots:
530   base:
531     - /srv/pillar
mkdir /srv/pillar

建立两个文件

[root@agent ~]# cat /srv/pillar/apache.sls
{%if grains['os'] == 'CentOS' %}
apache: httpd
{% elif grains['os'] == 'Debian' %}
apache: apache2
{% endif %}
[root@agent ~]# cat /srv/pillar/top.sls
base:
'*':
- apache

测试效果

[root@agent ~]# salt '*' pillar.items
dome_1:
----------
apache:
httpd
liang:
----------
apache:
httpd
[root@agent ~]#

可以测试数据  

[root@agent ~]# salt '*' saltutil.refresh_pillar
liang:
True
dome_1:
True
[root@agent ~]# salt -I 'apache:httpd' test.ping
dome_1:
True
liang:
True 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

说点什么

avatar
  Subscribe  
提醒