STATES TUTORIAL, PART 1 - BASIC USAGE(第二部分)
Posted 孤独的海浪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于STATES TUTORIAL, PART 1 - BASIC USAGE(第二部分)的主要内容,如果未能解决你的问题,请参考以下文章
Transparency Tutorial with C# - Part 3
Data Mining Tutorial -- 简明译文Part 03
Recurrent Neural Networks Tutorial, Part 2 – Implementing a RNN with Python, Numpy and Theano(