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
分类:
ansible
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通