iTranslated by AI
Disabling Ansible compatibility options for 20% faster execution
I couldn't find any articles about this in Japanese, so I'm writing this down.
What I did
-
Upgraded Ansible to version 2.9.16 or higher.
-
Disabled the option for maintaining compatibility with Ansible 2.5 and earlier in
ansible.cfg.
[defaults]
# default: True
inject_fact_as_vars = False
- Updated facts using the old notation in Ansible tasks, etc. (
ansible_*-->ansible_facts.*)
Changed facts such asansible_os_distributiontoansible_facts.os_distribution.
Details
Causes of performance degradation
As a premise, Ansible's processing slows down even for simple tasks as memory usage increases.
To give you an idea of the overhead, in the environment of this Issue, a simple sleep 1 took up to 26 seconds.
How to reduce memory usage
Disable fact gathering
As introduced in 8 ways to speed up your Ansible playbooks, it would be ideal if we could disable gather_facts. However, this was not applicable when actually using facts like ansible_* within the processing.
Therefore, I decided to change the options for maintaining compatibility around Ansible facts.
Namespace change for facts since Ansible 2.5
As mentioned in Ansible 2.5: Traveling space and time, the namespace changed from ansible_* to ansible_facts.* in Ansible 2.5.
This is a significant breaking change, so by default, the option to copy variables added to ansible_facts.* as ansible_* is enabled.
In short, the memory usage by facts is simply doubled.
The option that controls this copying of variables for compatibility purposes is inject_fact_as_vars. By disabling this option, facts will only be stored in ansible_facts.*.
ansible.cfg
[defaults]
# default: True
inject_fact_as_vars = False
To ensure all playbooks could run in this state, I searched using git grep ansible_ and fixed them one by one.
Other issues encountered
When I disabled this option while using Ansible 2.9.11, some modules (such as set_fact and include_vars) did not work correctly.
This bug seems to have been fixed in Ansible 2.9.16, so I resolved it by upgrading the version.
Discussion