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 ...
随机推荐
- DataInputStream 数据类型数据输入输出流
package IOliu; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileI ...
- Python学习笔记之文件处理
1.打开和关闭 要打开文件可以使用函数 open(),它位于自动导入模块 io 中.函数将文件名作为唯一不可缺少的函数,并返回一个文件对象.它还有一个名叫 mode 的参数,用于指定文件模式,其可取值 ...
- BZOJ 3218 UOJ #77 A+B Problem (主席树、最小割)
大名鼎鼎的A+B Problem, 主席树优化最小割-- 调题死活调不对,一怒之下改了一种写法交上去A了,但是改写法之后第4,5个点常数变大很多,于是喜提UOJ全站倒数第三 目前还不知道原来的写法为什 ...
- POJ 4046 Sightseeing
Sightseeing Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID ...
- springMVC @ModelAttribute功能
@ModelAttribute功能:将数据模型回写到页面 如: public String validate(@Valid @ModelAttribute("user") User ...
- 24 Point game
24 Point game 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 There is a game which is called 24 Point game ...
- JQuery的wrap用法
wrap是包裹元素的作用,比如我想在img外面包裹一个a标签时,可以这样写法: $(function(){ $('img').wrap(function(){ return '<a href=& ...
- Scala入门到精通——第一节 Scala语言初步
本节主要内容 Scala简单介绍 为什么要学习Scala Scala语言初步 1. Scala简单介绍 Scala(Scala Language的简称)语言是一种能够执行于JVM和.Net平台之上的通 ...
- 【小超_Android】android上开源的酷炫的交互动画和视觉效果:Interactive-animation
1.交互篇 2.视觉篇 交互篇 1.SlidingUpPanelLayout 项目介绍:他的库提供了一种简单的方式来加入一个可拖动滑动面板(由谷歌音乐推广,谷歌地图和Rdio)你的Android应用程 ...
- Windows 7下Git SSH 创建Key【待解决?】
1.在桌面右键,git bash here 2.检查本机是否有ssh key设置 $ cd ~/.ssh 或cd .ssh 如果没有则提示: No such file or directory:此时需 ...