saltsack自动化配置day01:数据系统-Grains(二)
一. Grains应用场景
Grains可以在state系统中使用,用于配置管理模块。
Grains可以在target中使用,用来匹配minion,比如匹配操作系统,使用-G选项。
Grains可以用于信息查询,Grains保存着收集到的客户端的详细信息。
二. Grains应用示例
1、minion配置grains:
自定义grains方法一
[root@luoahong ~]# vim /etc/salt/minion grains: roles: nginx env: test [root@luoahong ~]# systemctl restart salt-minion [root@salt-master ~]# salt 'salt-master' grains.ls [root@salt-master ~]# salt 'salt-master' grains.items [root@salt-master ~]# salt '10.1.0.112' grains.get role 10.1.0.112: nginx [root@salt-master ~]# salt '10.1.0.112' grains.get env 10.1.0.112: test [root@salt-master ~]#
自定义grains方法二(推荐)
1、配置文件
[root@luoahong ~]# vim /etc/salt/grains cloud: openstack
2、重启服务
[root@luoahong ~]# systemctl restart salt-minion
3、获取
[root@salt-master ~]# salt '10.1.0.112' grains.get cloud 10.1.0.112: openstack [root@salt-master ~]# [root@salt-master ~]# salt -G role:nginx cmd.run 'hostname' # 按角色匹配 10.1.0.112: luoahong [[root@salt-master ~]# salt -G os:CentOS cmd.run 'uptime' # 按操作系统匹配 salt-master: 17:24:03 up 202 days, 7:44, 1 user, load average: 0.55, 0.29, 0.21 10.1.0.112: 01:21:19 up 202 days, 7:07, 2 users, load average: 0.00, 0.01, 0.05 [root@salt-master ~]#
- 老师这个不就是不自动化了吗?
- 既然要手动设置,肯定有特殊情况
- ntp server和ntp 客户端能一样不
我要执行一个命令只有ntp客户端执行,服务器端不能执行
假如有一个server好说,要是有好多个了,后面可以用not去解决
2、查看minion的全部静态变量
[root@saltstack base]# salt 'manager' grains.items traffic-manager: ---------- SSDs: biosreleasedate: 01/01/2011 biosversion: 0.5.1 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 - rep_good - nopl - 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 - bmi1 - hle - avx2 - smep - bmi2 - erms - invpcid - rtm - rdseed - adx - smap - xsaveopt cpu_model: Intel Core Processor (Broadwell) cpuarch: x86_64 disks: - vda - vdb dns: ---------- domain: ip4_nameservers: - 218.30.19.50 - 61.134.1.4 ip6_nameservers: nameservers: - 218.30.19.50 - 61.134.1.4 options: search: - openstacklocal - novalocal sortlist: domain: fqdn: traffic-manager fqdn_ip4: fqdn_ip6: gid: 0 gpus: |_ ---------- model: GD 5446 vendor: unknown groupname: root host: traffic-manager hwaddr_interfaces: ---------- eth0: fa:16:3e:bd:4c:67 lo: 00:00:00:00:00:00 id: traffic-manager init: systemd ip4_gw: 192.168.0.1 ip4_interfaces: ---------- eth0: - 192.168.0.106 lo: - 127.0.0.1 ip6_gw: 192.168.0.1 ip6_interfaces: ---------- eth0: lo: ip_gw: True ip_interfaces: ---------- eth0: - 192.168.0.106 lo: - 127.0.0.1 ipv4: - 192.168.0.106 - 127.0.0.1 ipv6: kernel: Linux kernelrelease: 3.10.0-327.el7.x86_64 kernelversion: #1 SMP Thu Nov 19 22:10:57 UTC 2015 locale_info: ---------- defaultencoding: UTF-8 defaultlanguage: en_US detectedencoding: UTF-8 localhost: traffic-manager lsb_distrib_codename: CentOS Linux 7 (Core) lsb_distrib_id: CentOS Linux machine_id: cf9b563caf2bc11cab56d6a504ff6a29 manufacturer: Fedora Project master: saltstack.node1.com mdadm: mem_total: 7823 nodename: traffic-manager num_cpus: 4 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.2.1511 osrelease_info: - 7 - 2 - 1511 path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin pid: 11746 productname: OpenStack Nova 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/lib/python2.7/site-packages pythonversion: - 2 - 7 - 5 - final - 0 saltpath: /usr/lib/python2.7/site-packages/salt saltversion: 2018.3.2 saltversioninfo: - 2018 - 3 - 2 - 0 selinux: ---------- enabled: False enforced: Disabled serialnumber: da6ef2df-2f44-4c37-a7b4-08c4db44348b server_id: 1473315310 shell: /bin/sh swap_total: 4094 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: 9d461f47-0ad9-4dbe-8ab4-8fc3ef9a51fd virtual: kvm zfs_feature_flags: False zfs_support: False zmqversion: 4.1.4
2.显示grains的变量名称
[root@saltstack base]# salt 'manager' grains.ls traffic-manager: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - disks - dns - domain - fqdn - fqdn_ip4 - fqdn_ip6 - 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
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
salt '*' grains.item ip4_interfaces:eth0
5.-G的使用
-G就是grains [root@node1 ~]# salt -G 'os:CentOs' test.ping node2.minion: True 如果os的centos的 执行test.ping
6、top文件里面的匹配
base: 'os:CentOS': - match:grain - web.apache
- 不是最佳实践,实际我们用的不写在grains里写在pillar
- salt掉pillar pillar掉grains好复杂了sls文件看起来就很清晰
三、小结
作者:罗阿红
出处:http://www.cnblogs.com/luoahong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。