如何渗入循环运行的 Ansible 复制任务?

问题描述 投票:0回答:1

下面是我的剧本,它搜索所有

*.txt
文件并使用复制任务获取每个文件的副本。

---
- name: Backup txt files
  hosts: remotehost
  become: true
  tasks:
    - name: List txt files in /opt/cusfiles/
      find:
        paths: /opt/cusfiles/
        patterns: '*.txt'
      register: txt_files

    - name: Backup txt files
      copy:
        src: "{{ item.path }}"
        dest: "{{ item.path }}.bkp"
      with_items: "{{ txt_files.files }}"

该 playbook 在 Ansible Tower 上运行,并且结果在 playbook 运行中共享。

问题是当文件数量超过 500+ 时,此任务需要超过 25 分钟。

通过使用

raw
模块而不是
copy module

,我能够减少复印时间
raw: "cp {{ item.path }} {{ item.path }}.bkp"

但是此任务仍然需要 15-20 分钟。

我可以使用 Ansible 或 UNIX 技能缩短完成运行的时间吗?

performance loops shell time ansible
1个回答
0
投票

您可以将任务委托给 python 脚本。使用 python 脚本(或任何其他支持并发的语言)。

从你的剧本来看,它看起来像这样:

- name: Run Python script with Ansible
  hosts: your_target_servers
  tasks:
    - name: Execute Python script
      command: python /path/to/your/script.py
© www.soinside.com 2019 - 2024. All rights reserved.