Grains components of Saltstack series 4:Saltstack
Grains description
Grains is one of the most important components of Saltstack. The function of grains is the basic information of the controlled host of mobile phone, which is usually some static data, including CPU, kernel, operating system, virtualization and so on. On the server side, it can be flexibly customized according to this information, and administrators can use this information to customize different services.
Example: (janja template)
{% if grains ['os'] = =' Ubuntu'%} host: {{grains ['host']}} {% elif grains [' os'] = 'CentOS'%} host: {{grains [' fqdn]}} {% endif%} grains common operation commands
Match hosts with kernel version 2.6.32-358.14.1.el6.x86_64
Salt-G 'kernelrelease:2.6.32-358.14.1.el6.x8634 'cmd.run' uname-a'
Get grains entry information for all hosts
Salt'* 'grains.ls defines grains data
There are two ways to define grains data, one is to customize the configuration file on the controlled host, and the other is to implement it through the main control side extension module API. The difference is that modules are more flexible and can be dynamically defined through Python programming, while
Profiles are only suitable for relatively fixed keys and values. The following are explained separately:
1. Grains data customized by the host of the controlled end
[/ etc/salt/minion]
Default_include:minion.d/*.conf # Custom grains profile path
Grains configuration file: [/ etc/salt/minion.d/hostinfo.conf]
Grains: roles:-webserver-memcache deployment: datacenter4 cabinet: 13
Restart salt-minion to take effect: service salt-minion restart
Verify:
Salt 'wx' grains.item roles deployment cabinet
Results:
Salt 'wx' saltutil.sync_all
Result: the files are synchronized to the cache directory of the controlled terminal.
/ var/cache/salt/minion/extmods/grains/grains_openfile.py/var/cache/salt/minion/files/base/_grains/grains_openfile.py# Note: / var/cache/salt/minion/extmods/grains/ is the final storage location of the extension module files. After refreshing the module, the bytecode pyc;/var/cache/salt/minion/files/base/_grains/ will be produced in the same path as the temporary storage location.
Refresh module:
Salt 'wx' sys.reload_modules# generates pyc bytecode / var/cache/salt/minion/extmods/grains/grains_openfile.py/var/cache/salt/minion/extmods/grains/grains_openfile.pyc/var/cache/salt/minion/files/base/_grains/grains_openfile.py
Verify:
Salt 'wx' grains.item max_open_file
Results:
Wx:- max_open_file: 1024
Reference:
According to Liu Tiansi's "Python Automation Operation and maintenance Technology and Best practices"