SLS(代表SaLt State文件)是Salt State系统的核心。SLS描述了系统的目标状态,由格式简单的数据构成。这经常被称作配置管理
top.sls 是配置管理的入口文件,一切都是从这里开始,在master 主机上,默认存放在/srv/salt/目录.
top.sls 默认从 base 标签开始解析执行,下一级是操作的目标,可以通过正则,grain模块,或分组名,来进行匹配,再下一级是要执行的state文件,不包换扩展名。
创建 /srv/salt/top.sls
base:
'*':
- webserver
base:
group1:
- match: nodegroup
- webserver
base:
'os:Fedora':
- match: grain
- webserver
准备好top.sls文件后,编写一个state文件
apache: # 标签定义
pkg: # state declaration
- installed # function declaration
最后可以在终端中执行命令来查看结果:
salt '*' state.highstate
或附件 test=True参数 测试执行
salt '*' state.highstate -v test=True
主控端对目标主机(targeted minions)发出指令运行state.highstatem模块,目标主机首先会对top.sls下载,解析,然后按照top.sls内匹配规则内的定义的模块将被下载,解析,执行,然后结果反馈给 master.
注意在以上的例子中,SLS文件 webserver.sls 被简称为webserver. SLS文件命名空间有如下几条基本的规则:
下面是一个state多文件示例,
apache/init.sls
apache/httpd.conf
ssh/init.sls
ssh/server.sls
ssh/banner
ssh/ssh_config
ssh/sshd_config
创建一个引用这些目录的 server.sls
server:
- apache
- ssh
/srv/salt/apache.sls
apache:
pkg:
- installed
service:
- running
- require:
- pkg: apache
使用 include 可以包换有state文件而不必重新写
include:
apache
/srv/salt/apache.sls
apache:
pkg:
- installed
service:
- running
- require:
- pkg: apache
extend默认是替换引用文件的某个字段的属性,如例
include:
- apache
extend:
apache
pkg:
- name: vim
- installed
当extend与watch,或require结合使用的时候,则是扩展某个字段的属性,如例:
include:
- apache
extend:
apache
service:
- watch:
- file: /etc/redis.conf
httpd: # maps to "name"
pkg:
- installed
file: # maps to State module filename
- managed: # maps to the managed function in the file State module
- name: /etc/httpd/conf/httpd.conf # one of many options passed to the manage function
- source: salt://httpd/httpd.conf
- require:
- pkg: httpd
redis:
pkg:
- latest
file.managed:
- source: salt://redis/redis.conf
- name: /etc/redis.conf
- require:
- pkg: redis
service.running:
- enable: True
- watch:
- file: /etc/redis.conf
- pkg: redis
vim:
pkg.installed:
- order: 1
想让某个state最后一个运行,可以用last
{% for item in ['tmp','test'] %}
/opt/{{ item }}:
file.directory:
- user: root
- group: root
- mode: 755
- makedirs: True
{% endfor %}
```markdown
httpd:
pkg.managed:
{% if grains['os'] == 'Ubuntu' %}
- name: apache2
{% elif grains['os'] == 'CentOS' %}
- name: httpd
{% endif %}
- installed
/opt/test.conf
{% if grains['os'] == 'Ubuntu' %}
host: {{ grains['host'] }}
{% elif grains['os'] == 'CentOS' %}
host: {{ grains['fqdn'] }}
{% endif %}
```markdown
/opt/test.conf:
file.managed:
- source: salt://test.conf
- user: root
- group: root
- mode: 644
- template: jinja