Saltstack (v) state management of Saltstack

Source: Internet
Author: User
Tags saltstack

One, YAML syntax

  First look at Yaml, the default SLS file for renderer is Yaml renderer. Yaml is a markup language with a lot of powerful features. Salt uses a small subset of YAML to map very common data structures like lists and dictionaries. The job of Yaml renderer is to compile the structure of the YAML data format into a Python structure for use with salt.

YAML syntax has three caveats, as follows:

  1, use white space characters to indent the structure of the file, but cannot use tab

  2, note with # number

3, strings usually do not use quotation marks, if necessary, you can use single or double quotation marks. When using double quotation marks to denote a string, special characters can be defined by a backslash (\)

Specific usage :

1,List: Short slash (-) +White space characters-Henry-Sina equivalent to: ['Henry','Sina']
2, Dictionary: Key and value by colon (:) +white space characters separated name:henry site:http://www.saltstack.cn/Equivalent to: {'name':'Henry','site':'http://www.saltstack.cn/'} 3, Multilayer structure: Representing levels by indentation-Henry:-Sites:-Blog:http://www.saltstack.cn/Equivalent to: [{'Henry':[{'Sites':[{'Blog':'http://www.saltstack.cn/'}]}]}]

Website address: http://docs.saltstack.cn/topics/yaml/index.html

Two, Jinja

Salt By default uses the JINJA2 template system to generate YAML, a template engine for Python language development, similar to the Djingo template system

Usage of the Jinja module:

1Variable: {{foo}}, if the variable is a dictionary, you can use {{Foo.bar}} or {foo['Bar']}}      2 Note: {# comment #}3  for: # #可以使用for循环取值4{% forEachiteminchItems%}5 {{Eachitem}}6{% ENDFOR%}7 if: # #可以做if条件判断8{%ifGFW%}9Welcome to China!Ten{%elifNot Internet%} OneWelcome to North Korea! A{%Else%} - Freedom -{% ENDIF%}

Three, State Tree

Top file: Configuration Management Portal file, similar to pillar, specifies that minions need to complete those configuration management, default to Top.sls
  Note: When defining TOP.SLS, you will find Apache/init.sls first, if not, you will find Apache.sls
Base
‘*‘:
-Apache
SLS Module Usage point (.) To divide, as Apache.install equals Salt://apache/install.sls or Salt://apache/install/init.sls.
Include/extend:sls files can be referenced and extended via include and Extend
The ID in SLS must be unique, and the same class of state management modules under that ID can only have one, so-called Slsid is the first name defined at the beginning when the SLS file is defined.
0.17.0 Salt adds the State_auto_order parameter (the default value is true) so that state executes in a top-down order without requisites

Four, requisites (equivalent to SLS Process control, the one that executes first, the after execution, etc.) 

Require: When the state executes, the state must be executed first
Require_in: opposite to require position
Watch: In addition to require, it also monitors the state of the dependent State, and responds if the status changes (such as monitoring file changes and restarting the service immediately after changes occur)
Watch_in: opposite to watch position
prereq:0.16.0 new features that will check the state of a dependent state via the Test=true interface and, if the status changes, execute
Prereq_in: On the contrary

Five, the following is a description of the use of three modules, more module usage please refer to the official website

Website address: https://docs.saltstack.com/en/latest/ref/states/all/

 Package Status Management module :
Module Name: pkg
Function: Management package status, depending on the operating system, select the corresponding installation method
Basic operation:

pkg.installed:     #确保软件包已安装, if no installation is installed Pkg.latest:        #确保软件包是最新版本, if not, upgrade Pkg.remove:        #确保软件包已卸载, if previously installed, Uninstall Pkg.purge:         #除remove外, its configuration file is also deleted

 File Status Management module:
Module Name: File
Features: Managing File Status
Basic usage:

file . Managed:   #保证文件存在并且为对应的状态 file. recurse:   #保证目录存在并且为对应的状态 file. Absent:  #确保文件不存在 If there is a delete operation  

Service Status Management module:
Module Name: Service
Function: Manage Service status
Basic usage:

Service.running        #确保服务处于运行状态service. Enabled        #确保服务会开机自动启动service. Disabled    # Ensure that the service does not boot from the boot service.dead        #确保服务当前没有运行

Six, instance operations

(i), install Apache

Top.sls Base:'*':        -Apacheinit.sls Apache: # #slsID pkg.installed: # #安装httpd-name:httpdfile. Managed:-Name:/etc/httpd/conf/httpd.conf # #拷贝/srv/salt/apache/httpd.conf to minion/etc/httpd/conf/httpd.conf-Source:salt://apache/httpd.conf-require: # #检查apache有没有安装成功-Pkg:apache service.running:-enable:true-name:httpd-Watch: # #watch检测配置文件, restart the service if a change occurs-Pkg:apache-file: Apache

Run Configuration management: Salt ' * ' State.sls Apache
Salt ' * ' salt.highstate test=true #test =true for testing syntax

(b), Apache needs to monitor port 8080, the code is as follows:

 First Modify the Apache master configuration file, modify the configuration file to listen {{port}}, after modification, you can define the apache:pkg.installed directly in the SLS file :-name:httpdfile. Managed:-Name:/etc/httpd/conf/httpd.conf-Source:salt://apache/httpd.conf-require:-Pkg:apache-Template:jinja # #调用jinja模板渲染-Context: # #这里可以是context或default效果一样 Port:8080service.running:-enable:true-name:httpd-Watch:-Pkg:apache-file: Apache If more than one host listens to a different port, the specific changes are as follows: apache:pkg.installed:-name:httpdfile. Managed:-Name:/etc/httpd/conf/httpd.conf-Source:salt://apache/httpd.conf-require:-Pkg:apache-Template:jinja-defaults: {%ifGrains.ID=="10.13.41.80"%} port:8080      {%elifGrais.ID=="10.13.41.81"%} port:8081      {%Else%} port: the      {% ENDIF%} service.running:-enable:true-name:httpd-Watch:-Pkg:apache-file: Apache

In order for SLS files to be non-mixed with business data, business data should be stored independently, and it is time for the pillar to take the stage.

/srv/pillar/apache/Init.slsapache: {%ifGrains.ID=="Test"%} port:8081  {%elifGrains.ID=="test1"%} port:8082  {%Else%} port: the  {% ENDIF%}
/srv/pillar/Top.sls Base:'*': -Apachesalt'*'Saltutil.regresh_pillar # #刷新pillarsalt'*'pillar.get Apache:port # #取minion的pillar信息/srv/salt/apache/Deploy.sls
- Template:jinja
-
port:{{salt['pillar.get'('apaceh:port'), )}}

State management of Saltstack is introduced here.

Saltstack (v) state management of Saltstack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.