Article From:https://www.cnblogs.com/jimmy-xuli/p/9125053.html
  • Saltstackdata system

It is divided into Grains and Pillar

SaltStack data system

Grains (Grain)
Pillar (Column)

 

One.Grains
Grains:Static data collection of Minion local related information when Minion starts.
Operating system version, kernel version, CPU, memory, hard disk. Equipment model. serial number
Note: the data will not change without restarting minion. For example, different packages can be installed according to the system’s CentOS server and the system’s RedHat environment.

GrainsFunction: 1. collection of asset information 2. information query
Official document: https://docs.saltstack.com/en/getstarted/overview.html

1)Information management, including asset management;

salt 'linux-node1*' grains.ls  # List all hosts of ID and linux-node1, all key of grains.Salt'linux-node1*'grains.items grains.items lists the details of the host, which can be used for asset management.Salt '*' graiThe system version of all hosts is listed by ns.item OS[root@7mini-node1], "salt" * * "grains.item fqdn_ip4", which lists the IP addresses of all hosts.7mini-Node1:- ----Fqdn_ip4:- 10.0.0.117mini-node2:- ----Fqdn_ip4:- 10.0.0.12

2)For target selection; (query specific ID hosts, query system version CentOS hosts and other scenarios)  

salt -G 'os:Centos' test.ping  # All host systems are CentOS version Ping testsSalt -G'os:Centos'cmd.run'echo 123' 123', all host systems execute CentOS'echo version 123'

3)Use in configuration management

Way one: Modify the configuration file VIM /etc/salt/minion

Restart systemctl restart salt-minion

Through the custom item, you can restart all the hosts with Apache roles.

salt '*' grains.item roles  # Get the roles of all hostsSalt -G'roles:apache'cmd.run'systemctl restart httpd' roles all host roles is Apache execution command systemCTL restart httpd

Mode two:(The use of the production environment is also a common configuration.

Modify configuration file VIM /etc/salt/grains, write

cloud: openstack

Restart systemctl restart salt-minion

salt '*' grains.item cloud  # Get the cloud of all hosts

Modify the method that /etc/salt/grains does not restart the service. The refresh command is as follows (Note: mode one and mode two modify the configuration file, which can be used without restarting the service).

salt '*' saltutil.sync_grains

  

Two.Pillar

PillarIt is one of the most important systems of Salt. It is also a dictionary format, like the structure of grains, and data is stored in key/value format. In the design of Salt, Pillar uses a separate encryption sessiion. Can be used to provide a development interface,It is used to define data on the master side, then use it in minion, and generally transmit sensitive data, such as SSH key, encryption certificate, etc.

pillarSimilar to States, it is made up of SLS files with an entry file top.sls, which is associated with other SLS files, the default path is in /srv/pillar, and pillar_r inside pillar_rOots: specifies the location.

pillarWhat is the effect on earth? Now let me introduce a simple example, and you will see.

Using ZABBIX to monitor the new server (10), zabbix_agentd.conf needs to be distributed to the monitored host, the file of the hostname IP in this file is different, we can not write 10 points configuration file! So how to make hostnaWhen me was distributed, it was modified according to the monitored host IP. The render is used for rendering. The default renderer is Jinja, which supports the for in loop judgment. The format is {%…%} {% end*%}, so salt will render Jinja first.And then give it to yaml.

(1)How to define Pillar data

  • a.masterThe configuration file defines pillar:
    By default, all data in the master configuration file is added to Pillar and is available for all minion. If you want to disable this default value, you can add the following data in the master configuration file.
#The default items of pillar is empty, and /etc/salt/master needs to be modified.[root@linux-node1 -] salt '*' pillar.itemsLinux-node1.exampLe.com:- ----Linux-node2.example.com:- ----[root@linux-node1 -] VIM /etc/salt/mAster#pillar_opts: False opens the item and modifies it into TruePillar_opts: True[root@linux-node1 -] systemctl restart sAlt-master[root@linux-node1 -] salt '*' pillar.items
  • b.Using the SLS file to define the Pillar
    PillarUse a SLS file similar to State. The Pillar file is placed in the directory defined by pillar_roots in the master configuration file. The examples are as follows:
[root@linux-node1 ~]# vim /etc/salt/master
pillar_roots:
  base:
    - /srv/pillar/base
  prod:
    - /srv/pillar/prod

#This code defines the Pillar file in the base environment under the /srv/pillar/base directory. The Pillar file in prod environment is kept under /srv/pillar/prod.[root@linux-nodE1 -] MKDIR -p /srv/pillar/{base, prod}[root@linux-node1 -] tree /srv/pillar//srv/pillar/BasEProd[root@linux-node1] systemctl restart salt-masterThe pillar file in the base environment is created as Apache[root@linUx-node1 -] VIM /srv/pillar/base/apache.sls{% if grains['os'] = ='CentOS'%}Apache: httpd{% elifGrains['os'] = ='Debian'%}Apache: apache2{% ENDIF%}Similar to State, Pillar also has top file, and uses the same matching method to apply data.To minion. The examples are as follows:[root@linux-node1 -] VIM /srv/pillar/base/top.slsBase:'*':- Apache[root@lInux-node1 -] salt '*' pillar.itemsLinux-node1.example.com:- ----Apache:HttPDLinux-node2.example.com:- ----Apache:HttpdUnder the base environment, reference pillar[root@linux-node1 -] VIM /srv/salt/base/web/apache.slsApache-install:Pkg.installed:- name:['apache'] x)]Apache-service:Service.running:- name: (= (pillar['apache']))- enable: TRue[root@linux-node1 -] salt '*' state.highstate

Summary:
1.pillarLike the state, pillar_roots is configured in master.
2.To configure /srv/pillar/base, write a apache.sls.
3.pillarIt must be specified in top file, and all minion specified in top.sls needs to be executed in apache.sls under base environment.
4.Check whether you can get pillar value before using salt: * ‘pillar.items
5.Change the status configuration and change the name to a pillar reference, which is a Jinja syntax.

 

Three, Grains VS Pillar

 

  

  

 

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *