SaltStact自动化运维工具03
存储位置 类型 采集方式 场景
Grains minion 静态 minion启动时,可以刷新 1、获取信息 2、匹配
pillar master 动态 指定,实时生效 1、匹配 2、敏感数据配置
LAMP (软件安装salt.states.pki 配置文件salt.states.file 服务salt.states.service)
[root@linux-node1 prod]# mkdir -p /srv/salt/prod/{apache,mysql,php}
[root@linux-node1 prod]# tree
.
├── apache
├── mysql
└── php
#################################
[root@linux-node1 prod]# cd apache/
[root@linux-node1 apache]# vim init.sls
apache-install:
pkg.installed:
- name: httpd
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
apache-services:
service.running:
- name: httpd
- enable: True
[root@linux-node1 apache]# mkdir files
[root@linux-node1 apache]# cd files/
[root@linux-node1 files]# cp /etc/httpd/conf/httpd.conf .
[root@linux-node1 files]# salt 'linux-node1*' state.sls apache.init saltenv='prod'
linux-node1.localdomain:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 11:22:05.394917
Duration: 1124.351 ms
Changes:
----------
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: 11:22:06.550853
Duration: 32.42 ms
Changes:
----------
ID: apache-services
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 11:22:06.637945
Duration: 115.643 ms
Changes: Summary for linux-node1.localdomain
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
Total run time: 1.272 s
[root@linux-node1 prod]# cd php/
[root@linux-node1 php]# mkdir files
[root@linux-node1 php]# vim init.sls
php-install:
pkg.installed:
- pkgs:
- php
- php-pdo
- php-mysql
php-config:
file.managed:
- name: /etc/php.ini
- source: salt://php/files/php.ini
- user: root
- group: root
- mode: 644
[root@linux-node1 php]# yum -y install php
[root@linux-node1 php]# cp /etc/php.ini files/
#################################
[root@linux-node1 prod]# cd mysql/
[root@linux-node1 mysql]# mkdir files
[root@linux-node1 mysql]# vim init.sls
mysql-install:
pkg.installed:
- pkgs:
- mariadb
- mariadb-server
mysql-config:
file.managed:
- name: /etc/my.cnf
- source: salt://mysql/files/my.cnf
- user: root
- group: root
- mode: 644
mysql-service:
service.running:
- name: mariadb
- enable: True
[root@linux-node1 prod]# yum -y install mariadb-server
[root@linux-node1 prod]# cp /etc/my.cnf mysql/files/
#################################
[root@linux-node1 prod]# tree
.
├── apache
│ ├── files
│ │ └── httpd.conf
│ └── init.sls
├── mysql
│ ├── files
│ │ └── my.cnf
│ └── init.sls
└── php
├── files
│ └── php.ini
└── init.sls
[root@linux-node1 files]# salt 'linux-node1*' state.sls apache.init saltenv='prod'
salt -S '192.168.0.2' state.sls php.init saltenv=prod
salt -S '192.168.0.2' state.sls mysql.init saltenv=prod
等价于以下方式:
[root@linux-node1 prod]# vim ../base/top.sls
prod:
'linux-node1.localdomain':
- apache.init
- php.init
- mysql.init
[root@linux-node1 prod]# salt -S '192.168.0.2' state.highstate
linux-node1.localdomain:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 12:04:03.354119
Duration: 987.978 ms
Changes:
----------
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: 12:04:04.345800
Duration: 24.349 ms
Changes:
----------
ID: apache-services
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 12:04:04.371095
Duration: 59.907 ms
Changes:
----------
ID: php-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 12:04:04.431364
Duration: 26.57 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: 12:04:04.458181
Duration: 13.008 ms
Changes:
----------
ID: mysql-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 12:04:04.471433
Duration: 24.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: 12:04:04.496454
Duration: 11.538 ms
Changes:
----------
ID: mysql-service
Function: service.running
Name: mariadb
Result: True
Comment: The service mariadb is already running
Started: 12:04:04.508203
Duration: 45.043 ms
Changes: Summary for linux-node1.localdomain
------------
Succeeded: 8
Failed: 0
------------
Total states run: 8
Total run time: 1.193 s
incloude 和 extend 使用
[root@linux-node1 prod]# vim lamp.sls
include:
- php.init
- apache.init
- mysql.init
[root@linux-node1 prod]# vim ../base/top.sls
prod:
'linux-node1.localdomain':
- lamp
[root@linux-node1 prod]# salt -S '192.168.0.2' state.highstate
扩展
[root@linux-node1 prod]# vim lamp.sls
include: #相当于把三个文件夹里的init.sls的内容复制粘贴过来一样
- php.init
- apache.init
- mysql.init
extend: #相当于在php/init.sls里面的pkg.installed加了一个安装项目
php-install:
pkg.installed:
- name: php-mbstring
[root@linux-node1 prod]# salt -S '192.168.0.2' state.highstate
#################################
require(我依赖谁) 和 require_in(我被谁依赖) 使用
[root@linux-node1 prod]# vim apache/init.sls
apache-install: #没有ID下面的模块(例如pkg、file、service)不能重复
pkg.installed:
- name: httpd
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpdi.conf 故意改错http.conf httpdi.conf
- user: root
- group: root
- mode: 644
apache-services:
service.running:
- name: httpd
- enable: True
- require: #先会判断 apache-install 和 apache-config是否执行成功 不成功就不执行apache-services
- pkg: apache-install #格式: - 模块名: 自己定义的ID
- file: apache-config
[root@linux-node1 prod]# salt -S '192.168.0.2' state.highstate 先找到 base的top.sls ---> 再执行top.sls里面的lamp.sls --->最后执行lamp.sls里面的 mysql/init.sls php/init.sls apache/init.sls
linux-node1.localdomain:
----------
ID: php-install
Function: pkg.installed
Name: php-mbstring
Result: True
Comment: All specified packages are already installed
Started: 15:31:45.534122
Duration: 992.179 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: 15:31:46.529682
Duration: 24.317 ms
Changes:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 15:31:46.554230
Duration: 25.533 ms
Changes:
----------报错的地方
ID: apache-config
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: False
Comment: Source file salt://apache/files/httpdi.conf not found in saltenv 'prod'
Started: 15:31:46.580005
Duration: 5.781 ms
Changes:
----------报错的地方
ID: apache-services
Function: service.running
Name: httpd
Result: False
Comment: One or more requisite failed: apache.init.apache-config
Started: 15:31:46.587421
Duration: 0.02 ms
Changes:
----------
ID: mysql-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 15:31:46.587521
Duration: 24.84 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: 15:31:46.612602
Duration: 11.833 ms
Changes:
----------
ID: mysql-service
Function: service.running
Name: mariadb
Result: True
Comment: The service mariadb is already running
Started: 15:31:46.624651
Duration: 59.244 ms
Changes: Summary for linux-node1.localdomain
------------
Succeeded: 6
Failed: 2
------------
Total states run: 8
Total run time: 1.144 s
require_in(我被谁依赖) 演示
[root@linux-node1 prod]# vim apache/init.sls
跟require效果差不多
watch 和 watch_in 的使用(不是什么模块都可以使用 service有)
[root@linux-node1 prod]# vim apache/init.sls
看着(watch) 修改http.conf 文件的内容 就执行重启
[root@linux-node1 prod]# vim apache/init.sls
加了一行 reload: True
看着(watch) 修改http.conf 文件的内容 就执行重新加载
[root@linux-node1 prod]# vim apache/init.sls
看着(watch in) 修改http.conf 文件的内容 就执行重新加载
unless用法
[root@linux-node1 ~]# cd /var/www/html/
[root@linux-node1 html]# mkdir admin
[root@linux-node1 admin]# vim index.html
[root@linux-node1 salt]# cd /srv/salt/prod/apache/files/
[root@linux-node1 files]# vim httpd.conf
<Directory /var/www/html/admin>
AllowOverride All
Order allow,deny
Allow from all
AuthType Basic
AuthName "welcome"
AuthUserFile /etc/httpd/conf/htpasswd_file
Require user admin
</Directory>
[root@linux-node1 files]# whereis htpasswd
htpasswd: /usr/bin/htpasswd /usr/share/man/man1/htpasswd.1.gz
[root@linux-node1 files]# rpm -qf /usr/bin/htpasswd
httpd-tools-2.4.6-88.el7.centos.x86_64
[root@linux-node1 files]# cd ..
[root@linux-node1 apache]# vim init.sls 增加一个
apache-auth:
pkg.installed:
- name: httpd-tools
cmd.run:
- name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
- unless: test -f /etc/httpd/conf/htpasswd_file # 如果条件为假 才执行 htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
[root@linux-node1 ~]# salt -S '192.168.0.2' state.highstate
http://192.168.0.2/admin
jinja模板用法
[root@linux-node1 ~]# vim /srv/salt/prod/apache/init.sls #加上jinja模板
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
PORT: 80
IPADDR: {{ grains['fqdn_ip4'][0] }} #输出的是一个列表
[root@linux-node1 ~]# vim /srv/salt/prod/apache/files/httpd.conf
#Listen 12.34.56.78:80
Listen {{ IPADDR }}:{{ PORT }}
#
[root@linux-node1 ~]# vim /srv/salt/base/top.sls
#base:
# 'os:ubuntu':
# - match: grain
# - web.apache
prod:
'linux-node*.localdomain':
- lamp
~
[root@linux-node1 ~]# salt '*' state.highstate
SaltStact自动化运维工具03的更多相关文章
- SaltStact自动化运维工具01
什么是saltstackSaltstack是基于python开发的一套C/S架构配置管理工具使用SSL证书签方的方式进行认证管理底层使用ZeroMQ消息队列pub/sub方式通信 – 号称世界 ...
- SaltStact自动化运维工具02
Grains基础:• Grains是saltstack最重要的组件之一• 存储minion端的基本信息,这些信息一般都是静态的,如CPU.内核.操作系统等• Grains存储在minion本地• 管 ...
- 自动化运维工具之ansible
自动化运维工具之ansible 一,ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...
- Linux实战教学笔记25:自动化运维工具之ansible (一)
第二十五节 ansible之文件的批量分发 标签(空格分隔): Linux实战教学笔记-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转 ...
- 部署MySQL自动化运维工具inception+archer
***************************************************************************部署MySQL自动化运维工具inception+a ...
- Ansible自动化运维工具
ansible软件介绍 python语言是运维人员必会的语言! ansible是一个基于Python开发的自动化运维工具!(saltstack) 其功能实现基于SSH远程连接服务! ans ...
- 企业级自动化运维工具应用实战ansible
公司计划在年底做一次大型市场促销活动,全面冲刺下交易额,为明年的上市做准备.公司要求各业务组对年底大促做准备,运维部要求所有业务容量进行三倍的扩容,并搭建出多套环境可以共开发和测试人员做测试,运维老大 ...
- 自动化运维工具-Ansible之4-变量
自动化运维工具-Ansible之4-变量 目录 自动化运维工具-Ansible之4-变量 变量概述 变量的定义和调用 变量优先级测试 变量优先级测试二 变量注册 facts缓存 变量概述 变量提供 ...
- 自动化运维工具-Ansible之2-ad-hoc
自动化运维工具-Ansible之2-ad-hoc 目录 自动化运维工具-Ansible之2-ad-hoc Ansible ad-hoc Ansible命令模块 Ansible软件管理模块 Ansibl ...
随机推荐
- Golang - 处理字符串
目录 Golang - 处理字符串 1. 字符串操作 2. 字符串转换 Golang - 处理字符串 1. 字符串操作 func Contains(s, substr string) bool 字符串 ...
- SSH框架下单元测试的实现
SSH框架下单元测试的实现 实现的功能 实现了部门的增删改查 对Action进行了单元测试 对Service 进行了单元测试,通过mock的方式实现. 实现的步骤 一.对Action层的单元测试实现 ...
- COGS——C1176. [郑州101中学] 月考
http://cogs.pro/cogs/problem/problem.php?pid=1176 [题目描述] 在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时 间 ...
- Spring MVC-表单(Form)标签-复选框集合(Checkboxes)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_checkboxes.htm 说明:示例基于Spring MVC 4.1.6. 以 ...
- linux高级技巧:rsync同步(二)
1.配置两个节点同步 上个帖子已经展示了同步一个主机的方法.这次我们再加入一个要同步的主机. 方法上并无二质. 1.首先显示我们的控制台: ...
- 创建MAVEN项目报错
创建MAVEN项目pom.xml报错 Failure to transfer org.apache.maven:maven-archiver:jar:2.4.2 from http://repo.ma ...
- java根据内容生成二维码图片
package util; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; ...
- chromium添加flash
http://blog.csdn.net/lainegates/article/details/27830333
- bzoj4956: [Wf2017]Secret Chamber at Mount Rushmore
F......loyd? 这范围也是..... 然而大了也不会.... #include<cstdio> #include<iostream> #include<cstr ...
- HDU 1257(最小拦截系统)
Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不 能超过前一发的高度.某天,雷达 ...