ansible的playbook脚本示例

---
- hosts: test
  remote_user: root
  gather_facts: false
  vars:
    ansible_python_interpreter: /usr/bin/python3  #指定python环境
  serial: "50%"   #更新比例,或者是具体数量serial:1
  tasks:
  - debug:
      msg: "Execute remote ip {{ansible_ssh_host}}"

  - name: rsync docker-compose file
    synchronize:
      src: /opt/{{service}}.yml
      dest: /opt

  - name: update docker-compose template file  #循环替换compose脚本变量
    replace:
      path: /opt/{{service}}.yml
      regexp: "{{item.regexp1}}"
      replace: "{{item.replace}}"
    with_items:
      - {regexp1: "hostip", replace: "{{ inventory_hostname }}"}

  - name: docker login
    docker_login:
      registry: "192.168.1.2:8443"
      username: "test"
      password: "123456"
      reauthorize: yes

  - name: docker pull images
    shell: docker pull {{imagename}}
  - debug: msg="docker pull {{imagename}}---!"

  - name: check service container is exsit!  #检查服务容器是否存在,根据判断执行下面的when条件
    shell: docker ps -a | grep {{service}}| awk '{print $1}'
    register: cid
  - debug:
      msg: "this service container running id is {{cid.stdout}}"

  - name: check service port is listen  # 检查服务端口是否被占用
    shell: netstat -tunlp|grep {{serviceport}} | awk '{print $NF}' | awk -F'/' '{print $1}'
    register: sport
  - debug:
      msg: "this service port {{serviceport}} running process id {{sport.stdout}}"

  - fail:  # 服务端口是否被占用停止发布
      msg: "this {{service}} container is not exsit,but {{serviceport}} port is running,please check this port if be taken"
    when: cid.stdout == "" and sport.stdout != ""

  - name: halt this service #容器存在调用springboot的shutdown接口执行优雅关机
    ansible.builtin.uri:
      url: "http://{{inventory_hostname}}:{{serviceport}}/actuator/shutdown"
      method: POST
    ignore_errors: yes  #忽略异常错误
    when: cid.stdout != ""

  - name: pause waiting check service instance  #检查容器是否关闭,每3s执行一次检查,共10次
    shell: docker ps -a | grep {{service}}| grep Exited | awk '{print $1}'
    register: output
    until: "'{{output.stdout}}' != '' "
    retries: 10
    delay: 3
    ignore_errors: yes  #忽略异常错误
    when: cid.stdout != ""

  - debug:
      msg: "check service container id {{output.stdout}} if down..."
    when: cid.stdout != ""

  - debug: msg="service {{service}} is down ok"
    when: cid.stdout != ""
    #- name: pause check service
    #pause: seconds=300
  - name: remove target container  # 容器存在删除容器 参数-f处理容器停止异常执行强制删除
    shell: docker rm -f {{service}}
    when: cid.stdout != ""
  - debug:
      msg: docker rm -f {{service}}
    when: cid.stdout != ""

  - name: run docker compose
    shell: docker-compose -f /opt/{{service}}.yml up -d
  - debug:
      msg: docker-compose -f /opt/{{service}}.yml up -d


  - name: check this container is running...
    wait_for:
      host: "{{ ansible_ssh_host }}"
      port: "{{serviceport}}"
      state: started
      timeout: 360
      delay: 2

  - name: clear docker image and valume of useless
    shell: docker image prune -a -f && docker volume prune -f
  - debug:
      msg: docker image prune -a -f && docker volume prune -f
posted @ 2022-06-07 10:56  蒲公英PGY  阅读(91)  评论(0编辑  收藏  举报