ansbile--playbook剧本案例

个人博客转至: www.zhangshoufu.com

通过ansible批量管理三台服务器,使三台服务器实现备份,web01、nfs、backup,把web和nfs上的重要文件被分到backup上,主机ip地址分配如下

CharacterIP地址IP地址主机名
Rsync--server172.16.1.4110.0.0.41backup-rsync-41
NFS-client172.16.1.3110.0.0.31Nfs01-31
Web01172.16.1.710.0.0.7web01-7

在m01上操作,编写ansible清单和剧本
目录规划:
我们把所有的yaml文件都放在/playbook目录下,配置文件都放在/paly/conf目录下,脚本都放在/playbook/scripts目录下。

[root@m01-61 /]# mkdir /playbook/{conf,scripts}
[root@m01-61 /]# cat /etc/ansible/hosts     ---主机清单
[nfs]
172.16.1.31 ansible_ssh_private_key_file=/root/.ssh/test_id_rsa[web]
172.16.1.7 ansible_ssh_private_key_file=/root/.ssh/test_id_rsa[backup]
172.16.1.41 ansible_ssh_private_key_file=/root/.ssh/test_id_rsa[host:children]
nfs
web
backup

构建基础的剧本,所有的服务器都应用这个剧本

1,基础的额优化,关闭firewalld和selinux,修改ssh,修改dns的文件
2,安装构建epel源
3,安装nfs和rsyn服务
4,创建UID和GID为666的www用户
5,创建rsync推送使用的密钥文件
6,创建一个共同存放脚本的路径
7,创建备份的脚本,编写定时任务
[root@m01-61 /]# cd /playbook/
[root@m01-61 playbook]# cat base.yaml 
#zhe shi yi ge ji chu
- hosts: alltasks:#    - name: stop firewall- name: Install Epel reposget_url: url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo
# ssh firewall selinux hosts- name: Dns client filecopy: src=./conf/resolv.conf dest=/etc/resolv.conf- name: Install service rsync nfs-utilsyum: name=rsync,nfs-utils state=installed- name: create group group: name=www gid=666- name: creat useruser: name=www uid=666 group=www create_home=no shell=/sbin/nologin- name: rsync passwd filecopy: content='1' dest=/etc/rsync.pass mode=0600- name: creat /server/scriptsfile: path=/server/scripts state=directory recurse=yes - name: copy scriptscopy: src=./scripts/client_rsync_backup.sh dest=/server/scripts/client_rsync_backup.sh- name: crontab sh /server/scripts/client_rsync_backup.shcron: name="backup scripts" minute=0 hour=1 job="/usr/bin/bash /server/scripts/client_rsync_backup.sh &> /dev/null "

关闭backup的剧本

1,配置邮件服务,推送校验客户端推送是否完整,并发送邮件
2,创建backup和data目录
3,生成rsync的配置文件, 和密码文件
4,当rsync配置文件修改的时候,自动重启服务
5,每天晚上校验托送过来的备份数据是不是完整
[root@m01-61 playbook]# cat rsync.yaml 
- hosts: backuptasks:- name: install mailxyum: name=mailx state=installed- name: configure rsynccopy: src=conf/rsyncd.conf dest=/etc/rsyncd.confnotify: Restart rsync service- name: create dir /datafile: path=/data state=directory owner=www group=www - name: create dir /backupfile: path=/backup state=directory owner=www group=www- name: create file rsync passwdcopy: content='rsync_backup:1' dest=/etc/rsync.password motd=0600- name: configure mailcopy: src=./conf/mail.rc dest=/etc/mail.rc- name: copt scripts checkcopy: src=./scripts/check_backup.sh dest=/server/scripts/check_backup.sh- name: cron rootcron: name="check client backup" minute=0 hour=2 job='/usr/bin/bash /server/scripts/check_backup.sh &> /dev/null'- name: start rsyncservice: name=rsyncd state=startedhandlers:- name: Restart rsync serviceservice: name=rsyncd state=restarted[root@m01-61 playbook]# cat ./conf/rsyncd.conf 
uid = www
gid = www
port = 873
fake super = yes 
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]
comment = welcome to backup!
path = /backup
[data]
path = /data      

编写nfs的配置文件

1,配置nfs配置文件,然后编写一旦配置文件发生改变就重启服务
2,配置sersync服务,使一更改配置文件服务就杀死上次的进程,然后重启服务[root@m01-61 playbook]# cat nfs.yaml 
- hosts: nfstasks:- name: copy sersynccopy: src=./conf/sersync dest=/usr/local recurse=yes mode=755 notify: statr sersync- name: create /datafile: path=/data state=directory owner=www group=www- name: create nfs filecopy: src=./conf/exports dest=/etc/exportsnotify: restart nfs service- name: start rpcbind rsyncservice: name=rpcbind state=started enabled=yes- name: statrt nfs startservice: name=nfs-server state=started enabled=yeshandlers:- name: restart nfs serviceservice: name=nfs state=restarted- name: statr sersyncshell: " ps aux | grep [s]ersync | awk '{print \"kill -9\"$2}' | bash && /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml"

配置web剧本

[root@m01-61 playbook]# cat web_nfs.yaml 
- hosts: webtasks:- name: mount nfsmount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted

把所有的剧本合到一起来执行

[root@m01-61 playbook]# cat all.yaml 
- import_playbook: /playbook/base.yaml
- import_playbook: /playbook/rsync.yaml
- import_playbook: /playbook/nfs.yaml
- import_playbook: /playbook/web_nfs.yaml 

转载于:https://blog.51cto.com/13447608/2280886

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389629.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

5938. 找出数组排序后的目标下标

5938. 找出数组排序后的目标下标 给你一个下标从 0 开始的整数数组 nums 以及一个目标元素 target 。 目标下标 是一个满足 nums[i] target 的下标 i 。 将 nums 按 非递减 顺序排序后,返回由 nums 中目标下标组成的列表。如果不存在目标下标,返回一…

决策树之前要不要处理缺失值_不要使用这样的决策树

决策树之前要不要处理缺失值As one of the most popular classic machine learning algorithm, the Decision Tree is much more intuitive than the others for its explainability. In one of my previous article, I have introduced the basic idea and mechanism of a Dec…

说说 C 语言中的变量与算术表达式

我们先来写一个程序&#xff0c;打印英里与公里之间的对应关系表。公式&#xff1a;1 mile1.61 km 程序如下&#xff1a; #include <stdio.h>/* print Mile to Kilometre table*/ main() {float mile, kilometre;int lower 0;//lower limitint upper 1000;//upper limi…

gl3520 gl3510_带有gl gl本机的跨平台地理空间可视化

gl3520 gl3510Editor’s note: Today’s post is by Ib Green, CTO, and Ilija Puaca, Founding Engineer, both at Unfolded, an “open core” company that builds products and services on the open source deck.gl / vis.gl technology stack, and is also a major contr…

uiautomator +python 安卓UI自动化尝试

使用方法基本说明&#xff1a;https://www.cnblogs.com/mliangchen/p/5114149.html&#xff0c;https://blog.csdn.net/Eugene_3972/article/details/76629066 环境准备&#xff1a;https://www.cnblogs.com/keeptheminutes/p/7083816.html 简单实例 1.自动化安装与卸载 &#…

5922. 统计出现过一次的公共字符串

5922. 统计出现过一次的公共字符串 给你两个字符串数组 words1 和 words2 &#xff0c;请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。 示例 1&#xff1a;输入&#xff1a;words1 ["leetcode","is","amazing","as",&…

Python+Appium寻找蓝牙/wifi匹配

前言&#xff1a; 此篇是介绍怎么去寻找蓝牙&#xff0c;进行匹配。主要2个问题点&#xff1a; 1.在不同环境下&#xff0c;搜索到的蓝牙数量有变 2.在不同环境下&#xff0c;搜索到的蓝牙排序会变 简单思路&#xff1a; 将搜索出来的蓝牙名字添加到一个list去&#xff0c;然后…

power bi中的切片器_在Power Bi中显示选定的切片器

power bi中的切片器Just recently, while presenting my session: “Magnificent 7 — Simple tricks to boost your Power BI Development” at the New Stars of Data conference, one of the questions I’ve received was:就在最近&#xff0c;在“新数据之星”会议上介绍我…

字符串匹配 sunday算法

#include"iostream" #include"string.h" using namespace std;//BF算法 int strfind(char *s1,char *s2,int pos){int len1 strlen(s1);int len2 strlen(s2);int i pos - 1,j 0;while(j < len2){if(s1[i j] s2[j]){j;}else{i;j 0;}}if(j len2){…

5939. 半径为 k 的子数组平均值

5939. 半径为 k 的子数组平均值 给你一个下标从 0 开始的数组 nums &#xff0c;数组中有 n 个整数&#xff0c;另给你一个整数 k 。 半径为 k 的子数组平均值 是指&#xff1a;nums 中一个以下标 i 为 中心 且 半径 为 k 的子数组中所有元素的平均值&#xff0c;即下标在 i …

Adobe After Effects CS6 操作记录

安装 After Effects CS6 在Mac OS 10.12.5 上无法直接安装, 需要浏览到安装的执行文件后才能进行 https://helpx.adobe.com/creative-cloud/kb/install-creative-suite-mac-os-sierra.html , 但是即使安装成功, 也不能正常启动, 会报"You can’t use this version of the …

数据库逻辑删除的sql语句_通过数据库的眼睛查询sql的逻辑流程

数据库逻辑删除的sql语句Structured Query Language (SQL) is famously known as the romance language of data. Even thinking of extracting the single correct answer from terabytes of relational data seems a little overwhelming. So understanding the logical flow…

好用的模块

import requests# 1、发get请求urlhttp://api.xxx.xx/api/user/sxx_infodata{stu_name:xxx}reqrequests.get(url,paramsdata) #发get请求print(req.json()) #字典print(req.text) #string,json串# 返回的都是什么# 返回的类型是什么# 中文的好使吗# 2、发请求posturlhttp://api…

5940. 从数组中移除最大值和最小值

5940. 从数组中移除最大值和最小值 给你一个下标从 0 开始的数组 nums &#xff0c;数组由若干 互不相同 的整数组成。 nums 中有一个值最小的元素和一个值最大的元素。分别称为 最小值 和 最大值 。你的目标是从数组中移除这两个元素。 一次 删除 操作定义为从数组的 前面 …

BZOJ4127Abs——树链剖分+线段树

题目描述 给定一棵树,设计数据结构支持以下操作 1 u v d  表示将路径 (u,v) 加d 2 u v 表示询问路径 (u,v) 上点权绝对值的和 输入 第一行两个整数n和m&#xff0c;表示结点个数和操作数接下来一行n个整数a_i,表示点i的权值接下来n-1行,每行两个整数u,v表示存在一条(u,v)的…

数据挖掘流程_数据流挖掘

数据挖掘流程1-简介 (1- Introduction) The fact that the pace of technological change is at its peak, Silicon Valley is also introducing new challenges that need to be tackled via new and efficient ways. Continuous research is being carried out to improve th…

北门外的小吃街才是我的大学食堂

学校北门外的那些小吃摊&#xff0c;陪我度过了漫长的大学四年。 细数下来&#xff0c;我最怀念的是…… &#xff08;1&#xff09;烤鸡翅 吸引指数&#xff1a;★★★★★ 必杀技&#xff1a;酥流油 烤鸡翅有蜂蜜味、香辣味、孜然味……最爱店家独创的秘制鸡翅。鸡翅的外皮被…

786. 第 K 个最小的素数分数

786. 第 K 个最小的素数分数 给你一个按递增顺序排序的数组 arr 和一个整数 k 。数组 arr 由 1 和若干 素数 组成&#xff0c;且其中所有整数互不相同。 对于每对满足 0 < i < j < arr.length 的 i 和 j &#xff0c;可以得到分数 arr[i] / arr[j] 。 那么第 k 个…

[LeetCode]最长公共前缀(Longest Common Prefix)

题目描述 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀&#xff0c;返回空字符串 ""。 示例 1:输入: ["flower","flow","flight"]输出: "fl"示例 2:输入: ["dog","racecar",&quo…

域嵌套太深_pyspark如何修改嵌套结构域

域嵌套太深In our adventures trying to build a data lake, we are using dynamically generated spark cluster to ingest some data from MongoDB, our production database, to BigQuery. In order to do that, we use PySpark data frames and since mongo doesn’t have …