SaltStack配置管理之状态模块和jinja2(五)
官方文档 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(五)的更多相关文章
- SaltStack配置管理-LAMP状态设计
上一篇:SaltStack之Salt-ssh 配置文件模板 apache: pkg.installed: - name: httpd service.running: - name: httpd /e ...
- SaltStack配置管理-状态间关系
上一篇:SaltStack配置管理-LAMP状态设计 include包含 上篇安装LAMP环境是一个个环境安装,可以通过include模块全部安装 lamp.sls include: - apache ...
- 架构师成长之路5.3-Saltstack配置管理(State状态模块)
点击架构师成长之路 架构师成长之路5.3-Saltstack配置管理(State状态模块) 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需要 ...
- 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 ...
- SaltStack的salt-ssh使用及LAMP状态设计部署(五)
一.salt-ssh的使用 官方文档:https://docs.saltstack.com/en/2016.11/topics/ssh/index.html (1)安装salt-ssh [root@l ...
- SaltStack配置管理--状态间的关系(六)
一.include的引用 需求场景:用于含有多个SLS的状态,使用include可以进行多个状态的组合,将安装apache,php,mysql集合在一个sls中 [root@7mini-node1 p ...
- Saltstack_使用指南10_配置管理-状态模块
1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...
- saltStack 状态模块(状态间的关系)
unless onlyif:状态间的条件判断,主要用于cmd状态模块 常用方法: onlyif:检查的命令,仅当'onlyif' 选项指向的命令返回true时才执行name 定义的命 unle ...
- Saltstack 安装与常用模块
一.介绍 saltstack是基于C/S服务模式,在该架构中,服务器端叫做Master,客户端叫做Minion.传统的C/S模式我们这样理解,客户端发送请求给服务器端,服务器端接受到来自客户端的请求并 ...
随机推荐
- WEB API 中HTTP的get、post、put,delete 请求方式
一.WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD 操作: 1.POST 用于新建资源,服务端在指定的URI 上创 ...
- [Codevs 1421]秋静叶&秋穣子(最大-最小博弈)
题目:http://codevs.cn/problem/1421/ 分析:有向树上的最大-最小博弈 先手与后手的策略不同: 先手A:让对方取得尽量少的前提下,自己取得尽量大 后手B:让自己取得尽量多的 ...
- NVIC优先级分组
挂起,解挂,使能,失能
- Entity Framework 出现 "此 ObjectContext 实例已释放,不可再用于需要连接的操作" 的错误
原因 Entity的导航属性在View中使用,但是该Entity所在的Context已经在Controller中通过 using 释放掉:但是Entity又具有Deferred Query Evalu ...
- Js中Prototype、__proto__、Constructor、Object、Function关系介绍
一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...
- python基础-编码_if条件判断
一.第一句Python代码 在 /home/dev/ 目录下创建 hello.py 文件,内容如下: [root@python-3 scripts]# cat hello.py #!/usr/bin/ ...
- JAVA file文件操作
/** *文件重命名 * @param oldname 原来的文件名 * @param newname 新文件名 */ @RequestMapping("renameFile") ...
- Fiddler捕获 iPhone/Android 流量
由于 Fiddler 作为代理的形式来捕获流量,因此理论上来说是不区分客户端的(当然 Fiddler 要安装在 Windows,毕竟是 .NET 开发的工具). 这一篇文章教你如何利用 Fiddler ...
- javaweb学习总结(四十二)——Filter(过滤器)学习
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...
- lucene-一篇分词器介绍很好理解的文章
本文来自这里在前面的概念介绍中我们已经知道了分析器的作用,就是把句子按照语义切分成一个个词语.英文切分已经有了很成熟的分析器: StandardAnalyzer,很多情况下StandardAnalyz ...