首页 > 解决方案 > 在滚动部署中导入 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 的参数应用滚动部署?换句话说,我需要将所有项目运行到批量大小的每个元素(除了只有一次的构建,但我可以忍受完全提取它)

标签: ansiblecontinuous-deployment

解决方案


如您所见,剧本不能包含在剧本中,您可以将其包含在顶层:因为剧本是完整的个人剧本,具有自己的参数,例如主机和批量大小。

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


推荐阅读