標籤:component 使用者 bin rzsz directory oca sea exp 後台運行
第1章 思考1.1如何一鍵使用ansible搭建1.1.1第一步目標
首先我們要明確的是我們需要實現的是一鍵的自動化指令碼,既執行一個指令碼然後就自動化安裝nfs+rsync+sersync,然後讓web01自動掛載nfs共用目錄,
1.1.2第二步關係明確及順序
既然有目標了那麼我們需要做的就是我們需要明白自動化安裝的先後順序。
-
既然我們是需要ansible才能自動化安裝那麼我們就需要先安裝ansible
-
安裝了ansible之後,既然我們想要掛載,那麼我們就需要有nfs共用,和rsync備份。平級
-
在安裝了nfs和rsync之後我們就需要的是sersync來實現即時的備份。
-
在都安裝了之後我們最後做的才是需要的web01用戶端的掛載
- 1.3Ansible
Ansible是基於ssh-key的。所以我們需要的ssh秘鑰登入無密碼。但是如果想要非互動式秘鑰認證登入那麼我們就需要sshpass而這個包則是ansible才能提供的(也可以直接yum安裝),而如果要安裝ansible那麼需要清楚的是ansible是基於erel源的所以步驟如下
-
epel
-
yum ansible
-
秘鑰認證
-
安裝ansible
-
修改設定檔
-
判斷是否成功管理
- 1.4Rsync
Rsync是實現備份傳輸的,他的安裝只需要安裝rsync,建立設定檔,建立密碼檔案,建立使用者,修改許可權,啟動rsync
1.1.5Nfs
這裡需要明確一下的就是nfs是基於rpcbind的,所以我們需要先安裝的是rpcbind+nfs,然後我們需要明確原理nfs啟動需要找rpcbind,來實現的,所以我們需要先啟動rpcbind,在啟動nfs
1.1.6Sersync
要實現sersync就需要有這個包,所以我們直接在指令碼rz就好,但是系統預設不帶rz命令所以我們需要先安裝lrzsz(我對系統最佳化內建rz命令)。然後修改設定檔後台運行。
1.1.7Web01
這裡web01只是當了一個用戶端所以我們只在他這掛載就好,不需要別的
第2章 部署安裝2.1目錄規範
|
指令碼 |
/server/scripts |
|
安裝包 |
/opt/src |
|
安裝目錄 |
/usr/local |
|
Ansible的yml檔案 |
/etc/ansible/yml |
2.2執行過程
只需要執行install.sh就好
2.3個個指令碼的建立2.3.1Install指令碼(只需要執行這個指令碼就好)
#!/bin/bsh
sh Automatic_deployment_ansible.sh
mkdir -p /opt/src
echo "pleace rz rsync to /opt/src"
cd /opt/src
rz
if [ -f /opt/src/ser*.tar.gz ]
then
mkdir /etc/ansible/yml
cd /etc/ansible/yml
rz
echo "pleace input you install.yml,"
ansible-playbook index.yml
else
echo "you can not true sersync to /opt/src tryagent"
fi
2.3.2安裝ansible指令碼
#!/bin/bash
#just prevent you have ssh key
rm -rf /root/.ssh/id_dsa* >> /dev/null2>&1
ssh-keygen -t dsa -f /root/.ssh/id_dsa -P"" >> /dev/null 2>&1
#載入repo源
wget -O /etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-6.repo && \
#all can ssh each other
yum install ansible -y >> /dev/null2>&1 && \
echo "install ansible"
#judge you ansible install
if [ "`rpm -qa | grep -o ansible`" =="ansible" ]
then
for ip in web01 rsync nfs01
do
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-oStrictHostKeyChecking=no [email protected]$ip" >> /dev/null 2>&1
#justfor you Determine whether distribution of success of ssh
export a=$(ssh $ip hostname -I|awk ‘{print $1"|"$2}‘)
export b=$(cat /etc/hosts | grep $ip | awk ‘{print $1}‘)
done
if [["$b" =~ $a ]]
then
#insert you group of ansible
# read -p "pleaceinsert you ansible group name: " grop
# read -p "pleaceinsert The name of the group members just like one two ...: " arg
cat >> EOF<< /etc/ansible/hosts
[ls]
web01
nfs01
rsync
EOF
#judge you can useansible
ansible ls -m command-a "hostname" 2>> /var/log/ansible.log
if [ $? -ne 0 ]
then
echo"ERROR:you can not use ansible,pleace see /var/log/ansible.log"
else
echo"congratulations!!you ansible successful! and now you can do youwant"
fi
else
echo "you fenfa falit"
fi
else
echo "error:you can not install ansible"
fi
2.3.3Yml大量安裝的檔案
---
- hosts: ls #$(tail /etc/ansible/hosts | sed -rn‘s#^\[(.*)\]$#\1#gp‘)
tasks:
- name:copy
copy:src=/etc/hosts dest=/etc/hosts
- hosts: rsync
tasks:
- name:install rsync
script:/server/scripts/Automatic_deployment_rsync.sh
- hosts: nfs01
tasks:
- name:install nfs01
script:/server/scripts/Automatic_deployment_nfs.sh
- hosts: nfs01
tasks:
- name:copy sersync
copy:src=/opt/src dest=/opt/
- name:install sersync
script:/server/scripts/Automatic_deployment_sersync.sh
- hosts: web01
tasks:
- name :mount
command: mount 172.16.1.31:/backup /mnt
2.3.4安裝rsync指令碼
#!/bin/bash
#if install rsync
dir=/etc/rsyncd
#if install already and start we neet to stop rsyncand rm pid file
ps -ef | grep rsync | awk -F" " ‘{print$2}‘ >> /dev/null 2<&1
rm -rf /var/run/rsyncd.pid
#i need create configuration directory
mkdir -p $dir
# Determine whether the installation rsync
if [ "`rpm -qa | grep -o rsync`" !="rsync" ]
then
yum install rsync -y >> /dev/null 2>&1
if [ "`rpm -qa | grep -o rsync`" !="rsync" ]
then
echo"can not install rsync pleace see you network"
fi
fi
#Enter the directory
cd $dir
#Create a configuration file
cat >EOF << rsyncd.conf
uid = rsync
gid = rsync
use chroot =no
maxconnections = 200
timeout =300
pid file =/var/run/rsyncd.pid
lock file =/var/run/rsync.lock
log file =/var/log/rsyncd.log
[backup]
path =/backup/
ignoreerrors
read only =false
list = false
hosts allow= 172.16.1.0/24
hosts deny =0.0.0.0/32
auth users =rsync_backup
secrets file= /etc/rsyncd/password
EOF
#Determine whether a user
user=`id rsync`
if [ $? -eq 0 ]
then
chown -Rrsync.rsync /etc/rsyncd
else
useradd -s/sbin/nologin -M rsync
fi
#Create a password file
echo "rsync_backup:123456" >/etc/rsyncd/password
#give permissions password file
chmod 600 /etc/rsyncd/password
chown root.root $dir/password
mkdir -p /backup >> /dev/null 2>&1
chown rsync.rsync /backup
#start rsync
rsync --daemon --config=$dir/rsyncd.conf
2.3.5安裝sersync指令碼
#!/bin/bash
sedir=/usr/local/sersync
cd /opt/src
mkdir -p /usr/local/sersync
tar -zxvf sersync*.tar.gz -C $sedir--strip-components 1 >> /dev/null 2>&1
echo "123456" > $sedir/passwd
chmod 600 $sedir/passwd
sed -i ‘s#<localpathwatch="/opt/tongbu">#<localpathwatch="/backup">#g‘ $sedir/confxml.xml
sed -i ‘s#<remote ip="127.0.0.1"name="tongbu1"/>#<remote ip="rsync"name="backup"/>#g‘ $sedir/confxml.xml
sed -i ‘s#<auth start="false"users="root" passwordfile="/etc/rsync.pas"/>#<authstart="true" users="rsync_backup"passwordfile="/usr/local/sersync/passwd"/>#g‘ $sedir/confxml.xml
cd $sedir
./sersync2 -d -o confxml.xml >> /dev/null2>&1
2.4講解
在這裡我是通過install,sh來進行調用ansible的安裝指令碼,來先安裝ansible,在安裝並確定無錯誤之後我又調用執行了index.yml。而index.yml則是先將hosts檔案拷貝過去,然後安裝調用rsync安裝指令碼安裝,在之後調用sersync安裝指令碼,安裝sersync。在之後yml又讓ansible通過web01直接掛載使用nfs自動共用的目錄。
使用ansible自動化部署nfs+rsync+sersync+web01自動化掛載