【docker】jenkins+ansible批量部署

前言

  • jenkins操作ansible生成容器批量部署

流程

  • 制作playbook
vim playbook.yml
---
- hosts: all
  remote_user: root
  vars:
    timestamp: 20200625233149
  tasks:
    - name: docker pull new images
      shell: 'chdir=~ docker pull 172.16.81.150:8082/fe/nginx-fe-{{timestamp}}'
    - name: docker rmf
      shell: 'chdir=~ docker ps | grep jenkins-test && docker rm -f jenkins-test'
      ignore_errors: true
    - name: docker run
      shell: 'chdir=~ docker run -p 80:80 -itd --name jenkins-test 172.16.81.150:8082/fe/nginx-fe-{{timestamp}}'
  • 制作dockerfile
FROM centos:7
RUN yum -y install wget curl vim openssh-clients
RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum clean all
RUN yum makecache
# 拷贝公钥私钥进镜像内
COPY ssh /root/.ssh/
# 公钥私钥赋权
RUN chmod 755 ~/.ssh/
RUN chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
RUN yum -y install ansible
# 拷贝主机组进ansible目录
COPY hosts /etc/ansible/
# 关闭known_hosts校验
RUN sed -i 's/^#host_key_checking = False/host_key_checking = False/' /etc/ansible/ansible.cfg
RUN ansible --version
# 拷贝playbook进镜像内
COPY playbook.yml /root/
  • 生成镜像
docker build -t ansible:t1 .
  • 修改jenkins脚本:
timestamp=`date '+%Y%m%d%H%M%S'`

node -v
npm -v

npm install -g cnpm --registry=https://registry.npm.taobao.org

cnpm install

npm run build

(docker ps | grep ansible) && (docker rm -f ansible)

docker build -t 172.16.81.150:8082/fe/nginx-fe-$timestamp .
docker push 172.16.81.150:8082/fe/nginx-fe-$timestamp

docker run -id --name ansible ansible:t1

# 检查playbook语法
docker exec -i ansible ansible-playbook --syntax-check /root/playbook.yml
# playbook执行
docker exec -i ansible ansible-playbook -e "timestamp=$timestamp" /root/playbook.yml 

docker rm -f ansible

你可能感兴趣的:(docker,linux)