【问题标题】:Ansible built-in COPY task - does it run only in serial manner?Ansible 内置 COPY 任务 - 它仅以串行方式运行吗?
【发布时间】:2021-02-12 13:48:44
【问题描述】:

我找不到任何明确说明这一点的文档,但是如果我有一个具有复制任务并在多个主机上同时触发的剧本,我注意到当涉及到 COPY 任务时,只有一个 play 执行一次。所有其他主机上的 Playbook 执行暂停,然后在第一个主机完成时其中一个恢复。以此类推。

是否有人可以指出任何文档,说明 COPY 将独占运行?我在想 Ansible 可以通过这种方式实现它,以避免磁盘寻道等效率低下。任何指针?

(如果它确实默认串行运行,有没有办法并行运行它们?当然前提是它不会适得其反)。

这是我的一个示例任务。这会将单个 ISO 映像(从控制器节点)复制到多个目标主机。我正在使用 Ansible v2.9.4

- name: Copying binaries to target hosts
  copy:
    src: "{{ binary_local_path }}"
    dest: "{{ binary_remote_path }}"
  register: result

【问题讨论】:

    标签: ansible ansible-2.x


    【解决方案1】:

    默认情况下,任务并行运行。使用ansible-runner。它将为您提供details。例如,给定一本剧本

    shell> cat test-copy/test-28.yml 
    - hosts: test_01,test_02,test_03
      tasks:
        - copy:
            src: /etc/passwd
            dest: /tmp/test
    

    命令

    shell> ansible-runner run test-copy -i ID01 -p test-28.yml
    

    创建细节

    shell> tree test-copy/artifacts/ID01/
    test-copy/artifacts/ID01/
    ├── command
    ├── fact_cache
    ├── job_events
    │   ├── 10-c9ee2f18-9f8e-4b51-a5a5-81c7353131fb.json
    │   ├── 1-45a882a8-a95d-4026-a374-fa3be0dc26bb.json
    │   ├── 2-3d614ae4-866b-7e82-3e7a-000000000008.json
    │   ├── 3-3d614ae4-866b-7e82-3e7a-00000000000a.json
    │   ├── 4-4eb0b550-3537-4120-9fad-6ed5a7703f0d.json
    │   ├── 5-e305a7d2-d3bd-409b-963e-4b214a6a129b.json
    │   ├── 6-6949e8cc-458a-4626-b2cb-68a244e308af.json
    │   ├── 7-e1049b61-5c27-4627-b5b0-181533cb458a.json
    │   ├── 8-722940a2-1edd-45b7-8418-697e6f728000.json
    │   └── 9-b5983d4c-6973-4626-93ba-462c00cbfa78.json
    ├── rc
    ├── status
    └── stdout
    
    2 directories, 14 file
    

    查看 job_events。简化分析。安装角色 vbotka.ansible_lib

    shell> ansible-galaxy role install vbotka.ansible_lib
    

    并使用剧本中的任务al_runner_events

    shell> cat test-29.yml
    - hosts: localhost
      vars:
        al_runner_events_dir: test-copy/artifacts/ID01/job_events
      tasks:
        - include_role:
            name: vbotka.ansible_lib
            tasks_from: al_runner_events
        - debug:
            msg: "{{ item.counter }}
                  {{ item.event_data.host }}
                  {{ item.event_data.task }}
                  start: {{ item.event_data.start }}
                  end: {{ item.event_data.end }}"
          loop: "{{ al_runner_events_list|sort(attribute='counter') }}"
          loop_control:
            label: "{{ item.counter }}"
          when: item.event_data.duration|default(0) > 0
    

    给予

    shell> ansible-playbook test-29.yml | grep msg
      msg: '7 test_03 copy start: 2020-10-30T07:19:55.541914 end: 2020-10-30T07:20:00.078211'
      msg: '8 test_01 copy start: 2020-10-30T07:19:55.520058 end: 2020-10-30T07:20:00.239671'
      msg: '9 test_02 copy start: 2020-10-30T07:19:55.527841 end: 2020-10-30T07:20:00.460820'
    

    可以看到 copy 任务是并行运行的。


    如果你设置serial: 1

    shell> cat test-copy/test-28.yml
    - hosts: test_01,test_02,test_03
      serial: 1
      tasks:
        - copy:
            src: /etc/passwd
            dest: /tmp/test
    

    复制任务将串行运行

    shell> ansible-playbook test-29.yml | grep msg
      msg: '5 test_01 copy start: 2020-10-30T10:12:55.830801 end: 2020-10-30T10:12:59.696363'
      msg: '9 test_02 copy start: 2020-10-30T10:12:59.727570 end: 2020-10-30T10:13:02.787844'
      msg: '13 test_03 copy start: 2020-10-30T10:13:02.820704 end: 2020-10-30T10:13:05.835439'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      相关资源
      最近更新 更多