saltstack主要有兩種數據系統:html
1)當minion啓動後會收集自身的狀態信息即grains信息,grains信息是靜態的,存儲在minion端,如操做系統版本,內核版本,CPU,內存,硬盤,設備型號等。這些信息能夠做爲master端的匹配目標node
2)master端能夠自定義grains設置,但須要執行命令推送到minion端python
3)master端推送的數據會存放在minion端的/var/cache/saltlinux
grains官方文檔:https://docs.saltstack.com/en/latest/topics/grains/ios
刷新grains的方法:git
一、重啓minion 二、master強制刷新:salt '*' saltutil.sync_grains或者saltutil.sync_all
1)salt '*' grains.ls ==>列出全部可打印的狀態模塊web
[root@salt-master ~]# salt 'salt-minion1-c7' grains.ls salt-minion1-c7: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - disks - dns - domain - fqdn - fqdn_ip4 - fqdn_ip6 - fqdns - gid - gpus - groupname - host - hwaddr_interfaces - id - init - ip4_gw - ip4_interfaces - ip6_gw - ip6_interfaces - ip_gw - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - kernelversion - locale_info - localhost - lsb_distrib_codename - lsb_distrib_id - machine_id - manufacturer - master - mdadm - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - osrelease_info - path - pid - productname - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - serialnumber - server_id - shell - swap_total - systemd - uid - username - uuid - virtual - zfs_feature_flags - zfs_support - zmqversion
2)salt '*' grains.items ==>列出狀態信息shell
[root@salt-master ~]# salt 'salt-minion1-c7' grains.items salt-minion1-c7: ---------- SSDs: biosreleasedate: 05/19/2017 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 - 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 - rdseed - adx - smap - xsaveopt - arat cpu_model: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz cpuarch: x86_64 disks: - sda - sr0 dns: ---------- domain: ip4_nameservers: - 223.5.5.5 ip6_nameservers: nameservers: - 223.5.5.5 options: search: sortlist: domain: fqdn: salt-minion1-c7 fqdn_ip4: - 10.0.0.21 fqdn_ip6: - fe80::20c:29ff:fe3e:9d2f fqdns: gid: 0 gpus: |_ ---------- model: SVGA II Adapter vendor: vmware groupname: root host: salt-minion1-c7 hwaddr_interfaces: ---------- eth0: 00:0c:29:3e:9d:2f lo: 00:00:00:00:00:00 id: salt-minion1-c7 init: systemd ip4_gw: 10.0.0.254 ip4_interfaces: ---------- eth0: - 10.0.0.21 lo: - 127.0.0.1 ip6_gw: False ip6_interfaces: ---------- eth0: - fe80::20c:29ff:fe3e:9d2f lo: - ::1 ip_gw: True ip_interfaces: ---------- eth0: - 10.0.0.21 - fe80::20c:29ff:fe3e:9d2f lo: - 127.0.0.1 - ::1 ipv4: - 10.0.0.21 - 127.0.0.1 ipv6: - ::1 - fe80::20c:29ff:fe3e:9d2f kernel: Linux kernelrelease: 3.10.0-693.el7.x86_64 kernelversion: #1 SMP Tue Aug 22 21:09:27 UTC 2017 locale_info: ---------- defaultencoding: UTF-8 defaultlanguage: en_US detectedencoding: UTF-8 localhost: salt-minion1-c7 lsb_distrib_codename: CentOS Linux 7 (Core) lsb_distrib_id: CentOS Linux machine_id: 6a95166986604960b8a690e6e5103d2f manufacturer: VMware, Inc. master: 10.0.0.11 mdadm: mem_total: 976 nodename: salt-minion1-c7 num_cpus: 1 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.4.1708 osrelease_info: - 7 - 4 - 1708 path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin pid: 15941 productname: VMware Virtual Platform ps: ps -efHww pythonexecutable: /usr/bin/python pythonpath: - /usr/bin - /usr/lib64/python27.zip - /usr/lib64/python2.7 - /usr/lib64/python2.7/plat-linux2 - /usr/lib64/python2.7/lib-tk - /usr/lib64/python2.7/lib-old - /usr/lib64/python2.7/lib-dynload - /usr/lib64/python2.7/site-packages - /usr/lib64/python2.7/site-packages/gtk-2.0 - /usr/lib/python2.7/site-packages pythonversion: - 2 - 7 - 5 - final - 0 saltpath: /usr/lib/python2.7/site-packages/salt saltversion: 2019.2.0 saltversioninfo: - 2019 - 2 - 0 - 0 selinux: ---------- enabled: False enforced: Disabled serialnumber: VMware-56 4d e6 d4 88 d2 ac 39-c3 e1 9b 64 a5 3e 9d 2f server_id: 1333013820 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 +IDN version: 219 uid: 0 username: root uuid: d4e64d56-d288-39ac-c3e1-9b64a53e9d2f virtual: VMware zfs_feature_flags: False zfs_support: False zmqversion: 4.1.4
3)salt '*' grains.item fqdn_ip4 ==>列出特定的狀態項,如獲取minion的ip地址數據庫
[root@salt-master ~]# salt '*' grains.item fqdn_ip4 salt-minion1-c7: ---------- fqdn_ip4: - 10.0.0.21 salt-minion2-c7: ---------- fqdn_ip4: - 10.0.0.22 salt-minion3-c7: ---------- fqdn_ip4: - 10.0.0.23 salt-minion4-c6: ---------- fqdn_ip4: [root@salt-master ~]# salt 'salt-minion4-c6' grains.item ip4_interfaces:eth0 salt-minion4-c6: ---------- ip4_interfaces:eth0: - 10.0.0.24
4)salt '*' grains.get [key] ==>獲取相關信息vim
[root@salt-master ~]# salt '*' grains.get saltversion salt-minion2-c7: 2019.2.0 salt-minion1-c7: 2019.2.0 salt-minion3-c7: 2019.2.0 salt-minion4-c6: 2019.2.0 [root@salt-master ~]# salt '*' grains.get ip4_interfaces:eth0 salt-minion1-c7: - 10.0.0.21 salt-minion2-c7: - 10.0.0.22 salt-minion3-c7: - 10.0.0.23 salt-minion4-c6: - 10.0.0.24
5)salt –G ==>指定匹配目標的主機執行命令
[root@salt-master ~]# salt -G 'osmajorrelease:7' cmd.run 'uptime' #在centos7上執行,centos6不執行 salt-minion1-c7: 00:10:36 up 5:30, 2 users, load average: 0.00, 0.01, 0.05 salt-minion2-c7: 00:10:36 up 5:30, 2 users, load average: 0.08, 0.05, 0.05 salt-minion3-c7: 00:10:36 up 5:30, 2 users, load average: 0.04, 0.03, 0.05 [root@salt-master ~]# salt -G 'os:CentOS' cmd.run 'uptime' #在系統是centos的主機上執行 salt-minion3-c7: 00:10:56 up 5:30, 2 users, load average: 0.03, 0.03, 0.05 salt-minion2-c7: 00:10:56 up 5:30, 2 users, load average: 0.05, 0.05, 0.05 salt-minion4-c6: 08:09:26 up 5:28, 2 users, load average: 0.00, 0.00, 0.00 salt-minion1-c7: 00:10:56 up 5:30, 2 users, load average: 0.00, 0.01, 0.05
#編輯minion端的配置文件,自定義grains [root@salt-minion1-c7 ~]# vim /etc/salt/minion grains: roles: - webserver os_version: - centos7.5 #重啓minion [root@salt-minion1-c7 ~]# systemctl restart salt-minion #master端執行目標匹配 [root@salt-master ~]# salt -G 'roles:webserver' test.ping salt-minion1-c7: True [root@salt-master ~]# salt -G 'os_version:centos7.5' test.ping salt-minion1-c7: True
Master
端編寫一個Grains
的Python
腳本推送給minion
默認自定義腳本須要放在Master的/srv/salt/_grains目錄下,這個目錄須要本身建立,由/etc/salt/masterfile_root定義。同步到Minion的腳本會被放在minion的/
var/cache/salt/minion/extmods/grains/
自定義python腳本格式:
def 自定義函數名(): grains = {} grains['須要添加的grain item 名字'] = 命令行獲取方法 return grains
獲取系統時間實例:
#在master端自定義python腳本 [root@salt-master ~]# cat /srv/salt/_grains/get_time.py #!/usr/bin/env python #-*- coding: utf-8 -*- from datetime import datetime def get_server_time(): grains = {} grains['server_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') return grains #使用sync_grains命令同步腳本到Minion主機上,並經過grains.item命令獲取相關信息 [root@salt-master ~]# salt '*' saltutil.sync_grains salt-minion1-c7: - grains.get_time salt-minion4-c6: - grains.get_time salt-minion2-c7: - grains.get_time salt-minion3-c7: - grains.get_time [root@salt-master ~]# salt '*' grains.get server_time salt-minion2-c7: 2019-08-31 10:17:10 salt-minion1-c7: 2019-08-31 10:17:10 salt-minion3-c7: 2019-08-31 10:17:10 salt-minion4-c6: 2019-08-31 18:16:39 #Minion存放Master同步來的腳本目錄 [root@salt-minion1-c7 ~]# ll /var/cache/salt/minion/extmods/grains/ -rw------- 1 root root 206 Aug 31 10:17 get_time.py -rw------- 1 root root 443 Aug 31 10:17 get_time.pyc
默認grains信息>minion配置文件定義>master定義的grains推送
Pillar是動態的,Pillar存儲在master端,提供給minion端。
Pillar主要記錄一些加密信息, 能夠確保這些敏感數據不被其餘minion看到。
Pillar在SaltStack中主要做用是存儲和定義配置管理中須要的一些數據,好比軟件版本號、用戶名密碼等信息,它的存儲格式跟Grains相似,都是YAML格式。
pillar官方文檔:https://docs.saltstack.com/en/latest/topics/pillar/
pillar相關文件:
salt '*' sys.doc pillar # 查看與pillar有關的幫助信息 salt '*' pillar.items # 獲取全部pillar items值 salt '*' pillar.data # 等價於pillar.items salt '*' saltutil.refresh_pillar # 刷新pillar值 salt '*' saltutil.sync_all # 刷新pillar值,與refresh_pillar操做相似,但範圍更大 salt '*' sys.list_functions pillar # 列出全部的pillar相關函數方法 salt '*' pillar.get xxx # 獲取某項的值,相似於python字典的get函數 salt '*' pillar.raw # 內存中獲取 salt '*' state.highstate pillar={'test': 'abc'} # 在命令設置pillar 數據
(1)pillar目錄 pillar_roots: base: - /srv/pillar (2)是否將master配置文件中的數據信息添加到pillar中,默認是不加,須要的話能夠改爲True,重啓服務生效。 pillar_opts: False (3)pillar 源,salt支持引入pillar外部資源,例如從數據庫導入pillar值,默認是關閉的 ext_pillar_first: False (4)開啓pillar gitgs ssl驗證 pillar_gitfs_ssl_verify: True (5)開啓pillar render 錯誤信息 pillar_safe_render_error: True (6)設置pillar配置合併策略 pillar_source_merging_strategy: smart
1)salt '*' saltutil.refresh_pillar
2)salt '*' saltutil.sync_all
默認文件入口:/src/pillar/top.sls
top.sls的引用有兩種:
1)將引用sls文件放在同級目錄
[root@saltmaster srv]# tree /srv/pillar/ /srv/pillar/ # 默認pillar文件目錄 ├── packages.sls # packages 引用文件 ├── services.sls # services 引用文件 └── top.sls # 固定的入口文件top.sls
2)在/src/pillar/ 目錄下建立對應的packages, services目錄,並在該目錄下建立init.sls文件
[root@saltmaster srv]# tree /srv/pillar/ /srv/pillar/ ├── packages # packages 目錄 │ └── init.sls # 引用文件 ├── services # services 目錄 │ └── init.sls # 引用文件 └── top.sls # 固定的入口文件top.sls
備註:若是要引用執行的sls文件和目錄同時存在,也就是方法一和方法二都存在,packages.sls和packages/init.sls,會優先使用packages.sls文件。
[root@salt-master ~]# vim /etc/salt/master pillar_roots: base: - /srv/pillar [root@salt-master ~]# vim /etc/salt/master [root@salt-master ~]# systemctl restart salt-master [root@salt-master ~]# mkdir /srv/pillar [root@salt-master ~]# cd /srv/pillar
[root@salt-master pillar]# cat zabbix.sls zabbix: package-name: zabbix version: 3.2.4 port: 10050 user: admin #定義topfile [root@salt-master pillar]# cat top.sls base: 'salt-minion1-c7': - zabbix
[root@salt-master pillar]# salt '*' saltutil.refresh_pillar
[root@salt-master pillar]# salt 'salt-minion1-c7' pillar.item zabbix salt-minion1-c7: ---------- zabbix: ---------- package-name: zabbix port: 10050 user: admin version: 3.2.4
類型 | 數據採集方式 | 應用場景 | 定義位置 |
Grains | 靜態,minion啓動時採集 | 用於數據查詢,目標選擇,配置管理 | minion |
Pillar | 動態,master上定義 | 用於目標選擇,配置管理,敏感數據 | master |