官方文档 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

SaltStack配置管理之状态模块和jinja2(五)的更多相关文章

  1. SaltStack配置管理-LAMP状态设计

    上一篇:SaltStack之Salt-ssh 配置文件模板 apache: pkg.installed: - name: httpd service.running: - name: httpd /e ...

  2. SaltStack配置管理-状态间关系

    上一篇:SaltStack配置管理-LAMP状态设计 include包含 上篇安装LAMP环境是一个个环境安装,可以通过include模块全部安装 lamp.sls include: - apache ...

  3. 架构师成长之路5.3-Saltstack配置管理(State状态模块)

    点击架构师成长之路 架构师成长之路5.3-Saltstack配置管理(State状态模块) 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需要 ...

  4. 009(1)-saltstack之salt-ssh的使用及配置管理LAMP状态的实现

    1 salt-ssh的使用 1. 安装salt-ssh[root@slave1 .ssh]# yum install -y salt-ssh 2. 配置salt-ssh # Sample salt-s ...

  5. SaltStack的salt-ssh使用及LAMP状态设计部署(五)

    一.salt-ssh的使用 官方文档:https://docs.saltstack.com/en/2016.11/topics/ssh/index.html (1)安装salt-ssh [root@l ...

  6. SaltStack配置管理--状态间的关系(六)

    一.include的引用 需求场景:用于含有多个SLS的状态,使用include可以进行多个状态的组合,将安装apache,php,mysql集合在一个sls中 [root@7mini-node1 p ...

  7. Saltstack_使用指南10_配置管理-状态模块

    1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...

  8. saltStack 状态模块(状态间的关系)

    unless onlyif:状态间的条件判断,主要用于cmd状态模块 常用方法:    onlyif:检查的命令,仅当'onlyif'  选项指向的命令返回true时才执行name 定义的命 unle ...

  9. Saltstack 安装与常用模块

    一.介绍 saltstack是基于C/S服务模式,在该架构中,服务器端叫做Master,客户端叫做Minion.传统的C/S模式我们这样理解,客户端发送请求给服务器端,服务器端接受到来自客户端的请求并 ...

随机推荐

  1. 从Evernote迁移到Wiz

    实在受不了evernote没完没了的弹出广告和让升级账号,我不过就是想安安静静的记个笔记,真不想看您的广告好吧.于是无奈这下,只能选择转换到别的笔记工具阵营. 由于以前一直听说OneNote是仅次于E ...

  2. ASP.NET mvc异常处理的方法

    第一种:全局异常处理 1.首先常见保存异常的类(就是将异常信息写入到文件中去) public class LogManager { private string logFilePath = strin ...

  3. 离散系统频响特性函数freqz()

    MATLAB提供了专门用于求离散系统频响特性的函数freqz(),调用freqz()的格式有以下两种: l        [H,w]=freqz(B,A,N) B和A分别为离散系统的系统函数分子.分母 ...

  4. JS 页面加载触发事件 document.ready和window.onload的区别

    document.ready和onload的区别——JavaScript文档加载完成事件页面加载完成有两种事件: 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件): 二是onlo ...

  5. ASP.NET 问题集锦

    [1]解决错误:从客户端(Content="<p>测试</p>")中检测到有潜在危险的 Request.Form 值      .NetFrameWork ...

  6. tomcat设置端口号和默认webapp

    tomcat一下载,解压之后webapps目录下自带几个webapp: * docs文档:这是一个静态页面集,不用启动tomcat也可以阅读 * examples样例 * hostmanager主机管 ...

  7. bitmap转化base64

    /** * bitmap转化base64 */public static String bitmapToBase64(Bitmap bitmap) { String result = null; By ...

  8. 【CodeVS 2083】Cryptcowgraphy 解密牛语

    http://codevs.cn/problem/2083/ 奶牛搜索题.我加了如下剪枝: 1.用字符串hash判重.注意判重时也要对字符串长度判重,否则会出现两个字符串长度不同但hash值相同的情况 ...

  9. zoj3261 并查集离线处理

    Connections in Galaxy War Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  10. bootstrap fileinput-上传回调

    通过第三分jquery重新绑定一个重新加载事件 $("#file-4").on("fileuploaded", function(event, data, pr ...