作者:刘耀
QQ:22102107
SaltStack_Grains
Grains
grains是minion第一次启动的时候采集的静态数据,可以用在salt的模块和其他组件中。其实grains在每次的minion启动(重启)的时候都会采集,即向master汇报一次的。
1 . 查看minion的全部静态变量
[root@node1 ~]# salt '*' grains.items
node2.minion:
----------
SSDs:
cpu_flags:
- fpu
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pse36
- clflush
- mmx
- fxsr
- sse
- sse2
- ht
- syscall
- nx
- lm
- up
- rep_good
- unfair_spinlock
- pni
- cx16
- popcnt
- hypervisor
- lahf_lm
- abm
cpu_model:
QEMU Virtual CPU version 1.2.0
cpuarch:
x86_64
domain:
fqdn:
localhost
fqdn_ip4:
- 127.0.0.1
fqdn_ip6:
- ::1
gpus:
|_
----------
model:
GD 5446
vendor:
unknown
host:
localhost
hwaddr_interfaces:
----------
eth0:
52:54:00:05:9e:b4
lo:
00:00:00:00:00:00
id:
node2.minion
init:
upstart
ip4_interfaces:
----------
eth0:
- 10.105.61.227
lo:
- 127.0.0.1
ip6_interfaces:
----------
eth0:
lo:
ip_interfaces:
----------
eth0:
- 10.105.61.227
lo:
- 127.0.0.1
ipv4:
- 10.105.61.227
- 127.0.0.1
ipv6:
kernel:
Linux
kernelrelease:
2.6.32-504.el6.x86_64
locale_info:
----------
defaultencoding:
UTF8
defaultlanguage:
en_US
detectedencoding:
UTF-8
localhost:
node2
lsb_distrib_codename:
Final
lsb_distrib_id:
CentOS
lsb_distrib_release:
6.6
machine_id:
9293b91f6111b402930f04c20000000f
master:
115.29.51.8
mdadm:
mem_total:
996
nodename:
node2
num_cpus:
1
num_gpus:
1
os:
CentOS
os_family:
RedHat
osarch:
x86_64
oscodename:
Final
osfinger:
CentOS-6
osfullname:
CentOS
osmajorrelease:
6
osrelease:
6.6
osrelease_info:
- 6
- 6
path:
/sbin:/usr/sbin:/bin:/usr/bin
ps:
ps -efH
pythonexecutable:
/usr/bin/python2.6
pythonpath:
- /usr/bin
- /usr/lib64/python26.zip
- /usr/lib64/python2.6
- /usr/lib64/python2.6/plat-linux2
- /usr/lib64/python2.6/lib-tk
- /usr/lib64/python2.6/lib-old
- /usr/lib64/python2.6/lib-dynload
- /usr/lib64/python2.6/site-packages
- /usr/lib/python2.6/site-packages
- /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info
pythonversion:
- 2
- 6
- 6
- final
- 0
saltpath:
/usr/lib/python2.6/site-packages/salt
saltversion:
2015.5.8
saltversioninfo:
- 2015
- 5
- 8
- 0
selinux:
----------
enabled:
False
enforced:
Disabled
server_id:
556532862
shell:
/bin/bash
virtual:
kvm
zmqversion:
3.2.5
2.显示grains的变量名称
[root@node1 ~]# salt '*' grains.ls
node2.minion:
- SSDs
- 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
- master
- mdadm
- mem_total
- nodename
- num_cpus
- num_gpus
- os
- os_family
- osarch
- oscodename
- osfinger
- osfullname
- osmajorrelease
- osrelease
- osrelease_info
- path
- ps
- pythonexecutable
- pythonpath
- pythonversion
- saltpath
- saltversion
- saltversioninfo
- selinux
- server_id
- shell
- virtual
- zmqversion
3.显示某一个变量
[root@node1 ~]# salt '*' grains.item os
node2.minion:
----------
os:
CentOS
获取的是键值对 os:centos
[root@node1 ~]# salt '*' grains.item num_cpus
node2.minion:
----------
num_cpus:
1
4.直接获取内容
[root@node1 ~]# salt '*' grains.get os
node2.minion:
CentOS
5.-G的使用
-G就是grains
[root@node1 ~]# salt -G 'os:CentOs' test.ping
node2.minion:
True
如果os的centos的 执行test.ping
6.自定义grains
在minion端修改配置文件:
加入以下内容
grains:
roles: nginx
env: prod
重启服务
在master端执行:
[root@node1 ~]# salt -G 'env:prod' test.ping
node2.minion:
True
7.基于文件的grains
在minion端
新建grains文件
touch /etc/salt/grains
写入以下内容:
centos: node2
重启服务
master端执行:
[root@node1 ~]# salt -G 'centos:node2' test.ping
node2.minion:
True
不重启minion端 刷新grains
1.修改minion配置文件
[root@node2 ~]# cat /etc/salt/grains
centos: node2
test: node2
2.master端刷新
[root@node1 ~]# salt '*' saltutil.sync_grains
node2.minion:
3.测试
[root@node1 ~]# salt -G 'test:node2' test.ping
node2.minion:
True
pillar
给minion定义想要的数据
配置管理:
1.修改master配置文件
pillar_roots:
base:
- /etc/salt/pillar
创建
mkdir /etc/salt/pillar
进入
cd /etc/salt/pillar/
vim top.sls
base:
'*':
- init.rsyslog
mkdir init
cd init/
vim rsyslog.sls
{% if grains['osfinger'] == 'CentOS-6' %}
syslog:rsyslog
{% elif granins['osfinger'] == 'CentOS-5' %}
syslog: syslog
{{ endif }}