【问题标题】:How to speed up local action with loop (with_items or with_sequence)如何使用循环(with_items 或 with_sequence)加速本地操作
【发布时间】:2017-12-03 05:47:53
【问题描述】:

鉴于以下剧本:

---
- name: test local_action with_items
  hosts: localhost
  gather_facts: false
  tasks:
    - name: "add something to a file"
      shell: echo '{{item}}' >> foo.txt
      with_items:
      - "aaaaa"
      - "aaaaa"
# using 40 items

---
- name: test local_action with_items
  hosts: localhost
  gather_facts: false
  tasks:
    - name: "add something to a file"
      shell: echo '{{item}}' >> foo.txt
      with_sequence: count=40

后一个剧本运行 5 秒。

Using a bash loop is obviously much(1000 倍)更快,耗时 5 毫秒:

time for i in $(seq 1 40); do echo $i >> foo.txt;     done

现在很明显,Ansible 有一些开销,但有没有可能加快这个速度?

【问题讨论】:

  • Ansible 不应该是闪电般的速度,它应该是易于使用且舒适的。而且您通常使用 Ansible 将基础架构部署从几天缩短到几分钟,而不是将 echo-loops 从几分钟缩短到几秒。
  • @KonstantinSuvorov:当然可以,但是复杂的剧本可能包含许多 lineinfile 条目,如果周转时间可以减少几分钟,它可能会很有用。如果这个循环需要 100 毫秒或 500 毫秒...
  • 我希望在您的示例中看到关键字local_action

标签: ansible


【解决方案1】:

使用raw 模块代替shell 模块。它将与 bash 循环一样快。

---
- name: test local_action with_items
  hosts: localhost
  gather_facts: false
  tasks:
    - name: "add something to a file"
      raw: echo '{{item}}' >> foo.txt
      with_sequence: count=40
...

无论如何,如果您想要性能,也许可以用 C 编写代码。

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2019-12-27
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多