1. 程式人生 > 實用技巧 >ansible 自動部署工具

ansible 自動部署工具

centos安裝

yum install epel-release -y

yum install ansible –y

yum -y install ansible

vi /etc/ansible/ansible.cfg

修改become=true 和相關的配置 (設定shell執行使用者為root)

vi /etc/ansible/hosts

[test]
39.111.222.333
39.444.555.666

(設定自動部署的子機器)

ssh-keygen
ssh-copy-id [email protected]
ssh-copy-id [email protected]

生成證書並傳輸到子伺服器

因為我用的是golang,所以優雅停止服務的寫法是


signal.Notify(syscall.相應的關閉訊號)

再監聽函式中
<-signalChan
//這裡是服務停止了,把請求入口給關了的操作
time.Sleep(time.Minute)//等待一分鐘,讓所有請求完畢


至於部署指令碼,我這裡就不寫了。思路就是

首先將服務檔案傳輸到指定的組(force是false,不覆蓋,這一步是讓新機器能夠部署上程式碼的,檔案許可權是777)

ansible test -m copy -a 'src=/root/xxx dest=xxxx force=false mode=777'

ansible test -m copy -a 'src=/etc/systemd/system/xxx.service dest=/etc/systemd/system/xxxx.service force=false mode=777'

ansible test -m command -a 'systemctl enable /etc/systemd/system/xxxxx.service'

echo "開始停止服務"

ansible test -m command -a 'service xxxx stop'

echo "停止結束 開始部署"

ansible test -m copy -a 'src=/root/xxxx dest=/root/xxxx'

echo "部署結束,開始啟動"

ansible test -m command -a 'service xxxxx start'

echo "啟動結束,部署完畢"