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模式我们这样理解,客户端发送请求给服务器端,服务器端接受到来自客户端的请求并 ...
随机推荐
- Firefox about
在firefox的地址栏输入about:about,然后看一下各个链接.有的链接有具体的用途,有的链接疯言疯语,并无软用. about:about集中了火狐浏览器的全部用户界面,平时常见的prefer ...
- 【Zeyphr】保存json到数据库
方法一: public int SaveJob(JObject data) { var formWrapper = RequestWrapper.Instance().LoadSettingXmlSt ...
- 【JavaEE企业应用实战学习记录】servlet3.0上传文件
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 14:20 To change this tem ...
- hello Cookie
Cookie 是什么? Cookie在浏览器中的表现为请求头域和响应头域的字段,也就是伴随着请求和响应的一组键值对的文本.Cookie来源于服务器,第一次请求无Cookie参数,增加Cookie通过服 ...
- Android怎么使用字体图标 自定义FontTextView字体图标控件-- 使用方法
首先我想说明一下字体图标的好处,最大的好处就是自适应了,而且是使用TextView 不用去切图,是矢量图 灵活调用 第一步我要说明一下一般字体图标的来源,我这里使用的是 --阿里巴巴矢量图标库 -网 ...
- iOS开发中的错误整理,IOS9中canOpenURL调用失败分析
由于IOS加入对用户隐私以及禁止扫描系统信息的控制,目前通过canOpenURL的方法来判断用户是否安装特定app,则会出现-canOpenURL: failed for URL: "ABC ...
- epon e8-c HG220GS超级密码破解
网上找了很多管理电信e8-c的破解资料,大多都是明文密码,而hg220gs则为加密的密码,找来找去最后终于找到加密方式了base64,真心不容易 下面从其他博文中转载过来留着记录 低端hack.主要是 ...
- 16 IO操作文件读写
IO的分类 第一种分法: 1.输入流 2.输出流 第二种分法: 1.字节流 2.字符流 第三种分法: 1.节点流 2.处理流 I/O当中的核心类: InputStream <--------F ...
- Vi命令:如何删除全部内容?
在命令模式下,输入:.,$d 一回车就全没了. 表示从当前行到末行全部删除掉. 用gg表示移动到首行.
- DFS经典题,reachable or not in a 2D maze
[[0, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0], [0, 1, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0], ...