saltstack 数据系统
SaltStack有两大数据系统,分别是:
- Grains
- Pillar
SaltStack数据系统组件
Grains
Grains是SaltStack的一个组件,其存放着minion启动时收集到的信息。
Grains是SaltStack组件中非常重要的组件之一,因为我们在做配置部署的过程中会经常使用它,Grains是SaltStack记录minion的一些静态信息的组件。可简单理解为Grains记录着每台minion的一些常用属性,比如CPU、内存、磁盘、网络信息等。我们可以通过grains.items查看某台minion的所有Grains信息。
Grains的功能:
1. 收集资产信息
Grains应用场景:
- 信息查询
- 在命令行下进行目标匹配
- 在top file中进行目标匹配
- 在模板中进行目标匹配
实例!!
[root@master ~]# salt 'laolin' grains.items
laolin:
----------
SSDs:
biosreleasedate:
02/27/2020
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
......
......
selinux:
----------
enabled:
True
enforced:
Enforcing
serialnumber:
VMware-56 4d 21 6c 76 40 ae 6b-06 66 93 70 b2 73 03 4c
server_id:
134728941
shell:
/bin/sh
swap_total:
2047
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
uid:
0
username:
root
uuid:
6c214d56-4076-6bae-0666-9370b273034c
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.3.1
[root@master ~]# salt '*' grains.get fqdn_ip4
master:
- 192.168.160.131
laolin:
- 192.168.160.129
- 192.168.122.1
[root@master ~]# salt '*' grains.get ip4_interfaces
laolin:
----------
ens33:
- 192.168.160.129
lo:
- 127.0.0.1
virbr0:
- 192.168.122.1
virbr0-nic:
master:
----------
ens33:
- 192.168.160.131
lo:
- 127.0.0.1
[root@master ~]# salt '*' grains.get ip4_interfaces:ens33
laolin:
- 192.168.160.129
master:
- 192.168.160.131
用Grains来匹配minion
[root@master ~]# salt -G 'os:CentOS Stream' cmd.run 'uptime'
master:
22:29:10 up 43 min, 3 users, load average: 0.50, 0.27, 0.35
laolin:
22:29:10 up 58 min, 3 users, load average: 0.36, 0.40, 0.37
自定义grains的方法!
minion配置文件,在配置文件中搜索grains
在/etc/salt下生成一个grains文件,在此文件中定义
[root@laolin ~]# vim /etc/salt/grains
[root@laolin ~]# cat /etc/salt/grains
test-grains: linux-node1
[root@laolin ~]# systemctl restart salt-minion
[root@master ~]# salt '*' grains.get test-grains
laolin:
linux-node1
master:
# 不重启自定义grains!!
[root@laolin ~]# vim /etc/salt/grains
[root@laolin ~]# cat /etc/salt/grains
test-grains: linux-node1
aa: test
[root@laolin ~]# systemctl restart salt-minion
[root@master ~]# salt '*' saltutil.sync_grains
laolin:
master:
[root@master ~]# salt '*' grains.get aa
master:
test
minion1:
SaltStack组件之Pillar!
Pillar也是SaltStack组件中非常重要的组件之一,是数据管理中心,经常配置states在大规模的配置管理工作中使用它。Pillar在SaltStack中主要的作用就是存储和定义配置管理中需要的一些数据,比如软件版本号、用户名密码等信息,它的定义存储格式与Grains类似,都是YAML格式。
默认Base环境下Pillar的工作目录在/srv/pillar目录下。若你想定义多个环境不同的Pillar工作目录,只需要修改此处配置文件即可。
Pillar的特点:
- 可以给指定的minion定义它需要的数据
- 只有指定的人才能看到定义的数据
- 在master配置文件里设置
[root@master ~]# salt '*' pillar.items
master:
----------
laolin:
----------
默认pillar是没有任何信息的,如果想查看信息,需要在 master 配置文件上把 pillar_opts的注释取消,并将其值设为
True。
[root@master ~]# vim /etc/salt/master
# master config file that can then be used on minions.
pillar_opts: True
[root@master ~]# salt '*' pillar.items
master:
----------
master:
----------
__cli:
salt-master
__role:
master
allow_minion_key_revoke:
True
archive_jobs:
False
auth_events:
True
auth_mode:
1
auto_accept:
False
azurefs_update_interval:
60
cache:
localfs
cache_sreqs:
True
cachedir:
/var/cache/salt/master
clean_dynamic_modules:
True
cli_summary:
......
5
zmq_backlog:
1000
zmq_filtering:
False
zmq_monitor:
False
[root@master ~]# vim /etc/salt/master
pillar_roots:
base:
- /srv/pillar
[root@master ~]# mkdir -p /srv/pillar/base
[root@master ~]# systemctl restart salt-master
[root@master ~]# vim /srv/pillar/base/apache.sls
[root@master ~]# cat /srv/pillar/base/apache.sls
{% if grains['os'] == 'CentOS Stream' %}
apache: httpd
{% elif grains['os'] == 'Ubuntu' %}
apache: apache2
{% endif %}
[root@master ~]# vim /srv/pillar/base/top.sls
[root@master ~]# cat /srv/pillar/base/top.sls
base:
'laolin':
- apache
[root@master ~]# salt '*' pillar.items
minion:
----------
apache:
httpd
[root@master ~]# vim /srv/salt/base/top.sls
base:
'laolin':
- web.apache.install
[root@master ~]# mkdir /srv/salt/base/web/apache/
[root@master ~]# vim /srv/salt/base/web/apache/install.sls
apache-install:
pkg.installed:
- name: {{ pillar['apache'] }}
apache-service:
service.running:
- name: {{ pillar['apache'] }}
- enable: True
#配置ubuntu系统
ljj@czh-virtual-machine:~$ sudo apt-get install curl
tom@tom-virtual-machine:~$ sudo curl -fsSL -o /usr/share/keyrings/salt-archive-keyring.gpg https://repo.saltproject.io/py3/ubuntu/20.04/amd64/latest/salt-archive-keyring.gpg
tom@tom-virtual-machine:~$ echo "deb [signed-by=/usr/share/keyrings/salt-archive-keyring.gpg] https://repo.saltproject.io/py3/ubuntu/20.04/amd64/latest focal main" | sudo tee /etc/apt/sources.list.d/salt.list
tom@tom-virtual-machine:~$ sudo apt-get update
tom@tom-virtual-machine:~$ sudo apt-get install salt-minion
tom@tom-virtual-machine:~$ sudo vim /etc/salt/minion
master: 192.168.10.11
tom@tom-virtual-machine:~$ sudo systemctl restart salt-minion
[root@master ~]# salt-key -L
Accepted Keys:
master
laolin
ljj-virtual-machine
Denied Keys:
Unaccepted Keys:
tom-virtual-machine
[root@master ~]# salt 'ljj-virtual-machine' state.highstate
ljj-virtual-machine:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: The following packages were installed/updated: nginx
Started: 11:59:29.632336
Duration: 31734.892 ms
Changes:
----------
libnginx-mod-http-image-filter:
----------
new:
1.18.0-0ubuntu1.2
old:
libnginx-mod-http-xslt-filter:
----------
new:
1.18.0-0ubuntu1.2
old:
libnginx-mod-mail:
----------
new:
1.18.0-0ubuntu1.2
old:
libnginx-mod-stream:
----------
new:
1.18.0-0ubuntu1.2
old:
nginx:
----------
new:
1.18.0-0ubuntu1.2
old:
nginx-common:
----------
new:
1.18.0-0ubuntu1.2
old:
nginx-core:
----------
new:
1.18.0-0ubuntu1.2
old:
----------
ID: nginx-sevice
Function: service.running
Name: nginx
Result: True
Comment: The service nginx is already running
Started: 12:00:01.384897
Duration: 31.48 ms
Changes:
Summary for czh-virtual-machine
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 30.699 s
laolin:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: False
Comment: State 'pkg.installed' was not found in SLS 'web.nginx.install'
Reason: 'pkg' __virtual__ returned False
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: False
Comment: The named service nginx is not available
Started: 22:19:30.228337
Duration: 35.679 ms
Changes:
Summary for laolin
------------
Succeeded: 0
Failed: 2
------------
Total states run: 2
Total run time: 35.679 ms
ERROR: Minions returned with non-zero exit code