Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ansible slow with simple tasks

Tags:

ansible

After using ansible for about a week now, I found out that ansible takes similar amount of time regardless of how complicated is the task it is supposed to do.

  • Install 20 packages using apt - 3 seconds
  • Copy a single file with 2 config settings using template - 3 seconds.

While I can easily install 20 packages using just a single command,
template needs to be run in a loop, so if I have 20 config files to copy, then it takes a whole minute.

Scale that to 10 roles, some of them repeated 5 times and you can get over an hour to do a simple deployment.

Is ansible supposed to be this slow, or is there something I can do to improve the performace?

edit:
Based on your answers I assume this is not a normal behavior. Here is some code examples of those simple tasks as requested by @U880D. As I said, nothing special just simple configs:

# tasks/main.yml

- name: Configure php-{{ php_version }}
  template:
    src: '{{ item }}.j2'
    dest: '/etc/php/{{ php_version }}/{{ item }}'
  loop:
    - cli/conf.d/50-memory.ini
    - fpm/conf.d/50-memory.ini
    - fpm/conf.d/50-opcache.ini
    - fpm/pool.d/www.conf
  notify:
    - restart php {{ php_version }}
# templates/fpm/conf.d/50-memory.ini.j2

memory_limit = {{ php_fpm_memory_limit }}
post_max_size = {{ php_fpm_post_max_size }}
upload_max_filesize = {{ php_fpm_upload_max_filesize }}
max_file_uploads = {{ php_fpm_max_file_uploads }}
# templates/fpm/conf.d/50-opcache.ini.j2

[opcache]
opcache.enable=1
opcache.memory_consumption={{ php_opcache_memory_limit }}
opcache.validate_timestamps=1
opcache.revalidate_freq=1
opcache.huge_code_pages=1

edit2:
I am not sure if this is what task_profile should do, but here is the output of that command from above on server called management-1. I added a debug task after to get exact timings. 4 templates that didn't even need an update took ~7.3s:

TASK [php : Configure php-8.1] ************************************************************************************************************************************************************************************
Tuesday 22 March 2022  10:17:33 +0100 (0:00:02.730)       0:00:06.616 ********* 
ok: [management-1] => (item=cli/conf.d/50-memory.ini)
ok: [management-1] => (item=fpm/conf.d/50-memory.ini)
ok: [management-1] => (item=fpm/conf.d/50-opcache.ini)
ok: [management-1] => (item=fpm/pool.d/www.conf)

TASK [php : Debug] ************************************************************************************************************************************************************************************************
Tuesday 22 March 2022  10:17:40 +0100 (0:00:07.308)       0:00:13.924 ********* 
like image 762
HubertNNN Avatar asked Jan 28 '26 21:01

HubertNNN


1 Answers

You are right that Ansible is slow (as a hell). It is by design. They decided to copy python code through ssh to remote hosts to perform operations:

  • code need to be base64 encoded/decoded - CPU load/time/bloated network.
  • processes extensively forked/execed.

Each item in the loop is executed as a separate task.

Just relax and drink coffee. Or update you ansible.cfg and pray:

[ssh_connection]
pipelining = True

[defaults]
nocows = True
interpreter_python = /usr/bin/python3
gathering = False
forks = 2
strategy = free
like image 68
gavenkoa Avatar answered Feb 01 '26 07:02

gavenkoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!