# when条件语句 https://blog.51cto.com/linuxg/1775166 # Ansible Playbook Roles 实战案例 https://www.jianshu.com/p/157ed21bf47d # 指定ip执行 ansible-playbook -i /tmp/prod-server-list -v /srv/ansible/ohn/supervisor-monitor.yml -e 'hosts=prod' # 传变量 https://www.cnblogs.com/f-ck-need-u/p/7571974.html [root@OP supervisor-monitor]# cat /tmp/prod-server-list-tmp [prod] 10.104.54.234 instance=上海-mq-cron 10.104.138.126 instance=sht-new # 把变量传过去使用 ansible prod -i /tmp/prod-server-list-tmp -m shell -a 'echo "{{instance}}" > /tmp/11' # 指定 key ansible-playbook deploy_inin_java.yml -e "hosts=dev" --private-key=./new_www.key # 执行执行结果成功—— 正常退出 - name: check sonar-project.properties exists shell: ls /data/server/php/ register: result ignore_errors: yes connection: local - debug: var=result - meta: end_play when: result is succeeded # 检查端口 - hosts: "{{ deploy_host }}" gather_facts: False # 不使用缓存 serial: "50%" # 可以使用serial关键字定义Ansible一次应管理多少主机 tasks: - name: "restart service" shell: "supervisorctl update {{ pj }};supervisorctl restart {{ pj }}" - name: "wait for {{ port }}..." wait_for: "port={{ port }} timeout=60" when: env in ["prod","gray","pre"] and pj != 'socket' # 判断文件是否存在 不存在创建 - name: Register file stat: path: "/tmp/test_file" register: file_path - name: Create file if it doesn't exists file: path: "/tmp/test_file" state: touch when: file_path.stat.exists == False - name: Check that if the file devnet.md exists debug: msg: "File exists." when: file_status.stat.exists == True