gpt4 book ai didi

performance - 提高 Ansible 性能

转载 作者:行者123 更新时间:2023-12-04 11:29:32 32 4
gpt4 key购买 nike

我正在尝试提高 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
运行这需要惊人的 57 秒。与执行相同操作的 bash 脚本相比:
测试文件
#!/bin/bash
touch /tmp/hello
for i in {1..500}
do
sh /home/admin/hello.sh
echo "This is iteration $i"
done
你好.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
我还能做些什么来改善这种糟糕的表现?

最佳答案

使用您的代码,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"
你好.j2
{% for i in range(500) %}
Hello, World!
{% endfor %}

关于performance - 提高 Ansible 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68396384/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com