在滚动部署中导入剧本



我正在尝试自动化一个复杂的部署。基本上:

  • 我上传了一个新版本到一些服务器
  • 运行测试脚本
  • 在每个更新的服务器上验证结果
  • 如果成功,请在下一批中重复

这目前是手动完成的。

我有现有的剧本:

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参数应用于所有导入的剧本的滚动部署?换句话说,我需要对批量大小的每个元素运行所有项目(除了只有一次的构建,但我可以完全提取它(

正如您所看到的,剧本不能包含在剧本中,只需将其包含在最高级别即可:因为剧本是一个完整的个人剧本,有自己的参数,如主机和批量大小。

- 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

我给你的解决方案是将你的子剧本转换为角色。您可以在一个play中调用一个角色,它将在所有批处理大小上运行。剧本只是任务的集合,然后你可以使用角色,它也是任务的集合。

所以会有:

---
- 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,然后将子剧本转换为要包含的任务列表。

最新更新