Debian Codegraph @

ansible_collections/community/okd/docs/ansible_turbo_mode.rst

raw
1 .. _ansible_turbo_mode:
2
3
4 ******************
5 Ansible Turbo mode
6 ******************
7
8 Following document provides overview of Ansible Turbo mode in ``community.okd`` collection.
9
10 .. contents::
11 :local:
12 :depth: 1
13
14
15 Synopsis
16 --------
17 - A brief introduction about Ansible Turbo mode in ``community.okd`` collection.
18 - Ansible Turbo mode is an optional performance optimization. It can be enabled by installing the cloud.common collection and setting the ``ENABLE_TURBO_MODE`` environment variable.
19
20 Requirements
21 ------------
22
23 The following requirement is needed on the host that executes this module.
24
25 - The ``cloud.common`` collection (https://github.com/ansible-collections/cloud.common)
26
27 You will also need to set the environment variable ``ENABLE_TURBO_MODE=1`` on the managed host. This can be done in the same ways you would usually do so, for example::
28
29 ---
30 - hosts: remote
31 environment:
32 ENABLE_TURBO_MODE: 1
33 tasks:
34 ...
35
36
37 Installation
38 ------------
39
40 You can install ``cloud.common`` collection using following command::
41
42 # ansible-galaxy collection install cloud.common
43
44
45 Current situation without Ansible Turbo mode
46 ============================================
47
48 The traditional execution flow of an Ansible module includes the following steps:
49
50 - Upload of a ZIP archive with the module and its dependencies
51 - Execution of the module
52 - Ansible collects the results once the script is finished
53
54 These steps happen for each task of a playbook, and on every host.
55
56 Most of the time, the execution of a module is fast enough for
57 the user. However, sometime the module requires significant amount of time,
58 just to initialize itself. This is a common situation with the API based modules.
59
60 A classic initialization involves the following steps:
61
62 - Load a Python library to access the remote resource (via SDK)
63 - Open a client
64 - Load a bunch of Python modules.
65 - Request a new TCP connection.
66 - Create a session.
67 - Authenticate the client.
68
69 All these steps are time consuming and the same operations will be running again and again.
70
71 For instance, here:
72
73 - ``import openstack``: takes 0.569s
74 - ``client = openstack.connect()``: takes 0.065s
75 - ``client.authorize()``: takes 1.360s,
76
77 These numbers are from test running against VexxHost public cloud.
78
79 In this case, it's a 2s-ish overhead per task. If the playbook
80 comes with 10 tasks, the execution time cannot go below 20s.
81
82 How Ansible Turbo Module improve the situation
83 ==============================================
84
85 ``AnsibleTurboModule`` is actually a class that inherites from
86 the standard ``AnsibleModule`` class that your modules probably
87 already use.
88 The big difference is that when a module starts, it also spawns
89 a little Python daemon. If a daemon already exists, it will just
90 reuse it.
91 All the module logic is run inside this Python daemon. This means:
92
93 - Python modules are actually loaded one time
94 - Ansible module can reuse an existing authenticated session.
95
96 The background service
97 ======================
98
99 The daemon kills itself after 15s, and communication are done
100 through an Unix socket.
101 It runs in one single process and uses ``asyncio`` internally.
102 Consequently you can use the ``async`` keyword in your Ansible module.
103 This will be handy if you interact with a lot of remote systems
104 at the same time.
105
106 Security impact
107 ===============
108
109 ``ansible_module.turbo`` open an Unix socket to interact with the background service.
110 We use this service to open the connection toward the different target systems.
111
112 This is similar to what SSH does with the sockets.
113
114 Keep in mind that:
115
116 - All the modules can access the same cache. Soon an isolation will be done at the collection level (https://github.com/ansible-collections/cloud.common/pull/17)
117 - A task can load a different version of a library and impact the next tasks.
118 - If the same user runs two ``ansible-playbook`` at the same time, they will have access to the same cache.
119
120 When a module stores a session in a cache, it's a good idea to use a hash of the authentication information to identify the session.
121
122 Error management
123 ================
124
125 ``ansible_module.turbo`` uses exceptions to communicate a result back to the module.
126
127 - ``EmbeddedModuleFailure`` is raised when ``json_fail()`` is called.
128 - ``EmbeddedModuleSuccess`` is raised in case of success and returns the result to the origin module process.
129
130 These exceptions are defined in ``ansible_collections.cloud.common.plugins.module_utils.turbo.exceptions``.
131 You can raise ``EmbeddedModuleFailure`` exception yourself, for instance from a module in ``module_utils``.
132
133 .. note:: Be careful with the ``except Exception:`` blocks.
134 Not only they are bad practice, but also may interface with this
135 mechanism.
136
137
138 Troubleshooting
139 ===============
140
141 You may want to manually start the server. This can be done with the following command:
142
143 .. code-block:: shell
144
145 PYTHONPATH=$HOME/.ansible/collections python -m ansible_collections.cloud.common.plugins.module_utils.turbo.server --socket-path $HOME/.ansible/tmp/turbo_mode.kubernetes.core.socket
146
147 You can use the ``--help`` argument to get a list of the optional parameters.