其他分享
首页 > 其他分享> > ansible的playbook脚本示例

ansible的playbook脚本示例

作者:互联网

---
- hosts: test
  remote_user: root
  gather_facts: false
  vars:
    ansible_python_interpreter: /usr/bin/python3  #指定python环境
  serial: "100%" #更新比例
  tasks:
  - name: rsync docker-compose file
    synchronize:  
      src: /opt/{{service}}.yml 
      dest: /opt

  - name: update docker-compose template file  #替换模板文件变量
    replace:
      path: /opt/{{service}}.yml
      regexp: "{{item.regexp1}}"
      replace: "{{item.replace}}"
    with_items:
      - {regexp1: "hostip", replace: "{{ inventory_hostname }}"}

  - name: docker login
    docker_login:
      #registry: "harbor.test.com"
      registry: "192.168.1.2:8443"
      username: "test"
      password: "test"
      reauthorize: yes

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

  - name: stop target container
    shell: docker rm -f {{service}} 
  - debug:
      msg: docker rm -f {{service}}

  - name: 从本地镜像仓库重建容器
    shell: docker-compose -f /opt/{{service}}.yml up -d
  - debug:
      msg: docker-compose -f /opt/{{service}}.yml up -d

  - name: 清理无效镜像和卷
    shell: docker image prune -a -f && docker volume prune -f
  - debug:
      msg: docker image prune -a -f && docker volume prune -f

  - name: 检查容器启动成功没有
    wait_for:
      host: "{{ ansible_ssh_host }}"
      port: "{{serviceport}}"
      state: started
      timeout: 360
      delay: 2

标签:opt,compose,name,service,示例,ansible,playbook,docker,yml
来源: https://www.cnblogs.com/pgy674477230/p/16351000.html