Ansible

Ansible Role 持续交付 之【deploy-image

2018-06-12  本文已影响21人  lework

Ansible Role: deploy-image

部署docker image

要求

测试环境

ansible 2.5.3

os Centos 7.4.1708 X64

docker 18.03.1-ce

角色变量

# 代码文件
deploy_service_file: ""

# 代码路径
deploy_service_path: ""

# 应用的端口
deploy_service_port: ""

# 等待多少秒检测端口
deploy_service_port_delay: 1

# 检测端口超时时间
deploy_service_port_timeout: 90

# image的名称
deploy_image_name: ""

# image的tag
deploy_image_tag: "{{ deploy_image_name }}:latest"

# image的Dockerfile
deploy_image_dockerfile: ""

# 网络
deploy_container_network: "default"

# container的名称
deploy_container_name: "{{ deploy_image_name }}"

# container的主机名
deploy_container_hostname: "{{ deploy_image_name }}"

# container端口绑定
deploy_container_publish: 
  - "{{ deploy_service_port}}:{{ deploy_service_port}}"

# container目录绑定
deploy_container_volume:
  - "/tmp/:/tmp/"
  
deploy_docker_bin: "/usr/bin/docker"
# image编译脚本
deploy_image_build: >
  {{ deploy_docker_bin }} build --no-cache 
  --build-arg APP_FILE={{ deploy_service_file }} 
  --build-arg SERVER_PORT={{ deploy_service_port }} 
  -f {{ deploy_image_dockerfile}} 
  -t {{ deploy_image_tag }}
  {{ deploy_service_path }}
  
# image上传脚本
deploy_image_push: >
  {{ deploy_docker_bin }} push {{ deploy_image_tag }}

# image拉取脚本
deploy_image_pull: >
  {{ deploy_docker_bin }} pull {{ deploy_image_tag }}
  
# container关闭命令
deploy_container_stop: >
  {{ deploy_docker_bin }} stop {{ deploy_container_name }} &&
  {{ deploy_docker_bin }} rm {{ deploy_container_name }} ||
  echo error

# container运行命令
deploy_container_run: >
  {{ deploy_docker_bin }} run -tid
  --net {{ deploy_container_network }}
  --name {{ deploy_container_name }}
  -h {{ deploy_container_hostname }}
  {% for p in deploy_container_publish %}-p {{ p }} {% endfor %}
  {% for v in deploy_container_volume %}-v {{ v }} {% endfor %}
  --restart=always
  {{ deploy_image_tag }}

# 清除无用的镜像
deploy_clear: true
deploy_clear_run: >
  {{ deploy_docker_bin }} rmi --force $({{ deploy_docker_bin }} images | grep 'none' | awk '{print $3}')

# 验证url
deploy_verify_url: ""

# 是否回滚
deploy_rollback: false

依赖

github地址

https://github.com/kuailemy123/Ansible-roles/tree/master/deploy-image

Example Playbook

# 发布image
- hosts: docker3
  serial: "50%"
  gather_facts: false
  vars:
   - deploy_service_file: "test.war"
   - deploy_service_path: "/data/jenkins/workspace/test/dist/"
   - deploy_service_port: 26381
   - deploy_image_name: "test"
   - deploy_image_tag: "127.0.0.1/docker3/test:master"
   - deploy_image_dockerfile: "/opt/images/Dockerfile_tomcat7"
   - deploy_container_network: "test"
   - deploy_container_publish:
     - "26381:26381"
   - deploy_container_volume:
     - "/tmp/:/tmp/"
     - "/var/log/test:/usr/local/tomcat/logs"
  roles:
    - deploy-image

# 回滚image
- hosts: docker3
  serial: "50%"
  gather_facts: false
  vars:
   - deploy_rollback: true
   - deploy_service_port: 26381
   - deploy_image_name: "test"
   - deploy_image_tag: "127.0.0.1/docker3/test:v1.0"
   - deploy_container_network: "test"
   - deploy_container_publish:
     - "26381:26381"
   - deploy_container_volume:
     - "/tmp:/tmp"
     - "/var/log/test:/usr/local/tomcat/logs"
  roles:
    - deploy-image
上一篇下一篇

猜你喜欢

热点阅读