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.pswp.cn/news/389629.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389629.shtml
英文地址,請注明出處:http://en.pswp.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 …