提高 Ansible 性能。

5

我希望提高ansible playbook的性能。 我有一个测试playbook如下:

---
- name: Test
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Creating an empty file
      file:
        path: /tmp/hello
        state: touch

    - name: Test
      command: "echo 'Hello, World!' >> /tmp/hello"
      with_sequence: start=1 end=500
      delegate_to: localhost

运行此操作需要长达57秒之久。与执行相同操作的bash脚本相比: < p> test.sh

#!/bin/bash
touch /tmp/hello
for i in {1..500}
do
  sh /home/admin/hello.sh
  echo "This is iteration $i"
done

hello.sh

#!/bin/bash
echo "Hello, World!" >> /tmp/hello

运行该程序需要约1.5秒钟。

我已经对ansible.cfg文件进行了一些更改。

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=18000s -o PreferredAuthentications=publickey
control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r
pipelining = True

有没有其他办法可以改善这种糟糕的表现?

1个回答

4

使用你的代码,ansible会连接500次主机来运行一个命令。你可以先创建你想要的文件,然后将其上传到主机。

playbook.yml

---
- name: Test
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: create 'hello' file
      ansible.builtin.template:
        src: "../templates/hello.j2"
        dest: "/tmp/hello"

hello.j2

{% for i in range(500) %}
Hello, World!
{% endfor %}

我明白了。这对于特定情况有所帮助,但我正在寻找更通用的解决方案。我们有很多ansible playbook,我正在尝试找到一种优化它们的方法,或者理解它们为什么如此缓慢。我在问题描述中提到的playbook的目的纯粹是为了进行基准测试。 - Graeme Seaman
3
如果您想要更具体的例子,可能需要在新问题中提供。从这个回答可以看出,问题在于您必须处理Ansible中的连接开销。您可以在命令中构建循环,例如:shell: "for i in {1..500}; do echo 'Hello, World!' >> /tmp/hello; done" - β.εηοιτ.βε

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接