提高 Ansible 性能

Gra*_*man 5 performance ansible

我正在尝试提高 ansible 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
Run Code Online (Sandbox Code Playgroud)

运行这需要惊人的 57 秒。与执行相同操作的 bash 脚本相比:

测试文件

#!/bin/bash
touch /tmp/hello
for i in {1..500}
do
  sh /home/admin/hello.sh
  echo "This is iteration $i"
done
Run Code Online (Sandbox Code Playgroud)

你好.sh

#!/bin/bash
echo "Hello, World!" >> /tmp/hello
Run Code Online (Sandbox Code Playgroud)

这需要大约 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
Run Code Online (Sandbox Code Playgroud)

我还能做些什么来改善这种糟糕的表现?

Ron*_*den 4

使用您的代码,ansible 将连接到主机 500 次以运行命令。您可以先创建所需的文件,然后将其上传到主机。

剧本.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"
Run Code Online (Sandbox Code Playgroud)

你好.j2

{% for i in range(500) %}
Hello, World!
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

  • @GraemeSeaman,那么你必须给我们一个更具体的例子,可能是在一个新问题中。正如这个答案所述,问题是您必须在 Ansible 中处理连接开销。您可以做的是在命令中构建循环: `shell: "for i in {1..500}; do echo 'Hello, World!' >> /tmp/hello; 完成"` (3认同)