Ansible is an automated configuration management tool based on SSH (and of course Telnet, WinRM, etc.), and its easy to use, agent-free way of working has many advantages in many scenarios. As a result, it is not as efficient as other C/S tools, and has been criticized by other C/S tool users. For this reason, Ansible has made a number of improvements to its speed and efficiency.

Parameter name/Optimization category instructions
fact cache Cache facts information after the first collectionmemoryorredisOr in a file.
gather_subset Optional collectionnetwork.hardwareAnd so on, but not all of them
control_path openssh socketPersistent, reuse SSH connections
pipelinling openssh pipeliningInstead of creating temporary files on the client side, the client reads the rendered script from the pipe
fork Increase the number of parallel execution hosts
serial willPlay_hosts ` ` (1)The host is executed in batches
strategy The defaultlinear, a single task on each host will wait for other tasks to complete before executing the next taskfreeInstead of waiting for another host, proceed (which will look messy) with an optionhost_pinnedI don’t know what it is

Overview

Mitogen’s Ansible Plugin (Strategy Plugin) has been iterated to version 0.29. It can improve execution efficiency by 1.2x ~ 7x.

It replaces ansible’s default embedded and pure Python shell calls with efficient remote procedure calls. It does not optimize the execution efficiency of the module itself, but rather makes it as fast as possible to fetch and return the module. Transfer rendering scripts and other operations) to improve the overall efficiency, features as follows

Expect a 1.25x-7x speedup and a CPU usage reduction at least 2X, depending on network conditions, modules executed, and time already spent by targets on useful work. Mitogen cannot improve a module once it is executing, it can only ensure the module executes as quickly as possible.

  • One connection is used per target, in addition to one sudo invocation per user account. This is much better than SSH multiplexing combined with pipelining, as significant state can be maintained in RAM between steps, And system logs Aren’t spammed with repeat authentication events.

  • A single network roundtrip is used to execute a step whose code already exists in RAM on the target. Eliminating multiplexed SSH channel creation saves 4 ms runtime per 1 ms of network latency for every playbook step.

  • Processes are aggressively reused, avoiding the cost of invoking Python and recompiling imports, saving 300-800 ms for every playbook step.

  • Code is ephemerally cached in RAM, reducing bandwidth usage by an order of magnitude compared to SSH pipelining, with around 5x fewer frames traversing the network in a typical run.

  • Fewer writes to the target filesystem occur. In typical configurations, Ansible repeatedly rewrites and extracts ZIP files to multiple temporary directories on the target. Security issues relating to temporary files in cross-account scenarios are entirely avoided.

The effect is most potent on playbooks that execute many short-lived actions, Where Ansible’s overhead standardization the cost of the operation, for example when executing large with_items loops to run simple commands or write files.

By default, a connection is re-opened every time a task or loop is executed. The rendering execution code is temporarily stored in memory; Reduce the time consumption of multiplexing SSH tunnels. Reduce bandwidth for temporary file transfers; Code reuse, avoiding code recompilation costs, etc

The implementation principle, you can go to the official website to explain

(1).play_hostsIs a built-in parameter that refers to the list of hosts in the playBook currently being executed

(2).As soon as possibleRefers to the phase before running the module

Installation

  1. Download and extract mitogen – 0.2.9. Tar. Gz.

  2. Modify ansible.cfg

[defaults] strategy_plugins = /path/to/mitogen-0.2.9/ansible_mitogen/ strategy strategy = mitogen_linearCopy the code

The strategy key is optional. If omitted, the ANSIBLE_STRATEGY=mitogen_linear environment variable can be set on a per-run basis. Like mitogen_linear, the mitogen_free and mitogen_host_pinned strategies exists to mimic the free and host_pinned strategies.

Reference

networkgenomics.com/ansible/

Mitogen.networkgenomics.com/ansible_det…