STATES TUTORIAL, PART 1 - BASIC USAGE(第二部分)

STATES TUTORIAL, PART 1 - BASIC USAGE(第二部分)

SETTING UP THE SALT STATE TREE
在master设置file_roots

示例:

1 file_roots:
2   base:
3     - /srv/salt

重启master
  pkill salt-master
  salt-master -d


PREPARING THE TOP FILE

配置top入口文件

1 base:
2   ‘*‘:
3     - webserver

说明:top文件可以起到隔离环境的作用,默认环境为base,在base环境下对指定的minion进行匹配,匹配方式支持非常广泛。



CREATE AN SLS FILE
示例:

1 apache:                 # ID declaration,可以为任意值,默认作为函数的参数
2   pkg:                  # state declaration,指定状态模块
3     - installed         # function declaration,指定对应状态模块的函数


INSTALL THE PACKAGE

在定义好top文件和编写好sls文件之后,minion端会从master端下载top文件,并进行匹配,匹配则执行定义的配置内容。
示例:
  salt ‘*‘ state.apply

注意:state.apply与state.highstate的区别

1 state.apply invoked without any SLS names will run state.highstate
2     当运行state.apply没有指定sls文件的时候,会运行state.highstate,也就是运行top中定义的所有sls文件。
3 state.apply invoked with SLS names will run state.sls
4     当运行state.apply指定了sls文件的时候,会运行state.sls


SLS File Namespace

1 1、.sls的文件后缀是被去掉的,譬如webserver.sls被引用为webserver
2 2、webserver.dev被识别为webserver/dev.sls,webserver_1.0.sls不能被识别,webserver_1.0被识别为webserver_1/0.sls
3 3、webserver/init.sls被识别为webserver
4 4、如果webserver.sls和webserver/init.sls同时存在,webserver/init.sls将被忽略


Troubleshooting Salt

故障排查

(1)Turn up logging
    打开日志

    salt-minion -l debug

(2)Run the minion in the foreground
    前台运行,不使用-d参数

1 salt-minion
2 salt -t 60
3 salt-minion -l debug        # On the minion
4 salt ‘*‘ state.apply -t 60  # On the master

##############################################################################################################

STATES TUTORIAL, PART 2 - MORE COMPLEX STATES, REQUISITES

更复杂的state文件和组件

CALL MULTIPLE STATES

为一个安装apache包添加一个依赖条件

1 apache:
2   pkg.installed: []
3   service.running:
4     - require:
5       - pkg: apache


REQUIRE OTHER STATES

建立state状态之间的依赖关系

示例:

 1 apache:
 2   pkg.installed: []
 3   service.running:
 4     - require:
 5       - pkg: apache
 6
 7 /var/www/index.html:                        # ID declaration
 8   file:                                     # state declaration
 9     - managed                               # function
10     - source: salt://webserver/index.html   # function arg
11     - require:                              # requisite declaration
12       - pkg: apache                         # requisite reference

require vs. watch

  这两个Requisite declaration,由于不是每一个state支持watch,service state支持watch,它将根据watch设定的条件重启服务。

示例:

 1 /etc/httpd/extra/httpd-vhosts.conf:
 2  file.managed:
 3    - source: salt://webserver/httpd-vhosts.conf
 4
 5 apache:
 6   pkg.installed: []
 7   service.running:
 8     - watch:
 9       - file: /etc/httpd/extra/httpd-vhosts.conf
10     - require:
11       - pkg: apache

###############################################################################################################

STATES TUTORIAL, PART 3 - TEMPLATING, INCLUDES, EXTENDS

模板,include,extend


TEMPLATING SLS MODULES

在sls中使用模板
示例:

1 {% for usr in [‘moe‘,‘larry‘,‘curly‘] %}
2 {{ usr }}:
3   user.present
4 {% endfor %}

渲染后的结果:

1 moe:
2   user.present
3 larry:
4   user.present
5 curly:
6   user.present

示例1:

 1 {% for usr in ‘moe‘,‘larry‘,‘curly‘ %}
 2 {{ usr }}:
 3   group:
 4     - present
 5   user:
 6     - present
 7     - gid_from_name: True
 8     - require:
 9       - group: {{ usr }}
10 {% endfor %}


USING GRAINS IN SLS MODULES

在sls文件中使用grains

1 apache:
2   pkg.installed:
3     {% if grains[‘os‘] == ‘RedHat‘ %}
4     - name: httpd
5     {% elif grains[‘os‘] == ‘Ubuntu‘ %}
6     - name: apache2
7     {% endif %}

USING ENVIRONMENT VARIABLES IN SLS MODULES

在state文件中使用salt[‘environ.get‘](‘VARNAME‘)的方式设置环境变量
示例:

1 MYENVVAR="world" salt-call state.template test.sls
2
3 Create a file with contents from an environment variable:
4   file.managed:
5     - name: /tmp/hello
6     - contents: {{ salt[‘environ.get‘](‘MYENVVAR‘) }}

environ模块的使用链接参考:
  https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.environ.html
譬如获取minion端的环境变量信息:
  salt ‘*‘ environ.items

可以设置错误检查逻辑:

 1 {% set myenvvar = salt[‘environ.get‘](‘MYENVVAR‘) %}
 2 {% if myenvvar %}
 3
 4 Create a file with contents from an environment variable:
 5   file.managed:
 6     - name: /tmp/hello
 7     - contents: {{ salt[‘environ.get‘](‘MYENVVAR‘) }}
 8
 9 {% else %}
10
11 Fail - no environment passed in:
12   test.fail_without_changes
13
14 {% endif %}


CALLING SALT MODULES FROM TEMPLATES

在模板中使用salt执行模块
示例:

1 moe:
2   user.present:
3     - gid: {{ salt[‘file.group_to_gid‘](‘some_group_that_exists‘) }}

譬如通过用户名获取组名网卡名等:
  salt ‘XXX‘ file.group_to_gid username
  salt[‘network.hw_addr‘](‘eth0‘)


ADVANCED SLS MODULE SYNTAX

sls文件语法进阶

INCLUDE DECLARATION
示例:

python/python-libs.sls:

1 python-dateutil:
2   pkg.installed

python/django.sls:

1 include:
2   - python.python-libs
3
4 django:
5   pkg.installed:
6     - require:
7       - pkg: python-dateutil


EXTEND DECLARATION

extend可以修改之前定义好的sls文件内容,下例是添加一个apache虚拟主机配置。

示例:

apache/apache.sls:

1 apache:
2   pkg.installed

apache/mywebsite.sls:

 1 include:
 2   - apache.apache
 3
 4 extend:
 5   apache:
 6     service:
 7       - running
 8       - watch:
 9         - file: /etc/httpd/extra/httpd-vhosts.conf
10
11 /etc/httpd/extra/httpd-vhosts.conf:
12   file.managed:
13     - source: salt://apache/httpd-vhosts.conf


NAME DECLARATION

可以通过name声明重写一个ID声明
示例:
apache/mywebsite.sls:

 1 include:
 2   - apache.apache
 3
 4 extend:
 5   apache:
 6     service:
 7       - running
 8       - watch:
 9         - file: mywebsite        #引用一个ID声明
10
11 mywebsite:
12   file.managed:
13     - name: /etc/httpd/extra/httpd-vhosts.conf
14     - source: salt://apache/httpd-vhosts.conf


NAMES DECLARATION

多个name声明可以覆盖ID声明,并可以做到消除冗余状态

1 stooges:
2   user.present:
3     - names:
4       - moe
5       - larry
6       - curly

时间: 2024-08-10 11:53:54

STATES TUTORIAL, PART 1 - BASIC USAGE(第二部分)的相关文章

STATES TUTORIAL, PART 4(第三部分)

STATES TUTORIAL, PART 4(第三部分) 使用file_roots配置state文件环境 SALT FILESERVER PATH INHERITANCE salt文件服务器路径解密 示例: 1 # In the master config file (/etc/salt/master) 2 file_roots: 3 base: 4 - /srv/salt 5 - /mnt/salt-nfs/base 注意: 当定义的多路径之中存在相同的文件引用路径,则以先匹配到的为准. #

github basic usage

1. create a new accout, create orginazation, create repo 2. install git in your local pc Note: you can create ssh key to avoid username/password input for github operation https://help.github.com/articles/generating-ssh-keys https://help.github.com/a

something about basic usage of vector,queue

1.for a two dimension vector, we must assign at least the first dimension of the vector 2.each dimension of an inner vector can be different 3.if you don't want to set a volume for vector<vector<int> > for uncertainness, you can just use a met

pcl之basic usage

pcl之basic usage width(int) two meanings: it can specify the total number of points in the cloud for unorganized point cloud datasets; it can specify the width (total number of points in a row) of an organized point cloud dataset. The advantages of an

opnet tutorial of mm1 &amp; basic process

Packet streams are used to connect each of the modules in the Node Editor. The name of the underlying process model, acb_fifo, reflects its major characteristics: "a" indicates that it is active (that is, it acts as its own server), "c"

STATES TUTORIAL(第四部分)

MOD AGGREGATE STATE RUNTIME MODIFICATIONS 略 ALTERING STATES 略 FILE STATE BACKUPS 可以在多个地方设置minion端的文件备份.示例: backup_mode: minion 或 1 /etc/ssh/sshd_config: 2 file.managed: 3 - source: salt://ssh/sshd_config 4 - backup: minion BACKED-UP FILES 备份文件的位置位于mi

STATES TUTORIAL(第五部分)

EXTENDING EXTERNAL SLS DATA 扩展额外的sls数据,当一个sls文件需要被局部修改的时候,譬如当一个state文件需要添加watch条件的时候. THE EXTEND DECLARATION 通过extend扩展sls数据,一般通过include导入一个ID declaration.示例: 1 include: 2 - http 3 - ssh 4 5 extend: 6 apache: 7 file: 8 - name: /etc/httpd/conf/httpd.c

3.0 Basic Usage of Class

Well, this week I tried to use some class things. Following are parts of my exercising codes. Definig my class named Time //note that cnt is a static member of this class to count the number of //instances created. Constructors & Overloading //as you

lua basic usage

#pragma comment (lib, "lua5.1.lib") #include <conio.h> extern "C"{ #include "lua.h" #include "lualib.h" #include "lauxlib.h" } int main() { // Create and start our environment lua_State *EnvOne = lua