官方文档 https://docs.saltstack.com/en/latest/topics/states/index.html
配置管理之SLS
Salt State SLS描述文件(YAML)
名称ID声明 默认是name声明
备注: 一个ID声明下面。状态模块不能重复使用
例:
apache-install:
pkg.installed:
- names:
- httpd
- httpd-devel apache-service: # ID声明,高级状态,ID必须唯一。
service.running: # State声明 状态声明
- name: httpd # 选项声明
- enable: True php:
pkg.installed
常用状态模块介绍
1)pkg (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.pkg.html#module-salt.states.pkg)
pkg.installed # 安装
pkg.latest # 确保最新版本
pkg.remove # 卸载
pkg.purge # 卸载并删除配置文件
# 同时安装多个包
common_packages:
pkg.installed:
- pkgs:
- unzip
- dos2unix
- salt-minion: 2015.8.5-1.el6
2)file (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#module-salt.states.file)
salt:// 表示当前环境的根目录。例如:
那么salt://lamp/files/httpd.conf 表示 /srv/salt/lamp/files/httpd.conf
3)service (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.service.html#module-salt.states.service)
redis:
service.running:
- enable: True # 开机自启动
- reload: True # 重载
LAMP架构slat实现安装、配置、启动
1.安装软件包 pkg
2.修改配置文件 file
3.启动服务 service
lamp.sls文件内容如下
lamp-pkg:
pkg.installed:
- pkgs:
- httpd
- php
- mariadb
- mariadb-server
- php-mysql
- php-cli
- php-mbstring apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://lamp/files/httpd.conf
- user: root
- group: root
- mode: 644 php-config:
file.managed:
- name: /etc/php.ini
- source: salt://lamp/files/php.ini
- user: root
- group: root
- mode: 644 mysql-config:
file.managed:
- name: /etc/my.cnf
- source: salt://lamp/files/my.cnf
- user: root
- group: root
- mode: 644 apache-service:
service.running:
- name: httpd
- enable: True
- reload: True mysql-service:
service.running:
- name: mariadb
- enable: True
- reload: True
命令: salt 'linux-node2*' state.sls lamp.lamp
执行结果
linux-node2.example.com:
----------
ID: lamp-pkg
Function: pkg.installed
Result: True
Comment: targeted packages were installed/updated.
The following packages were already installed: httpd, mariadb-server, mariadb
Started: ::16.178765
Duration: 194279.377 ms
Changes:
----------
libzip:
----------
new:
0.10.-.el7
old:
php:
----------
new:
5.4.-36.3.el7_2
old:
php-cli:
----------
new:
5.4.-36.3.el7_2
old:
php-common:
----------
new:
5.4.-36.3.el7_2
old:
php-mbstring:
----------
new:
5.4.-36.3.el7_2
old:
php-mysql:
----------
new:
5.4.-36.3.el7_2
old:
php-pdo:
----------
new:
5.4.-36.3.el7_2
old:
----------
ID: apache-config
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: ::30.519583
Duration: 98.547 ms
Changes:
----------
ID: php-config
Function: file.managed
Name: /etc/php.ini
Result: True
Comment: File /etc/php.ini is in the correct state
Started: ::30.620067
Duration: 36.824 ms
Changes:
----------
ID: mysql-config
Function: file.managed
Name: /etc/my.cnf
Result: True
Comment: File /etc/my.cnf is in the correct state
Started: ::30.657074
Duration: 58.78 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: ::30.853149
Duration: 40.481 ms
Changes:
----------
ID: mysql-service
Function: service.running
Name: mariadb
Result: True
Comment: The service mariadb is already running
Started: ::30.893939
Duration: 33.928 ms
Changes: Summary for linux-node2.example.com
------------
Succeeded: (changed=)
Failed:
------------
Total states run:
Total run time: 194.548 s
第二种方式:
文件lamp2.sls 内容如下:
apache-server:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://lamp/files/httpd.conf
- user: root
- group: root
- mode: 644
service.running:
- name: httpd
- enable: True
- reload: True mysql-server:
pkg.installed:
- pkgs:
- mariadb
- mariadb-server
file.managed:
- name: /etc/my.cnf
- source: salt://lamp/files/my.cnf
- user: root
- group: root
- mode: 644
service.running:
- name: mariadb
- enable: True
- reload: True php-config:
file.managed:
- name: /etc/php.ini
- source: salt://lamp/files/php.ini
- user: root
- group: root
- mode: 644
命令: salt 'linux-node2*' state.sls lamp.lamp2
执行结果
linux-node2.example.com:
----------
ID: apache-server
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: ::53.886308
Duration: 665.948 ms
Changes:
----------
ID: apache-server
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: ::54.553919
Duration: 19.867 ms
Changes:
----------
ID: apache-server
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: ::54.574411
Duration: 29.927 ms
Changes:
----------
ID: mysql-server
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: ::54.604496
Duration: 0.771 ms
Changes:
----------
ID: mysql-server
Function: file.managed
Name: /etc/my.cnf
Result: True
Comment: File /etc/my.cnf is in the correct state
Started: ::54.605362
Duration: 15.125 ms
Changes:
----------
ID: mysql-server
Function: service.running
Name: mariadb
Result: True
Comment: The service mariadb is already running
Started: ::54.620592
Duration: 29.75 ms
Changes:
----------
ID: php-config
Function: file.managed
Name: /etc/php.ini
Result: True
Comment: File /etc/php.ini is in the correct state
Started: ::54.650496
Duration: 17.036 ms
Changes: Summary for linux-node2.example.com
------------
Succeeded:
Failed:
------------
Total states run:
Total run time: 778.424 ms
配置管理之状态间关系
状态间关系:
1.我依赖谁 require
apache-service:
service.running:
- name: httpd
- enable: True
- reload: True
- require:
- pkg: lamp-pkg # pkg ID
- file: apache-config # file ID
2 我被谁依赖 require_in
mysql-config:
file.managed:
- name: /etc/my.cnf
- source: salt://lamp/files/my.cnf
- user: root
- group: root
- mode: 644
- require_in:
- service: mysql-service
3 我监控谁 watch
apache-service:
service.running:
- name: httpd
- enable: True
- reload: True
- require:
- pkg: lamp-pkg
- watch:
- file: apache-config
1. 若果apache-config这个id的状态发生变化就reload
2. 如果不加reload: True,那么就restart
4 我被谁监控 watch_in
5 我引用谁 include
例:lamp第一种方法中,将安装、配置、启动分别保存3个文件, 由一个总文件引用
init.sls文件内容
include:
- lamp.lamp_pkg
- lamp.lamp_config
- lamp.lamp_service
lamp_pkg.sls文件内容
lamp-pkg:
pkg.installed:
- pkgs:
- httpd
- php
- mariadb
- mariadb-server
- php-mysql
- php-cli
- php-mbstring
lamp_config.sls文件内容
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://lamp/files/httpd.conf
- user: root
- group: root
- mode: 644 php-config:
file.managed:
- name: /etc/php.ini
- source: salt://lamp/files/php.ini
- user: root
- group: root
- mode: 644 mysql-config:
file.managed:
- name: /etc/my.cnf
- source: salt://lamp/files/my.cnf
- user: root
- group: root
- mode: 644
- require_in:
- service: mysql-service
lamp_service.sls文件内容
apache-service:
service.running:
- name: httpd
- enable: True
- reload: True
- require:
- pkg: lamp-pkg
- watch:
- file: apache-config mysql-service:
service.running:
- name: mariadb
- enable: True
- reload: True
执行命令:salt 'linux-node2*' state.sls lamp.init
6 我扩展谁
如何编写SLS技巧:
1.按状态分类 如果单独使用,很清晰。
2.按服务分类 可以被其他的SLS include。例如LNMP include mysql的服务。
jinja2
文档:http://docs.jinkan.org/docs/jinja2/
模板包含 变量 或 表达式,两种分隔符: {% ... %} 和 {{ ... }} 。前者用于执行诸如 for 循环 或赋值的语句,后者把表达式的结果打印到模板上。
salt中如何使用jinja2:
文档:https://docs.saltstack.com/en/latest/topics/jinja/index.html
1)告诉File模块,你要使用jinja
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://lamp/files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
2)列出参数列表
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://lamp/files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
- defaults:
PORT: 8080
3)模板引用
httpd.conf配置文件引用如下
执行命令:salt 'linux-node2*' state.sls lamp.init
执行结果:
linux-node2.example.com:
----------
ID: lamp-pkg
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: ::02.903236
Duration: 4591.748 ms
Changes:
----------
ID: apache-config
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf updated
Started: ::07.558365
Duration: 90.859 ms
Changes:
----------
diff:
---
+++
@@ -, +, @@
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:
-Listen
+Listen #
# Dynamic Shared Object (DSO) Support
----------
ID: php-config
Function: file.managed
Name: /etc/php.ini
Result: True
Comment: File /etc/php.ini is in the correct state
Started: ::07.649429
Duration: 63.754 ms
Changes:
----------
ID: mysql-config
Function: file.managed
Name: /etc/my.cnf
Result: True
Comment: File /etc/my.cnf is in the correct state
Started: ::07.713515
Duration: 49.273 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service reloaded
Started: ::07.800629
Duration: 135.15 ms
Changes:
----------
httpd:
True
----------
ID: mysql-service
Function: service.running
Name: mariadb
Result: True
Comment: The service mariadb is already running
Started: ::07.936165
Duration: 95.71 ms
Changes: Summary for linux-node2.example.com
------------
Succeeded: (changed=)
Failed:
------------
Total states run:
Total run time: 5.026 s
- 模板里面支持: salt执行模块 grinas 进行赋值
例:修改配置文件httpd.conf,将IP地址指向本机IP,通过grains['fqdn_ip4'][0]可以获取本机IP地址
salt 'linux-node2*' grains.item fqdn_ip4
- 模板里面支持:salt远程执行模块
例:修改配置文件httpd.conf,{{ salt['netwrok.hw_addr']('eth0') }}
salt 'linux-node2*' network.hw_addr eth0
执行命令:salt 'linux-node2*' state.sls lamp.init
执行结果
linux-node2.example.com:
----------
ID: lamp-pkg
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: ::57.213758
Duration: 664.953 ms
Changes:
----------
ID: apache-config
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf updated
Started: ::57.880642
Duration: 82.912 ms
Changes:
----------
diff:
---
+++
@@ -, +, @@
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:
-Listen
+Listen 192.168.137.12:
+
+# MAC IS: :0c::fd:dd: #
# Dynamic Shared Object (DSO) Support
----------
ID: php-config
Function: file.managed
Name: /etc/php.ini
Result: True
Comment: File /etc/php.ini is in the correct state
Started: ::57.963715
Duration: 14.577 ms
Changes:
----------
ID: mysql-config
Function: file.managed
Name: /etc/my.cnf
Result: True
Comment: File /etc/my.cnf is in the correct state
Started: ::57.978393
Duration: 12.482 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service reloaded
Started: ::58.021471
Duration: 127.043 ms
Changes:
----------
httpd:
True
----------
ID: mysql-service
Function: service.running
Name: mariadb
Result: True
Comment: The service mariadb is already running
Started: ::58.148913
Duration: 58.592 ms
Changes: Summary for linux-node2.example.com
------------
Succeeded: (changed=)
Failed:
------------
Total states run:
Total run time: 960.559 ms
- 模板里面支持: salt执行模块 pillar进行赋值
例:修改配置文件httpd.conf,{{ pillar['apache'] }}
salt 'linux-node2*' pillar.item apache
执行命令:salt 'linux-node2*' state.sls lamp.init
执行结果:
linux-node2.example.com:
----------
ID: lamp-pkg
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: ::16.490143
Duration: 712.121 ms
Changes:
----------
ID: apache-config
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf updated
Started: ::17.204369
Duration: 93.136 ms
Changes:
----------
diff:
---
+++
@@ -, +, @@
Listen 192.168.137.12: # MAC IS: :0c::fd:dd:
+# pillar: httpd #
# Dynamic Shared Object (DSO) Support
----------
ID: php-config
Function: file.managed
Name: /etc/php.ini
Result: True
Comment: File /etc/php.ini is in the correct state
Started: ::17.297764
Duration: 17.209 ms
Changes:
----------
ID: mysql-config
Function: file.managed
Name: /etc/my.cnf
Result: True
Comment: File /etc/my.cnf is in the correct state
Started: ::17.315170
Duration: 15.217 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: ::17.331369
Duration: 184.591 ms
Changes:
----------
httpd:
True
----------
ID: mysql-service
Function: service.running
Name: mariadb
Result: True
Comment: The service mariadb is already running
Started: ::17.516431
Duration: 32.057 ms
Changes: Summary for linux-node2.example.com
------------
Succeeded: (changed=)
Failed:
------------
Total states run:
Total run time: 1.054 s