在滚动部署中导入剧本
Importing playbooks in a rolling deploy
我正在尝试自动执行复杂的部署。基本上:
- 我将新版本上传到一些服务器
- 运行一个测试脚本
- 在每个更新的服务器上验证结果
- 如果成功,在下一批重复
目前这是手动完成的。
我有现成的剧本:
update.yml
start_test.sh #this one runs just locally so it does not need to be playbook
verify.yml
我只使用一本剧本进行设置时遇到问题。
正如 docs 所说,无法将剧本导入 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 不能包含在 play 中,只是您可以将它包含在一个顶层:因为 play 是一个完整的独立 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 然后将您的子剧本转换为要包含的任务列表。
我正在尝试自动执行复杂的部署。基本上:
- 我将新版本上传到一些服务器
- 运行一个测试脚本
- 在每个更新的服务器上验证结果
- 如果成功,在下一批重复
目前这是手动完成的。
我有现成的剧本:
update.yml
start_test.sh #this one runs just locally so it does not need to be playbook
verify.yml
我只使用一本剧本进行设置时遇到问题。
正如 docs 所说,无法将剧本导入 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 不能包含在 play 中,只是您可以将它包含在一个顶层:因为 play 是一个完整的独立 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 然后将您的子剧本转换为要包含的任务列表。