Ansible-下部
ansible-playbook
playbook是由一个或多个模块组成的,使用多个不同的模块,完成一件事情。
ansible软件特点
可以实现批量管理
可以实现批量部署
ad-hoc(批量执行命令)---针对临时性的操作
ansible clsn -m command -a "hostname" <- 批量执行命令举例
编写剧本-脚本(playbook)---针对重复性的操作
ansible核心功能
pyYAML-----用于ansible编写剧本所使用的语言格式(saltstack---python)
rsync-ini语法 sersync-xml语法 ansible-pyYAML语法
paramiko---远程连接与数据传输
Jinja2-----用于编写ansible的模板信息
剧本编写规则说明
YAML三板斧
缩进
YAML使用一个固定的缩进风格表示层级结构,每个缩进由两个空格组成, 不能使用tabs
冒号
以冒号结尾的除外,其他所有冒号后面所有必须有空格。
短横线
表示列表项,使用一个短横杠加一个空格。
多个项使用同样的缩进级别作为同一列表。
剧本书写格式
- hosts: 172.16.1.7 处理指定服务器 (空格)hosts:(空格)172.16.1.7
task: 剧本所要干的事情; (空格)(空格)task:
- name: (两个空格)-(空格)name:
command: echo hello clsn linux (四个空格)command:(空格)
剧本格式示例
[root@m01 ansible-playbook]# vim rsync.yaml
- hosts: 172.16.1.41
tasks:
- name: Install Rsync
yum: name=rsync state=installed
剧本检查方法
ansible-playbook --syntax-check 01.yml
--- 进行剧本配置信息语法检查
ansible-playbook -C 01.yml
--- 模拟剧本执行(彩排)
语法检查
ansible-playbook --syntax-check
[root@m01 ansible-playbook]# ansible-playbook --syntax-check 01.yml
playbook: 01.yml
模拟剧本执行
ansible-playbook -C
[root@m01 ansible-playbook]# ansible-playbook -C 01.yml
PLAY [all] ****************************************************************
TASK [Gathering Facts] ****************************************************
ok: [172.16.1.41]
ok: [172.16.1.8]
此处省略………..
PLAY RECAP ****************************************************************
172.16.1.31 : ok=2 changed=0 unreachable=0 failed=0
172.16.1.41 : ok=2 changed=0 unreachable=0 failed=0
172.16.1.8 : ok=2 changed=0 unreachable=0 failed=0
剧本示例
剧本编写内容扩展:剧本任务编写多个任务
- hosts: all
tasks:
- name: restart-network
cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
- name: sync time
cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"
剧本编写内容扩展:剧本任务编写多个主机
- hosts: 172.16.1.7
tasks:
- name: restart-network
cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
- name: sync time
cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"
- hosts: 172.16.1.31
tasks:
- name: show ip addr to file
shell: echo $(hostname -i) >> /tmp/ip.txt
剧本编写方式
多主机单任务编写方式
多主机多任务编写方式
不同主机多任务编写方式
Ansible项目案例
环境规划
全网备份
实时备份
角色 | 外网IP(NAT) | 内网IP(LAN) | 部署软件 |
---|---|---|---|
m01 | eth0:10.0.0.61 | eth1:172.16.1.61 | ansible |
backup | eth0:10.0.0.41 | eth1:172.16.1.41 | rsync |
nfs | eth0:10.0.0.31 | eth1:172.16.1.31 | nfs、Sersync |
web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | httpd |
目录规划
[root@m01 ~]# mkdir /etc/ansible/ansible_playbook/{file,conf,scripts} -p
[root@m01 ~]# tree /etc/ansible/ansible_playbook/
/etc/ansible/ansible_playbook/
├── conf
└── file
└── scripts
需提前准备好的文件
rsync配置文件
准备对应的配置文件存放至/etc/ansible/ansible_playbook/conf/
[root@m01 conf]# cat /etc/ansible/ansible_playbook/conf/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.password
log file = /var/log/rsyncd.log
#####################################
[backup]
path = /backup
[data]
path = /data
nfs配置文件
准备nfs配置文件exports
[root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/conf/nfs_exports
/data/ 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
Sersync软件包
下载Sersync软件包
[root@m01 ansible_playbook]# ll /etc/ansible/ansible_playbook/file/
-rw-r--r-- 1 root root 727290 Aug 1 12:04 sersync.tar.gz
sersync配置文件
准备sersync实时同步的配置文件
[root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/conf/confxml.xml.nfs
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="true"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data">
<remote ip="172.16.1.41" name="data"/>
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
基础环境部署
基础环境:所有机器统一的配置
1.需要关闭firewalld以及selinux、epel仓库、ssh端口、优化基础配置
2.需要安装rsync和nfs-utils
3.准备www用户
4.需要准备/etc/rsync.pass密码文件
5.需要准备全网备份脚本
基础的playbook剧本
[root@m01 ansible_playbook]# cat base.yaml
- hosts: all
tasks:
- name: Install Epel Repos
get_url: url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo
- name: Install Rsync Nfs-Utils
yum: name=rsync,nfs-utils state=installed
- name: Create Group WWW
group: name=www gid=666
- name: Create User WWW
user: name=www uid=666 group=666 create_home=no shell=/sbin/nologin
- name: Create Rsync_Client_Pass
copy: content='1' dest=/etc/rsync.pass mode=600
- name: Create Scripts Directory
file: path=/server/scripts recurse=yes state=directory
- name: Push File Scripts
copy: src=./scripts/rsync_backup_md5.sh dest=/server/scripts/
- name: Crontable Scripts
cron: name="backup scripts" hour=01 minute=00 job="/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null"
应用环境:Rsync
1.安装rsync
2.配置rsync(配置变更,一定要进行重载操作)
3.创建虚拟用户,权限调整
4.创建目录/data/ /backup
5.启动rsync
6.配置邮箱->邮箱的发件人->校验的脚本
[root@m01 ansible_playbook]# cat rsync.yaml
- hosts: backup
tasks:
- name: Installed Rsync Server
yum: name=rsync,mailx state=installed
- name: configure Rsync Server
copy: src=./conf/rsyncd.conf dest=/etc/rsyncd.conf
notify: Restart Rsync Server
- name: Create Virt User
copy: content='rsync_backup:1' dest=/etc/rsync.password mode=600
- name: Create Data
file: path=/data state=directory recurse=yes owner=www group=www mode=755
- name: Create Backup
file: path=/backup state=directory recurse=yes owner=www group=www mode=755
- name: Start RsyncServer
service: name=rsyncd state=started enabled=yes
- name: Push Check Scripts
copy: src=./scripts/rsync_check_backup.sh dest=/server/scripts/
- name: Crond Check Scripts
cron: name="check scripts" hour=05 minute=00 job="/bin/bash /server/scripts/rsync_check_backup.sh &>/dev/null"
应用环境:NFS
1.安装nfs-utils
2.配置nfs (当修改了配置,触发重载操作)
3.创建目录,授权
4.启动
[root@m01 ansible_playbook]# cat nfs.yaml
- hosts: nfs
tasks:
- name: Installed Nfs Server
yum: name=nfs-utils state=installed
- name: Configure Nfs Server
copy: src=./conf/exports dest=/etc/exports
notify: Restart Nfs Server
- name: Create Share Data
file: path=/data state=directory recurse=yes owner=www group=www mode=755
- name: Start Nfs Server
service: name=nfs-server state=started enabled=yes
handlers:
- name: Restart Nfs Server
service: name=nfs-server state=restarted
应用环境:Sersync
1.下载sersync
2.解压,改名,配置
3.启动
[root@m01 ansible_playbook]# cat sersync.yaml
- hosts: nfs
tasks:
- name: Scp Sersync
copy: src=./file/sersync2.5.4_64bit_binary_stable_final.tar.gz dest=/usr/local/sersync.tar.gz
- name: Zip
shell: cd /usr/local && tar xf sersync.tar.gz && mv GNU-Linux-x86 sersync
args:
creates: /usr/local/sersync
- name: configure Sersync
copy: src=./conf/confxml.xml dest=/usr/local/sersync/
- name: Start Sersync
shell: /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
应用环境:WEB
挂载nfs共享的目录
[root@m01 ansible_playbook]# cat web.yaml
- hosts: web
tasks:
- name: Mount NFS Server Share Data
mount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted
包含include
[root@m01 ansible_playbook]# cat mail.yaml
- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: sersync.yaml
- import_playbook: web.yaml
Ansible-下部的更多相关文章
- 如何利用ansible callback插件对执行结果进行解析
最近在写一个批量巡检工具,利用ansible将脚本推到各个机器上执行,然后将执行的结果以json格式返回来. 如下所示: # ansible node2 -m script -a /root/pyth ...
- 《Ansible权威指南》笔记(2)——Inventory配置
四.Inventory配置ansible通过Inventory来定义主机和组,使用时通过-i指定读取,默认/etc/ansible/hosts.可以存在多个Inventory,支持动态生成.1.定义主 ...
- useful Ansible commands
This article includes some useful Ansible commands. I will try to write blogs by English. You may wa ...
- 《Ansible权威指南》笔记(4)——Playbook
七.Playbook1.语法特性如下:(1)"---"首行顶格开始(2)#号注释(3)缩进统一,不同的缩进代表不同的级别,缩进要对齐,空格和tab不能混用(4)区别大小写,键值对k ...
- 《Ansible权威指南》笔记(3)——Ad-Hoc命令集,常用模块
五.Ad-Hoc命令集1.Ad-Hoc命令集通过/usr/bin/ansible命令实现:ansible <host-pattern> [options] -v,--verbose ...
- 《Ansible权威指南》笔记(1)——安装,ssh密钥登陆,命令
2016-12-23 读这本<Ansible权威指南>学习ansible,根据本书内容和网上的各种文档,以及经过自己测试,写出以下笔记.另,这本书内容很好,但印刷错误比较多,作者说第二版会 ...
- 自动化运维工具ansible部署以及使用
测试环境master 192.168.16.74webserver1 192.168.16.70webserver2 192.168.16.72安装ansiblerpm -Uvh http://ftp ...
- Ansible Ubuntu 安装部署
一.安装: $ sudo apt-get install ansible 二.配置: a.基本配置 $ cd /etc/ansible/ $ sudo cp hosts hosts_back 备份一个 ...
- Ansible 模块命令介绍
copy模块: 目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上 命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/' ...
- 用Vagrant和Ansible搭建持续交付平台
这是一个关于Vagrant的学习系列,包含如下文章: Vagrant入门 创建自己的Vagrant box 用Vagrant搭建Jenkins构建环境 用Vagrant和Ansible搭建持续交付平台 ...
随机推荐
- php弹出确认框
下面的代码只需要放在同一个文件中就可以运行了~~ html<a href="__URL__/shanchu/id/{$vo.id}" onclick='return del( ...
- windows虚拟机中DNS服务配置
在linux虚拟机中进行DNS服务配置并进行正向解析反向解析我博客中已经写过,下面 我来介绍一下在windows虚拟机中DNS服务的配置使用. 1.打开一台windows虚拟机中服务器管理器——角色— ...
- Docker 构建私有仓库
Docker Hub 目前Docker官方维护了一个公共仓库Docker Hub,其中已经包含了数量超过15000的镜像.大部分需求都可以通过在Docker Hub中直接下载镜像来实现. 可以在htt ...
- javascript实用Date工具
时间字符串和年月日数据之间的自由转换工具:2018年更新版 上代码: /** * @Desc: 时间处理工具 * @Author: 拿饭盒当烟灰缸 * @Date: 2018-02-27 15:42: ...
- Android多线程之(一)View.post()源码分析——在子线程中更新UI
提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...
- Flask 蓝图机制及应用
我们都知道 flask 是一个轻量级的 web 框架,相对于其他同类型框架更为灵活.轻便.安全且容易上手.开发者可以随意编写自己想要的项目结构,同时还有很多的第三方库供君选择.但是灵活的同时也带来了相 ...
- springcloud+kafka集群
上节说了kafka在linux环境下怎么搭建集群.这节写一下怎么在springcloud中以stream流方式去做kafka集群对接. 1.yml配置 #spring Cloud kafka -- s ...
- F5负载均衡架构
初识F5 提起F5,首先会想到负载均衡,也就是Load Balance.其意思就是将负载(工作任务)进行平衡.分摊到多个操作单元上进行执行,例如Web服务器.FTP服务器.企业关键应用服务器和其它关键 ...
- FPGA_VIP_V101 摄像头视频采集 调试总结之SDRAM引起的水平条纹噪声问题
FPGA_VIP_V101 摄像头视频采集 调试总结之SDRAM引起的水平条纹噪声问题 此问题困扰我很近,终于在最近的项目调整中总结了规律并解决了. 因为之前对sdram并不熟悉,用得也不是太多,于是 ...
- 【重温基础】11.Map和Set对象
本文是 重温基础 系列文章的第十一篇. 今日感受:注意身体,生病花钱又难受. 系列目录: [复习资料]ES6/ES7/ES8/ES9资料整理(个人整理) [重温基础]1.语法和数据类型 [重温基础]2 ...