SaltStack之数据系统
一、SaltStack数据系统
SaltStack有两大数据系统,分别是:
- Grains
- Pillar
二、SaltStack数据系统组件
2.1 SaltStack组件之Grains
Grains是SaltStack的一个组件,其存放着minion启动时收集到的信息。
grains:收集minion启动之前产生的信息,minion启动之后再产生新的信息就收集不到。
Grains是SaltStack组件中非常重要的组件之一,因为我们在做配置部署的过程中会经常使用它,Grains是SaltStack记录minion的一些静态信息
(已经生成的既有事实,简单的说就是,它已经存在了)的组件。可简单理解为Grains记录着每台minion的一些常用属性,比如CPU、内存、磁盘、网络信息等。我们可以通过grains.items查看某台minion的所有Grains信息。
Grains的功能:
- 收集资产信息
Grains应用场景:
- 信息查询
- 在命令行下进行目标匹配
// 获取IP地址
[root@master base]# salt 'minion2' grains.get fqdn_ip4
minion2:
- 192.168.47.161
// 获取mac地址
[root@master base]# salt 'minion2' grains.get hwaddr_interfaces
minion2:
----------
ens33:
00:0c:29:a8:8f:68
lo:
00:00:00:00:00:00
[root@master base]# salt 'minion2' grains.get hwaddr_interfaces:ens33
minion2:
00:0c:29:a8:8f:68
// 根据类型来匹配
[root@master base]# salt -G 'os:CentOS' test.ping ## G=grains
minion2:
True
- 在top file中进行目标匹配
[root@master ~]# vim /srv/salt/base/top.sls
base:
'minion':
- web.nginx.install
'os:CentOS': ## 所有的centos系统
- match:grain
- web.apache.install
- 在模板中进行目标匹配
模板中进行目标匹配请看:https://docs.saltstack.com/en/latest/topics/pillar/
信息查询实例:
// 查看minion2这台主机里面的所有的grains信息
[root@master base]# salt 'minion2' grains.items
minion2:
----------
biosreleasedate: ## 日期
07/29/2019
biosversion: ## 版本
6.00
cpu_flags: ## cpu
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pat
- pse36
- clflush
- mmx
- fxsr
- sse
- sse2
- ss
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- eagerfpu
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- mpx
- rdseed
- adx
- smap
- clflushopt
- xsaveopt
- xsavec
- ibpb
- ibrs
- stibp
- arat
- spec_ctrl
- intel_stibp
- arch_capabilities
cpu_model:
Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
cpuarch:
x86_64
cwd:
/
disks:
- sda
- sr0
dns:
----------
domain:
ip4_nameservers:
- 192.168.47.2
ip6_nameservers:
nameservers:
- 192.168.47.2
options:
search:
- localdomain
sortlist:
domain:
efi:
False
efi-secure-boot:
False
fqdn: ## 完全相对域名(整个主机的域名)
minion2
fqdn_ip4:
- 192.168.47.161
fqdn_ip6:
- fe80::c7db:4c9f:7bcc:55f5
fqdns:
- minion2
gid:
0
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
vmware
groupname:
root
host:
minion2
hwaddr_interfaces: ## 硬件地址
----------
ens33:
00:0c:29:a8:8f:68
lo:
00:00:00:00:00:00
id:
minion2
init:
systemd
ip4_gw:
192.168.47.2
ip4_interfaces:
----------
ens33:
- 192.168.47.161
lo:
- 127.0.0.1
ip6_gw:
False
ip6_interfaces:
----------
ens33:
- fe80::c7db:4c9f:7bcc:55f5
lo:
- ::1
ip_gw:
True
ip_interfaces:
----------
ens33:
- 192.168.47.161
- fe80::c7db:4c9f:7bcc:55f5
lo:
- 127.0.0.1
- ::1
ipv4:
- 127.0.0.1
- 192.168.47.161
ipv6:
- ::1
- fe80::c7db:4c9f:7bcc:55f5
kernel:
Linux
kernelparams:
|_
- BOOT_IMAGE
- /vmlinuz-3.10.0-862.el7.x86_64
|_
- root
- /dev/mapper/centos-root
|_
- ro
- None
|_
- crashkernel
- auto
|_
- rd.lvm.lv
- centos/root
|_
- rd.lvm.lv
- centos/swap
|_
- rhgb
- None
|_
- quiet
- None
|_
- LANG
- zh_CN.UTF-8
kernelrelease:
3.10.0-862.el7.x86_64
kernelversion:
#1 SMP Fri Apr 20 16:44:24 UTC 2018
locale_info:
----------
defaultencoding:
UTF-8
defaultlanguage:
zh_CN
detectedencoding:
UTF-8
timezone:
unknown
localhost:
minion2
lsb_distrib_codename:
CentOS Linux 7 (Core)
lsb_distrib_id:
CentOS Linux
lvm:
----------
centos:
- home
- root
- swap
machine_id:
e9319b22209b40c1b7d23fdaa83ed485
manufacturer:
VMware, Inc.
master:
192.168.47.163
mdadm:
mem_total:
1821
nodename:
minion2
num_cpus:
2
num_gpus:
1
os:
CentOS
os_family:
RedHat
osarch:
x86_64
oscodename:
CentOS Linux 7 (Core)
osfinger:
CentOS Linux-7
osfullname:
CentOS Linux
osmajorrelease:
7
osrelease:
7.5.1804
osrelease_info:
- 7
- 5
- 1804
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
pid:
1360
productname:
VMware Virtual Platform
ps:
ps -efHww
pythonexecutable:
/usr/bin/python3
pythonpath:
- /usr/bin
- /usr/lib64/python36.zip
- /usr/lib64/python3.6
- /usr/lib64/python3.6/lib-dynload
- /usr/lib64/python3.6/site-packages
- /usr/lib/python3.6/site-packages
pythonversion:
- 3
- 6
- 8
- final
- 0
saltpath:
/usr/lib/python3.6/site-packages/salt
saltversion:
3004
saltversioninfo:
- 3004
selinux:
----------
enabled:
False
enforced:
Disabled
serialnumber:
VMware-56 4d 6d c3 65 a9 09 1f-1a f0 4a 1a 13 a8 8f 68
server_id:
1938021810
shell:
/bin/sh
ssds:
swap_total:
2047
systemd:
----------
features:
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
version:
219
systempath:
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
transactional:
False
uid:
0
username:
root
uuid:
c36d4d56-a965-1f09-1af0-4a1a13a88f68
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion: ## 消息队列的版本
4.1.4
自定义Grains的两种方法:
- minion配置文件,在配置文件中搜索grains(不建议使用)
[root@minion ~]# vim /etc/salt/minion
## 取消下面四行注释
137 grains:
138 roles:
139 - webserver
140 - memcache
141 # deployment: datacenter4
142 # cabinet: 13
143 # cab_u: 14-15
[root@minion ~]# systemctl restart salt-minion
[root@master ~]# salt 'minion' test.ping
minion:
True
[root@master ~]# salt 'minion' grains.items
minion:
----------
biosreleasedate:
07/29/2019
biosversion:
6.00
cpu_flags:
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pat
- pse36
- clflush
- mmx
- fxsr
- sse
- sse2
- ss
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- cpuid
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- invpcid_single
- ssbd
- ibrs
- ibpb
- stibp
- ibrs_enhanced
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- mpx
- rdseed
- adx
- smap
- clflushopt
- xsaveopt
- xsavec
- xsaves
- arat
- md_clear
- flush_l1d
- arch_capabilities
cpu_model:
Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
cpuarch:
x86_64
cwd:
/
disks:
- sr0
- sda
dns:
----------
domain:
ip4_nameservers:
- 192.168.47.2
ip6_nameservers:
nameservers:
- 192.168.47.2
options:
search:
- localdomain
sortlist:
domain:
efi:
False
efi-secure-boot:
False
fqdn:
minion
fqdn_ip4:
- 192.168.47.160
fqdn_ip6:
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
fqdns:
- minion
gid:
0
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
vmware
groupname:
root
host:
minion
hwaddr_interfaces:
----------
ens33:
00:0c:29:b1:e1:b3
lo:
00:00:00:00:00:00
id:
minion
init:
systemd
ip4_gw:
192.168.47.2
ip4_interfaces:
----------
ens33:
- 192.168.47.160
lo:
- 127.0.0.1
ip6_gw:
False
ip6_interfaces:
----------
ens33:
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
lo:
- ::1
ip_gw:
True
ip_interfaces:
----------
ens33:
- 192.168.47.160
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
lo:
- 127.0.0.1
- ::1
ipv4:
- 127.0.0.1
- 192.168.47.160
ipv6:
- ::1
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
kernel:
Linux
kernelparams:
|_
- BOOT_IMAGE
- (hd0,msdos1)/vmlinuz-4.18.0-257.el8.x86_64
|_
- root
- /dev/mapper/cs-root
|_
- ro
- None
|_
- crashkernel
- auto
|_
- resume
- /dev/mapper/cs-swap
|_
- rd.lvm.lv
- cs/root
|_
- rd.lvm.lv
- cs/swap
|_
- rhgb
- None
|_
- quiet
- None
kernelrelease:
4.18.0-257.el8.x86_64
kernelversion:
#1 SMP Thu Dec 3 22:16:23 UTC 2020
locale_info:
----------
defaultencoding:
UTF-8
defaultlanguage:
zh_CN
detectedencoding:
UTF-8
timezone:
CST
localhost:
minion
lsb_distrib_codename:
CentOS Stream 8
lsb_distrib_id:
CentOS Stream
lsb_distrib_release:
8
lvm:
----------
cs:
- home
- root
- swap
machine_id:
8272ff49fc6341d696ff7bea5bca4e9d
manufacturer:
VMware, Inc.
master:
192.168.47.163
mdadm:
mem_total:
780
nodename:
minion
num_cpus:
1
num_gpus:
1
os:
CentOS Stream
os_family:
RedHat
osarch:
x86_64
oscodename:
CentOS Stream 8
osfinger:
CentOS Stream-8
osfullname:
CentOS Stream
osmajorrelease:
8
osrelease:
8
osrelease_info:
- 8
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
pid:
85597
productname:
VMware Virtual Platform
ps:
ps -efHww
pythonexecutable:
/usr/bin/python3.6
pythonpath:
- /usr/bin
- /usr/lib64/python36.zip
- /usr/lib64/python3.6
- /usr/lib64/python3.6/lib-dynload
- /usr/lib64/python3.6/site-packages
- /usr/lib/python3.6/site-packages
pythonversion:
- 3
- 6
- 8
- final
- 0
roles:
- webserver
- memcache
saltpath:
/usr/lib/python3.6/site-packages/salt
saltversion:
3004
saltversioninfo:
- 3004
selinux:
----------
enabled:
False
enforced:
Disabled
serialnumber:
VMware-56 4d 54 a0 23 64 fe bd-5a 26 63 85 6c b1 e1 b3
server_id:
279719642
shell:
/bin/sh
ssds:
swap_total:
2139
systemd:
----------
features:
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy
version:
239
systempath:
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
transactional:
False
uid:
0
username:
root
uuid:
a0544d56-6423-bdfe-5a26-63856cb1e1b3
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.3.4
- 在/etc/salt下生成一个grains文件,在此文件中定义(推荐方式)
[root@minion ~]# vim /etc/salt/grains
地址: 虎泉
[root@minion ~]# systemctl restart salt-minion
[root@master ~]# salt 'minion' test.ping
minion:
True
[root@master ~]# salt 'minion' grains.items
minion:
----------
biosreleasedate:
07/29/2019
biosversion:
6.00
cpu_flags:
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pat
- pse36
- clflush
- mmx
- fxsr
- sse
- sse2
- ss
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- cpuid
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- invpcid_single
- ssbd
- ibrs
- ibpb
- stibp
- ibrs_enhanced
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- mpx
- rdseed
- adx
- smap
- clflushopt
- xsaveopt
- xsavec
- xsaves
- arat
- md_clear
- flush_l1d
- arch_capabilities
cpu_model:
Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
cpuarch:
x86_64
cwd:
/
disks:
- sr0
- sda
dns:
----------
domain:
ip4_nameservers:
- 192.168.47.2
ip6_nameservers:
nameservers:
- 192.168.47.2
options:
search:
- localdomain
sortlist:
domain:
efi:
False
efi-secure-boot:
False
fqdn:
minion
fqdn_ip4:
- 192.168.47.160
fqdn_ip6:
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
fqdns:
- minion
gid:
0
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
vmware
groupname:
root
host:
minion
hwaddr_interfaces:
----------
ens33:
00:0c:29:b1:e1:b3
lo:
00:00:00:00:00:00
id:
minion
init:
systemd
ip4_gw:
192.168.47.2
ip4_interfaces:
----------
ens33:
- 192.168.47.160
lo:
- 127.0.0.1
ip6_gw:
False
ip6_interfaces:
----------
ens33:
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
lo:
- ::1
ip_gw:
True
ip_interfaces:
----------
ens33:
- 192.168.47.160
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
lo:
- 127.0.0.1
- ::1
ipv4:
- 127.0.0.1
- 192.168.47.160
ipv6:
- ::1
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
kernel:
Linux
kernelparams:
|_
- BOOT_IMAGE
- (hd0,msdos1)/vmlinuz-4.18.0-257.el8.x86_64
|_
- root
- /dev/mapper/cs-root
|_
- ro
- None
|_
- crashkernel
- auto
|_
- resume
- /dev/mapper/cs-swap
|_
- rd.lvm.lv
- cs/root
|_
- rd.lvm.lv
- cs/swap
|_
- rhgb
- None
|_
- quiet
- None
kernelrelease:
4.18.0-257.el8.x86_64
kernelversion:
#1 SMP Thu Dec 3 22:16:23 UTC 2020
locale_info:
----------
defaultencoding:
UTF-8
defaultlanguage:
zh_CN
detectedencoding:
UTF-8
timezone:
CST
localhost:
minion
lsb_distrib_codename:
CentOS Stream 8
lsb_distrib_id:
CentOS Stream
lsb_distrib_release:
8
lvm:
----------
cs:
- home
- root
- swap
machine_id:
8272ff49fc6341d696ff7bea5bca4e9d
manufacturer:
VMware, Inc.
master:
192.168.47.163
mdadm:
mem_total:
780
nodename:
minion
num_cpus:
1
num_gpus:
1
os:
CentOS Stream
os_family:
RedHat
osarch:
x86_64
oscodename:
CentOS Stream 8
osfinger:
CentOS Stream-8
osfullname:
CentOS Stream
osmajorrelease:
8
osrelease:
8
osrelease_info:
- 8
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
pid:
98439
productname:
VMware Virtual Platform
ps:
ps -efHww
pythonexecutable:
/usr/bin/python3.6
pythonpath:
- /usr/bin
- /usr/lib64/python36.zip
- /usr/lib64/python3.6
- /usr/lib64/python3.6/lib-dynload
- /usr/lib64/python3.6/site-packages
- /usr/lib/python3.6/site-packages
pythonversion:
- 3
- 6
- 8
- final
- 0
saltpath:
/usr/lib/python3.6/site-packages/salt
saltversion:
3004
saltversioninfo:
- 3004
selinux:
----------
enabled:
False
enforced:
Disabled
serialnumber:
VMware-56 4d 54 a0 23 64 fe bd-5a 26 63 85 6c b1 e1 b3
server_id:
279719642
shell:
/bin/sh
ssds:
swap_total:
2139
systemd:
----------
features:
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy
version:
239
systempath:
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
transactional:
False
uid:
0
username:
root
uuid:
a0544d56-6423-bdfe-5a26-63856cb1e1b3
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.3.4
地址:
虎泉
不重启的情况下自定义Grains:
[root@minion ~]# vim /etc/salt/grains
地址: 虎泉
饮料: 纯牛奶
// 同步grains
[root@master ~]# salt 'minion' grains.items
minion:
Minion did not return. [No response]
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
salt-run jobs.lookup_jid 20211102135635516801
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt-run jobs.lookup_jid 20211102135635516801
minion:
----------
biosreleasedate:
07/29/2019
biosversion:
6.00
cpu_flags:
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pat
- pse36
- clflush
- mmx
- fxsr
- sse
- sse2
- ss
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- cpuid
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- invpcid_single
- ssbd
- ibrs
- ibpb
- stibp
- ibrs_enhanced
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- mpx
- rdseed
- adx
- smap
- clflushopt
- xsaveopt
- xsavec
- xsaves
- arat
- md_clear
- flush_l1d
- arch_capabilities
cpu_model:
Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
cpuarch:
x86_64
cwd:
/
disks:
- sr0
- sda
dns:
----------
domain:
ip4_nameservers:
- 192.168.47.2
ip6_nameservers:
nameservers:
- 192.168.47.2
options:
search:
- localdomain
sortlist:
domain:
efi:
False
efi-secure-boot:
False
fqdn:
minion
fqdn_ip4:
- 192.168.47.160
fqdn_ip6:
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
fqdns:
- minion
gid:
0
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
vmware
groupname:
root
host:
minion
hwaddr_interfaces:
----------
ens33:
00:0c:29:b1:e1:b3
lo:
00:00:00:00:00:00
id:
minion
init:
systemd
ip4_gw:
192.168.47.2
ip4_interfaces:
----------
ens33:
- 192.168.47.160
lo:
- 127.0.0.1
ip6_gw:
False
ip6_interfaces:
----------
ens33:
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
lo:
- ::1
ip_gw:
True
ip_interfaces:
----------
ens33:
- 192.168.47.160
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
lo:
- 127.0.0.1
- ::1
ipv4:
- 127.0.0.1
- 192.168.47.160
ipv6:
- ::1
- fe80::7a15:4c0a:5a8f:f2b9
- fe80::ffee:dcb8:ca4d:af8
kernel:
Linux
kernelparams:
|_
- BOOT_IMAGE
- (hd0,msdos1)/vmlinuz-4.18.0-257.el8.x86_64
|_
- root
- /dev/mapper/cs-root
|_
- ro
- None
|_
- crashkernel
- auto
|_
- resume
- /dev/mapper/cs-swap
|_
- rd.lvm.lv
- cs/root
|_
- rd.lvm.lv
- cs/swap
|_
- rhgb
- None
|_
- quiet
- None
kernelrelease:
4.18.0-257.el8.x86_64
kernelversion:
#1 SMP Thu Dec 3 22:16:23 UTC 2020
locale_info:
----------
defaultencoding:
UTF-8
defaultlanguage:
zh_CN
detectedencoding:
UTF-8
timezone:
CST
localhost:
minion
lsb_distrib_codename:
CentOS Stream 8
lsb_distrib_id:
CentOS Stream
lsb_distrib_release:
8
lvm:
----------
cs:
- home
- root
- swap
machine_id:
8272ff49fc6341d696ff7bea5bca4e9d
manufacturer:
VMware, Inc.
master:
192.168.47.163
mdadm:
mem_total:
780
nodename:
minion
num_cpus:
1
num_gpus:
1
os:
CentOS Stream
os_family:
RedHat
osarch:
x86_64
oscodename:
CentOS Stream 8
osfinger:
CentOS Stream-8
osfullname:
CentOS Stream
osmajorrelease:
8
osrelease:
8
osrelease_info:
- 8
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
pid:
98439
productname:
VMware Virtual Platform
ps:
ps -efHww
pythonexecutable:
/usr/bin/python3.6
pythonpath:
- /usr/bin
- /usr/lib64/python36.zip
- /usr/lib64/python3.6
- /usr/lib64/python3.6/lib-dynload
- /usr/lib64/python3.6/site-packages
- /usr/lib/python3.6/site-packages
pythonversion:
- 3
- 6
- 8
- final
- 0
saltpath:
/usr/lib/python3.6/site-packages/salt
saltversion:
3004
saltversioninfo:
- 3004
selinux:
----------
enabled:
False
enforced:
Disabled
serialnumber:
VMware-56 4d 54 a0 23 64 fe bd-5a 26 63 85 6c b1 e1 b3
server_id:
279719642
shell:
/bin/sh
ssds:
swap_total:
2139
systemd:
----------
features:
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy
version:
239
systempath:
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
transactional:
False
uid:
0
username:
root
uuid:
a0544d56-6423-bdfe-5a26-63856cb1e1b3
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.3.4
地址:
虎泉
饮料:
纯牛奶
2.2 SaltStack组件之Pillar
pillar比grains更加高级一点,pillar主要是用来(在master端)定义变量的。
Pillar也是SaltStack组件中非常重要的组件之一,是数据管理中心,经常配置states在大规模的配置管理工作中使用它。Pillar在SaltStack中主要的作用就是存储和定义配置管理中需要的一些数据,比如软件版本号、用户名密码等信息,它的定义存储格式与Grains类似,都是YAML格式。
在Master配置文件中有一段Pillar settings选项专门定义Pillar相关的一些参数:
[root@master ~]# cd /etc/salt/
[root@master salt]# ls
cloud cloud.maps.d master minion.d proxy
cloud.conf.d cloud.profiles.d master.d minion_id proxy.d
cloud.deploy.d cloud.providers.d minion pki roster
[root@master salt]# vim master
858 pillar_roots:
859 base:
860 - /srv/pillar/base
[root@master salt]# systemctl restart salt-master
[root@master ~]# tree /srv/
/srv/
└── salt
├── base
│ ├── top.sls
│ └── web
│ ├── apache
│ │ └── install.sls
│ └── nginx
│ └── install.sls
├── dev
├── prod
└── test
8 directories, 3 files
// 因为srv/下面没有pillar/base这两个目录,所以要去创建一个
[root@master ~]# mkdir /srv/pillar/base -p
[root@master ~]# tree /srv/
/srv/
├── pillar
│ └── base
└── salt
├── base
│ ├── top.sls
│ └── web
│ ├── apache
│ │ └── install.sls
│ └── nginx
│ └── install.sls
├── dev
├── prod
└── test
10 directories, 3 files
默认Base环境下Pillar的工作目录在/srv/pillar目录下。若你想定义多个环境不同的Pillar工作目录,只需要修改此处配置文件即可。
Pillar的特点:
- 可以给指定的minion定义它需要的数据
- 只有指定的人才能看到定义的数据
- 在master配置文件里设置
//查看pillar的信息
[root@master ~]# salt '*' pillar.items
minion2:
----------
minion:
----------
master:
----------
默认pillar是没有任何信息的,如果想查看信息,需要在 master 配置文件上把 pillar_opts的注释取消,并将其值设为 True。
注:这个功能一点用处都没有,后面也用不到它,最好是用默认的方式,关闭它
[root@master ~]# vim /etc/salt/master
908 pillar_opts: True
//重启master并查看pillar的信息
[root@master ~]# systemctl restart salt-master
[root@master ~]# salt 'minion' pillar.items
minion:
----------
master:
----------
__cli:
salt-master
__role:
master
allow_minion_key_revoke:
True
archive_jobs:
False
auth_events:
True
....此处省略N行
winrepo_branch:
master
winrepo_cachefile:
winrepo.p
winrepo_dir:
/srv/salt/win/repo
winrepo_dir_ng:
/srv/salt/win/repo-ng
winrepo_fallback:
winrepo_insecure_auth:
False
winrepo_passphrase:
winrepo_password:
winrepo_privkey:
winrepo_pubkey:
winrepo_refspecs:
- +refs/heads/*:refs/remotes/origin/*
- +refs/tags/*:refs/tags/*
winrepo_remotes:
- https://github.com/saltstack/salt-winrepo.git
winrepo_remotes_ng:
- https://github.com/saltstack/salt-winrepo-ng.git
winrepo_ssl_verify:
True
winrepo_user:
worker_threads:
5
zmq_backlog:
1000
zmq_filtering:
False
zmq_monitor:
False
pillar自定义数据:
在master的配置文件里找pillar_roots可以看到其存放pillar的位置
// 首先在配置文件里面的pillar——roots下面加入/srv/pillar/base(因为在前面做过,所以这里省略)
// 创建/srv/pillar/base目录,也在前创建过了(这里也省略)
// 重启服务(这里也省略,前面重启过了)
// 去/srv/pillar/base下面写一个apache.sls状态文件
[root@master ~]# cd /srv/pillar/base/
[root@master base]# ls
[root@master base]# vim apache.sls
{% if grains['os'] == 'CentOS' %} ## 如果grains里面的os系统等于CentOS的话,就安装httpd
package: httpd
{% elif grains['os'] == 'CentOS Stream' %}
package: nginx
{% endif %}
//定义top file入口文件(这里面的top file跟状态文件里面的top file不一样,这个里面的top file是用来指定变量文件是谁能够用的,而在salt里面的top file是表示给高级状态用的。这是两回事,但是它的名字是一样的,都叫top.sls;而且放的位置也是一模一样的,都是在base里面,只不过一个在/srv的salt下面,一个在/srv的pillar下面。除此之外,一模一样,没有区别)
[root@master ~]# cd /srv/pillar/base/
[root@master base]# vim top.sls
base: ## 指定环境
'minion*': ## 指定目标
- apache ## 引用apache.sls
//这个top.sls文件的意思表示的是minion所有主机的base环境能够访问到apache这个pillar
[root@master base]# salt '*' pillar.items
minion2:
----------
package:
httpd
minion:
----------
package:
nginx
master:
----------
//在salt下修改apache的状态文件,引用pillar的数据
[root@master base]# vim /srv/salt/base/web/apache/install.sls
apache-install:
pkg.installed:
- name: {{ pillar['package'] }} ## 用pillar去取package这个变量的值
apache-service:
service.running:
- name: {{ pillar['package'] }}
- enable: true
[root@master ~]# cat /srv/salt/base/top.sls
base:
'minion':
- web.nginx.install
'minion2':
- web.apache.install
//执行高级状态文件
[root@master base]# salt '*' state.highstate
master:
----------
ID: states
Function: no.None
Result: False
Comment: No Top file or master_tops data matches found. Please see master log for details.
Changes:
Summary for master
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
minion2:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 15:56:24.458259
Duration: 1069.042 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 15:56:25.531713
Duration: 56.368 ms
Changes:
Summary for minion2
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.125 s
minion:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 15:06:13.652748
Duration: 1515.835 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: The service nginx is already running
Started: 15:06:15.170662
Duration: 81.283 ms
Changes:
Summary for minion
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.597 s
ERROR: Minions returned with non-zero exit code
2.3 Grains与Pillar的区别
存储位置 | 类型 | 采集方式 | 应用场景 | |
---|---|---|---|---|
Grains | minion | 静态 | minion启动时采集,可通过刷新避免重启minion服务 | 1.信息查询 2.在命令行下进行目标匹配 3.在top file中进行目标匹配 4.在模板中进行目标匹配 |
Pillar | master | 动态 | 指定,实时生效 | 1.目标匹配 2.敏感数据配置 |