3分钟
06 结合滚动更新测试
如果你已经读到 委托,滚动更新,本地动作 滚动更新的扩展好处可以迅速变得明显, 你可以使用 playbook 运行的成功或失败来决定是否增加机器到负载均衡器中.
这是嵌入测试的显著结果:
---
- hosts: webservers
serial: 5
pre_tasks:
- name: take out of load balancer pool
command: /usr/bin/take_out_of_pool {{ inventory_hostname }}
delegate_to: 127.0.0.1
roles:
- common
- webserver
- apply_testing_checks
post_tasks:
- name: add back to load balancer pool
command: /usr/bin/add_back_to_pool {{ inventory_hostname }}
delegate_to: 127.0.0.1在上述过程中, “task out of the pool” 和 “add back” 的步骤将会代替 Ansible 调用负载均衡模块或对应的 shell 命令. 您还可以使用监控模块对机器进行创建和关闭中断的窗口.
然而, 你可以从上面看出测试作为入口 – 如果”apply_testing_checks”这一步不执行, 这个机器将不会被添加到池中.
阅读关于”max_fail_percentage”的代表章节, 你可以控制有多少失败的测试后停止程序的滚动更新.
这种方式也可以被修改为先在测试机器上执行测试步骤然后在远程机器执行测试的步骤:
---
- hosts: webservers
serial: 5
pre_tasks:
- name: take out of load balancer pool
command: /usr/bin/take_out_of_pool {{ inventory_hostname }}
delegate_to: 127.0.0.1
roles:
- common
- webserver
tasks:
- script: /srv/qa_team/app_testing_script.sh --server {{ inventory_hostname }}
delegate_to: testing_server
post_tasks:
- name: add back to load balancer pool
command: /usr/bin/add_back_to_pool {{ inventory_hostname }}
delegate_to: 127.0.0.1在上面的例子中, 从测试服务器上执行一个脚本紧接着将一个远程的节点添加到 pool 中.
在出现问题时, 解决一些服务器不能使用 Ansible 自动生成的重试文件重复部署这些服务器.
学员评价