This is the 21st day of my participation in the August More Text Challenge

A lifelong learner, practitioner and sharer committed to the path of technology, a busy and occasionally lazy original blogger, an occasionally boring and occasionally humorous teenager.

Welcome to dig friends wechat search “Jie Ge’s IT journey” attention!

How to collect information about a controlled host in the Saltstack component?

1. Saltstack collects information about the controlled host

1.1 What are Grains?

Grains is one of the most important components of Saltstack. The role of Grains is to collect the basic information of the controlled host, which is usually some static data, including CPU, kernel, operating system, virtualization, etc., and can be flexibly customized on the server side according to these information. Administrators can use these information to make personalized Settings for different businesses. The official provides: Grains is used to distinguish different Minion and make different configurations.

1.2 Application Scenarios

  • Used in state to configure the management module
  • Used in target to match minion, such as the -g option for matching the operating system
  • For information query, Grains saves the collected client details

The CentOS distribution host will be matched by host: {{grains[‘ XXX ‘]}}. Take the host saltstack_web1group_1 (CentOS 6.5) as an example and get host: saltstack_web1group_1. If the command line matches the controlled end whose operating system version is CentOS, the command line can be filtered by the -g parameter.

[root@saltstack-master ~]# salt -G 'os:CentOS' test.ping
saltstack_web1group_1:
True
saltstack_web1group_2:
True
Copy the code

1.3 Common operation commands of Grains

Match minion with kernel version 2.6.32-504.el6.x86_64 and run ‘uname -a’

[root@saltstack-master ~]# salt -G 'kernelrelease:2.6.32-431.el6.x86_64' cmd.run 'uname -a'
saltstack_web1group_1:
Linux saltstack_web1group_1 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
saltstack_web1group_2:
Linux saltstack_web1group_2 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Copy the code

Obtain information of all minion grains items

[root@saltstack-master ~]# salt '*' grains.ls saltstack_web1group_1: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gpus - Host -------------- Ignore some content ------------------Copy the code

Of course, you can also get single host data, such as the operation release.

[root@saltstack-master ~]# salt 'saltstack_web1group_1' grains.item os
saltstack_web1group_1:
----------
os:
CentOS
Copy the code

1.4 Defining data of Grains

There are two ways to define the data of Grains, one of which is to customize the configuration file in Minion, and the other is to realize the data through the API of the Master extension module. The difference is that the module is more flexible and can be dynamically defined through Python programming, while the configuration file is only suitable for relatively fixed keys and values.

[root@saltstack-master ~]# salt 'saltstack_web1group_1' grains.items saltstack_web1group_1: ---------- SSDs: Biosreleasedate: 05/19/2017 biosversion: 6.00 cpu_flags: - fpu - vme - DE -- -- -- -- -- -- -- -- -- -- -- -- -- -- ignore part -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

1.5 The host of the controlled end customizes data of Grains

Log in to a controlled host (such as SN2013-08-022) using SSH. The customized configuration file path is /etc/salt/minion and the parameter is default_include:minion.d/*conf

Create a file “/ etc/salt/minion. D/hostinfo. Conf 】

[root@saltstack_web1group_1 ~]# cd /etc/salt/minion.d/ [root@saltstack_web1group_1 minion.d]# vim hostinfo.conf grains: roles: - webserver - memcache deployment: datacenter4 cabinet: 14 [root@saltstack_web1group_1 minion.d]# service salt-minion restart Stopping salt-minion daemon: [confirm] Starting salt-minion daemon:Copy the code

Salt ‘saltstack_web1group_1’ grains. Itemroles deployment Cabinet, check the configured key and value.

[root@saltstack-master ~]# salt 'saltstack_web1group_1' grains.item roles deployment cabinet
saltstack_web1group_1:
----------
cabinet:
14
deployment:
datacenter4
roles:
- webserver
- memcache
Copy the code

1.6 Expansion module of main control end customizes data of Grains

Python code is first written on the master, then the Python file is synchronized to the controlled, and finally the refresh takes effect (that is, the Python source file is compiled into bytecode PYC). Generate _grains directory (see file_roots in /etc/salt/master configuration file, default base configuration is/SRV /salt). Run install -d/SRV /salt/_grains to start code. Obtain real-time data of the maximum number of open files (Ulimit-n) allowed by the controlled host system.

[root@saltstack-master ~]# install -d /srv/salt/_grains [root@saltstack-master ~]# cd /srv/salt/_grains [root@saltstack-master _grains]# vim grains_openfile.py #! /usr/bin/env python # -* -coding :utf-8 -* -import OS,sys,commands Def Grains_openfile() def Grains_openfile(): "Return OS Max open file of Grains value" "Grains = {} # Try: getulimit = commands.getStatusOutput ('source /etc/profile; ulimit -n') except Exception,e: pass if getulimit[0]==0: _open_file=int(getuLIMIT [1]) grains['max_open_file'] = _open_file Open_file is the value of Return GrainsCopy the code

Grains_openfile () defines a function to get the maximum number of open files. The name of the function is not required, but conforms to Python function naming rules.

Grains ={} Initialize a grains dictionary, Variables must be named with grains so that SaltStack recognizes the Linux Ulimit that will be fetched by GRAINS [‘max_open_file’]=_open_file The result value of -n assigns grains[‘max_open_file’], where “max_open_file” is the element of grains and _open_file is the value of grains.

Synchronizing modules at the main control terminal: Salt ‘saltSTACK_web1group_1’ saltutil. sync_ALL ‘saltSTACK_web1group_1’ saltutil.sync_all ‘saltutil. salt ‘saltstack_web1group_1’ saltutil.

[root@saltstack-master _grains]# salt 'saltstack_web1group_1' saltutil.sync_all saltstack_web1group_1: ---------- beacons: grains: - grains.grains_openfile modules: output: renderers: returners: sdb: states: utils: [root @ saltstack_web1group_1 minion. D] # ll/var/cache/salt/minion extmods/grains/total amount of 4 - rw. -- -- -- -- -- -- -- 1 root root on March 20, 774 Blessed grains_openfile. Py [root @ saltstack_web1group_1 minion. D] # ll/var/cache/salt/minion/files/base / _grains/total amount of 4 -rw-------. 1 root root 774 March 20 16:36 grains_openfile.pyCopy the code

The/var/cache/salt/minion/extmods/grains/final location for extension module file, after the refresh module will be in the same directory to generate bytecode pyc; The/var/cache/salt/minion/files/base / _grains/deposit for the temporary position

Refresh module salt ‘saltstack_web1group_1’ Sys. Reload_modules in/var/cache/salt/minion/extmods/grains/position of a compiled bytecode file grains_openfile. Py files, for the python executable format.

[root@saltstack-master _grains]# salt 'saltstack_web1group_1' sys.reload_modules saltstack_web1group_1: True [root @ saltstack_web1group_1 minion. D] # ll/var/cache/salt/minion extmods/grains/total amount of 4 - rw. -- -- -- -- -- -- -- 1 root root, 774 3月 20 16:36 grains_openfile.pyCopy the code

The verification result is that the information of Grains can be viewed on the main control side.

Execute salt 'saltstack_web1group_1' grains. Item max_open_fileCopy the code

The result shows “max_open_FILE :65535”, which is the information of the previously customized host Grains.

[root@saltstack-master _grains]# salt 'saltstack_web1group_1' grains.item max_open_file
saltstack_web1group_1:
----------
max_open_file: 65535
Copy the code

Recommended reading

Saltstack centralized management platform installation

Execute commands remotely using Saltstack

Follow me to learn Saltstack common modules and apis

In this paper, to the end.


Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!

By the way, please give me some free attention! In case you get lost and don’t find me next time.

See you next time!