文章目录
1. include的使用
引用多个SLS的状态可以用include模块实现,include模块通常放在文件顶部
用法:
include:
- base //需要引用的sls
- emacs //需要引用的sls
注:include本身作为一个顶级声明,不允许在一个文件中出现多次
应用实例:
[root@node01 base]# tree
.
├── database
│ └── mysql
│ └── init.sls
└── web
└── apache
└── init.sls
[root@node01 base]# vim test.sls
include:
- database.mysql.init
- web.apache.init
[root@node01 base]# salt '*' state.sls test
...省略执行状态
2. extend的使用
扩展某个SLS的状态可以使用extend实现
用法:
include:
- database.mysql.init
- web.apache.init
extend:
apache-install: //指定需要扩展的id
pkg.installed: //指定扩展使用的模块
- name: wget //参数
应用实例:
[root@node01 base]# tree
.
├── database
│ └── mysql
│ └── init.sls
├── test.sls
└── web
└── apache
└── init.sls
[root@node01 base]# vim test.sls
include:
- database.mysql.init
- web.apache.init
extend:
apache-install:
pkg.installed:
- name: wget
[root@node01 base]# salt '*' state.sls test
...省略执行状态
3. require与require_in的使用
require:我依赖谁
require_in:我被谁依赖
如果所依赖的id段没有执行成功,则require所在的id段不执行指令
用法:
apache-server:
service.running:
- name: httpd
- enable: True
- require:
- pkg: apache-install //表示依赖id为apache-install的pkg模块
应用实例:
[root@node01 base]# vim web/apache/init.sls
apache-install:
pkg.installed:
- name: httpd
apache-server:
service.running:
- name: httpd
- enable: True
- require:
- pkg: apache-install
[root@node01 base]# salt '*' state.sls web.apache.init
...省略执行状态
4. watch与watch_in的使用
监控某个文件是否改变,如果改变则执行某个指令
用法:
[root@node01 base]# vim web/apache/init.sls
apache-install:
pkg.installed:
- name: httpd
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://web/apache/file/httpd.conf
apache-server:
service.running:
- name: httpd
- enable: True
- reload: True
- require:
- pkg: apache-install
- watch:
- file: /etc/httpd/conf/httpd.conf //表示监控id为/etc/httpd/conf/httpd.conf的file模块
5. unless的使用
状态间的条件判断,如果判断成功则不执行所在id的指令,判断失败则执行
用法:
[root@node01 base]# vim test.sls
this_test:
cmd.run:
- name: hostname
- unless: test -f /root/abc //判断root目录中是否有abc文件,没有则执行指令
[root@node02 ~]# ls
anaconda-ks.cfg original-ks.cfg
[root@node01 base]# salt '*' state.sls test
node02:
----------
ID: this_test
Function: cmd.run
Name: hostname
Result: True
Comment: Command "hostname" run //已经执行指令了
...省略执行状态
6. template模板的使用
使用jinja模板定义变量
用法:
[root@node01 base]# cat test.sls
/opt/test:
file.managed:
- source: salt://file/test
- template: jinja //使用模板
- defaults: //定义默认值
DNS: 114.114.114.114 //定义DNS为114.114.114.114
[root@node01 base]# cat file/test
nameserver: {{ DNS }}
[root@node02 ~]# cat /opt/test
nameserver: 114.114.114.114