What is grains:
is a component of salt that is responsible for the static data collected at the first boot of the Minion, which can be used in salt modules and other components. Each time the Minion starts (restarts), it will be collected.
Application Scenarios:
1. Using the configuration Management module in state
2. Used in target to match minion, for example, match operating system using-G option
3. For information query, grains Save the details of the collected client
View Grains:
# Salt ' node23.com ' grains.ls #查看grains的名称 salt ' node23.com ' grains.items view name and value node23.com:-Ssds-biosreleaseda Te-biosversion-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-manufacturer-master-mdadm-mem_total-nodename-num_cpus -NUM_GPUS-OS-OS_FAMILY-OSARCH-OSCODENAME-OSFINGER-OSFULLNAME-OSMAJORRELEASE-OSR Elease-osrelease_info-path-productname-ps-pythonexecutable-pythonpath-pythonversion -Saltpath-saltversion-saltversioninfo-selinux-serialnumber-server_id-shell-virtual -Zmqversion
Using grains:
1. Use in the command line:
# salt-g ' Os:centos ' test.pingnode23.com:truenode24.com:true# salt-g ' ip_interfaces:eth0:192.168.0.* ' Cmd.run ' W ' Node23.com:19:00:46 up 3 days, 12:21, 4 users, Load average:0.00, 0.00, 0.00 USER TTY from [Email protected] IDLE jcpu PCPU What root tty1-sat10 3days 0.08s 0.08s-bash root pts/0 192. 168.0.113 18:56 1.00s 0.48s 0.42s/usr/bin/python root pts/1 192.168.0.113 17:33 1:26m 0.04s 0.0 4s-bash root PTS/3 192.168.0.113 16:30 1:30m 0.54s 0.54s-bashnode24.com:02:44:30 up 3 days, 8:34 , 5 users, Load average:0.31, 0.08, 0.02 USER TTY from [email protected] IDLE jcpu PCPU What Root tty1:0 Sat18 3days 7.15s 7.15s/usr/bin/xorg:root pts/0:0.0 Sat18 3days 0.03s 0.03s/bin/bash Root pts/1 192.168.0.113 00:14 2:18m 0.13s 0.13s-baSH root pts/3 192.168.0.113 01:17 1:26m 0.03s 0.03s-bash root PTS/4 192.168.0.113 02:39 4:33 0.04s 0.04s-bash
2. Using Grains in SLS
base: ‘os:CentOS‘: - match: grain - nginx
Custom Grains
- Defined in the Minon file
vim /etc/salt/miniongrains: web: - nginx/etc/init.d/salt-minion restart # salt ‘node23.com‘ grains.get webnode23.com: - nginx
defined in 2.grains files
vim /etc/salt/grainsbash: shellOS: centos6.6/etc/init.d/salt-minion restart # salt ‘node23.com‘ grains.get OSnode23.com:centos6.6# salt ‘node23.com‘ grains.get bashnode23.com:shell
Recommended use of custom grains
?
Saltstack's Grains detailed