ansible - 在滚动部署中导入 playbook
问题描述
我正在尝试自动化复杂的部署。基本上:
- 我将新版本上传到某些服务器
- 运行测试脚本
- 在每个更新的服务器上验证结果
- 如果成功,请在下一批重复
目前这是手动完成的。
我有现有的剧本:
update.yml
start_test.sh #this one runs just locally so it does not need to be playbook
verify.yml
我只有一个剧本来设置这个问题。正如文档所说,无法将剧本导入该tasks
部分。因此,这:
---
- hosts: all
gather_facts: no
serial: 3
vars:
port: 9009
tasks:
- name: Build
shell: ../compile.sh
register: compile_out
failed_when: "'OK' not in compile_out.stdout"
delegate_to: 127.0.0.1
run_once: true
- name: Update batch
import_playbook: update.yml -e "port={{ port }}"
- name: Run test
shell: ./start_test.sh
- name: Verify
import_playbook: verify.yml
不起作用。
但是,如果我将导入的项目移动到顶层,我假设顶层serial
参数并不适用于所有步骤,因为它只适用于第一个剧本(“ Build
”)。
---
- hosts: all
gather_facts: no
serial: 3
vars:
port: 9009
tasks:
- name: Build
shell: ../compile.sh
register: compile_out
failed_when: "'OK' not in compile_out.stdout"
delegate_to: 127.0.0.1
run_once: true
- name: Update batch
import_playbook: update.yml -e "port={{ port }}"
- name: Run test
shell: ./start_test.sh
- name: Verify
import_playbook: verify.yml
如何使用serial
应用于所有导入 playbook 的参数应用滚动部署?换句话说,我需要将所有项目运行到批量大小的每个元素(除了只有一次的构建,但我可以忍受完全提取它)
解决方案
如您所见,剧本不能包含在剧本中,您可以将其包含在顶层:因为剧本是完整的个人剧本,具有自己的参数,例如主机和批量大小。
- hosts: localhost
tasks:
- debug:
msg: play1
- name: Include a play after another play
import_playbook: otherplays.yaml
- name: This DOES NOT WORK
hosts: all
tasks:
- debug:
msg: task1
- name: This fails because I'm inside a play already
import_playbook: stuff.yaml
我为您提供的解决方案是将您的子剧本转换为角色。您可以在戏剧中调用一个角色,它将在所有批量大小上运行。剧本只是任务的集合,然后您可以使用角色,它也是任务的集合。
所以会有:
---
- hosts: all
gather_facts: no
serial: 3
vars:
port: 9009
pre_tasks:
- name: Build
shell: ../compile.sh
register: compile_out
failed_when: "'OK' not in compile_out.stdout"
delegate_to: 127.0.0.1
run_once: true
roles:
- update
- verify
然后,在第一个角色(更新)结束时执行 start_test.sh 脚本。或者,您也可以将其转换为角色。
您还可以使用 include_tasks,然后将您的子剧本转换为要包含的任务列表。
推荐阅读
- javascript - 从两点经纬度旋转 div 线
- python - 如何正确获取python类型的变量?
- javascript - 如何在 javascript 中创建删除/删除功能按钮?可以删除元素的按钮
- python - 假设符号在 SymPy 中具有整数值
- javascript - 添加主屏幕图标以创建反应应用程序时遇到问题
- c# - 具有多个 DbContext 的 Code First 自动迁移
- python - 我的代码中的错误在哪里尝试使用 python 比较不区分大小写
- dynamic - 级联动态选择列表的动态操作在列表值更改时触发,不仅在用户从列表中选择值时
- python - 从 Excel 工作表写入表格
- selenium - TestNg 中的静态和非静态