Saltstack2 grains module
# Grains module: #
Application scenarios of grains:
1. Information query
2. Match minion in remote execution
For example, I want to execute a command on all centos machines.
3. Match minion in top.sls
4. use it in the template of the configuration file
1. Information query
# list all the key of grains and all the modules supported by grains
[root@salt_master ~] # salt 'minion.saltstack.com' grains.ls
Minion.saltstack.com:
-SSDs
-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
-master
-mdadm
-mem_total
-nodename
-num_cpus
-num_gpus
-os
-os_family
-osarch
-oscodename
-osfinger
-osfullname
-osmajorrelease
-osrelease
-osrelease_info
-path
-ps
-pythonexecutable
-pythonpath
-pythonversion
-saltpath
-saltversion
-saltversioninfo
-selinux
-server_id
-shell
-virtual
-zmqversion
# # display the contents of all grains
[root@salt_master ~] # salt 'minion.saltstack.com' grains.items
Minion.saltstack.com:
-
SSDs:
Cpu_flags:
-fpu
-vme
-de
-pse
-tsc
-msr
-pae
-mce
-cx8
-apic
-sep
-mtrr
-pge
-mca
-cmov
-pat
-pse36
-clflush
-dts
-mmx
-fxsr
-sse
-sse2
-ss
-syscall
-nx
-pdpe1gb
-rdtscp
-lm
-constant_tsc
-up
-arch_perfmon
-pebs
-bts
-xtopology
-tsc_reliable
-nonstop_tsc
-aperfmperf
-unfair_spinlock
-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
-ida
-arat
-epb
-xsaveopt
-pln
-pts
-dts
-fsgsbase
-bmi1
-avx2
-smep
-bmi2
-invpcid
Cpu_model:
Intel (R) Core (TM) i5-4460 CPU @ 3.20GHz
Cpuarch:
X86_64
Domain:
Saltstack.com
Fqdn:
Minion.saltstack.com
Fqdn_ip4:
-10.20.23.124
Fqdn_ip6:
Gpus:
| | _
-
Model:
SVGA II Adapter
Vendor:
Unknown
Host:
Minion
Hwaddr_interfaces:
-
Eth2:
00:0c:29:6e:29:0d
Lo:
00:00:00:00:00:00
Id:
Minion.saltstack.com
Init:
Upstart
Ip4_interfaces:
-
Eth2:
-10.20.23.124
Lo:
-127.0.0.1
Ip6_interfaces:
-
Eth2:
-fe80::20c:29ff:fe6e:290d
Lo:
-: 1
Ip_interfaces:
-
Eth2:
-10.20.23.124
-fe80::20c:29ff:fe6e:290d
Lo:
-127.0.0.1
-: 1
Ipv4:
-10.20.23.124
-127.0.0.1
Ipv6:
-: 1
-fe80::20c:29ff:fe6e:290d
Kernel:
Linux
Kernelrelease:
2.6.32-431.el6.x86_64
Locale_info:
-
Defaultencoding:
UTF8
Defaultlanguage:
Zh_CN
Detectedencoding:
UTF-8
Localhost:
Salt_minion
Lsb_distrib_codename:
Final
Lsb_distrib_id:
CentOS
Lsb_distrib_release:
6.5
Master:
Salt_master
Mdadm:
Mem_total:
four hundred and seventy four
Nodename:
Salt_minion
Num_cpus:
one
Num_gpus:
one
Os:
CentOS
Os_family:
RedHat
Osarch:
X86_64
Oscodename:
Final
Osfinger:
CentOS-6
Osfullname:
CentOS
Osmajorrelease:
six
Osrelease:
6.5
Osrelease_info:
-6
-5
Path:
/ sbin:/usr/sbin:/bin:/usr/bin
Ps:
Ps-efH
Pythonexecutable:
/ usr/bin/python2.6
Pythonpath:
-/ usr/bin
-/ usr/lib64/python26.zip
-/ usr/lib64/python2.6
-/ usr/lib64/python2.6/plat-linux2
-/ usr/lib64/python2.6/lib-tk
-/ usr/lib64/python2.6/lib-old
-/ usr/lib64/python2.6/lib-dynload
-/ usr/lib64/python2.6/site-packages
-/ usr/lib/python2.6/site-packages
Pythonversion:
-2
-6
-6
-final
-0
Saltpath:
/ usr/lib/python2.6/site-packages/salt
Saltversion:
2015.5.10
Saltversioninfo:
-2015
-5
-10
-0
Selinux:
-
Enabled:
True
Enforced:
Permissive
Server_id:
748666745
Shell:
/ bin/bash
Virtual:
VMware
Zmqversion:
3.2.5
# if you want to view an item, change items to singular item, and add the name of an item at the end:
[root@salt_master ~] # salt 'minion.saltstack.com' grains.item os
Minion.saltstack.com:
-
Os:
CentOS
# get parameter: only the content of the value is displayed relative to the items,get
[root@salt_master ~] # salt 'minion.saltstack.com' grains.get os
Minion.saltstack.com:
CentOS
2. Match minion in remote execution
For example, I want to execute a command on all centos machines.
[root@salt_master ~] # salt-- help | grep grain
-G,-- grain Instead of using shell globs to evaluate the target
Use a grain value to identify targets, the syntax for
The target is the grain key followed by a
-- grain-pcre Instead of using shell globs to evaluate the target
Use a grain value to identify targets, the syntax for
The target is the grain key followed by a pcre regular
[root@salt_master ~] # salt-G 'os:CentOS' test.ping # #-G means to use grains for matching, and the parameter must follow salt.
Minion.saltstack.com:
True
Minion1.saltstack.com:
True
[root@salt_master] # salt-G 'fqdn:minion.saltstack.com' test.ping
Minion.saltstack.com:
True
[root@salt_master ~] # salt 'minion.saltstack.com' grains.get fqdn # # check the fqdn of minion.saltstack.com to locate
Minion.saltstack.com:
Minion.saltstack.com
So through the grains plug-in can be achieved in any condition of the robot to execute the same command, can be matched through grains.
If the built-in grains does not meet our needs, we can also customize the grains, and customize the grains by modifying the configuration file on the minion side
# minion #
[root@salt_minion ~] # vim / etc/salt/minion # cut to the end of the file
Grains:
Roles: nginx
Env: prod
[root@salt_minion ~] # / etc/init.d/salt-minion restart
Stopping salt-minion daemon: [OK]
Starting salt-minion daemon: [OK]
[root@salt_master] # salt-G 'env:prod' test.ping
Minion.saltstack.com:
True # successful match
[root@salt_master] # salt-G 'roles:nginx' test.ping
Minion.saltstack.com:
True
If you find it inconvenient to write grains in the minion configuration file, you can also write it in the / etc/salt/grains file.
The premise is that it cannot conflict with the configuration file.
# difference: adding to the configuration file / etc/salt/minion must be specified as grains,. The second method is to create a file named grains under / etc/salt/. Instead of specifying grains, just write #
[root@salt_minion ~] # vim / etc/salt/grains
Cloud: openstack
[root@salt_minion ~] # / etc/init.d/salt-minion restart # must be restarted after being added
Stopping salt-minion daemon: [OK]
Starting salt-minion daemon: [OK]
[root@salt_minion ~] # cat / etc/salt/grains
Cloud: openstack
[root@salt_minion ~] # tail / etc/salt/minion
#
Mysql.host: '10.20.23.209'
Mysql.user: 'salt'
Mysql.pass: 'salt'
Mysql.db: 'salt'
Mysql.port: 3306
Grains:
Role: nginx
Env: prod
# match:
[root@salt_master] # salt-G 'cloud:openstack' test.ping
Minion.saltstack.com:
True
# using salt'* 'saltutil.sync_grains to refresh grains, there is no need to restart the minion. Test: #
[root@salt_minion ~] # vim / etc/salt/grains
Cloud: openstack
Test: salt # add this line, save exit, and do not restart the minion side
# cut to master #
[root@salt_master ~] # salt'* 'saltutil.sync_grains
Minion.saltstack.com:
Minion1.saltstack.com:
[root@salt_master] # salt-G 'test:salt' test.ping
Minion.saltstack.com:
True
3. Match minion in top.sls
[root@salt_master ~] # vim / srv/salt/top.sls # # finally, add the following:
'role:nginx':
-match: grain
-init.pkg
[root@salt_master ~] # cat / srv/salt/top.sls
Base:
'(minion | minion1) .saltstack.com':
-match: pcre
-init.pkg
-init.limit
# 'role:nginx':
#-match: grain
#-init.pkg # comment out first, use the # sign, and know how to write it.