ansible 自动部署工具

centos安装

 

yum install epel-release -y

yum install ansible –y

yum -y install ansible

 

vi /etc/ansible/ansible.cfg

修改become=true 和下面的become (设置shell执行用户为root,脱掉注释就好)

 

vi /etc/ansible/hosts

[test]
39.111.222.333
39.444.555.666

(设置自动部署的子机器)

 

ssh-keygen
ssh-copy-id root@39.111.222.333
ssh-copy-id root@39.444.555.666

生成证书并传输到子服务器

 

 

因为我用的是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 "启动结束,部署完毕"

 

 

详细的用法可以参考:https://www.cnblogs.com/keerya/p/7987886.html#_label2_0_1

posted @ 2020-12-29 15:56  愚智  阅读(109)  评论(0编辑  收藏  举报