Debian Codegraph @

test/integration/targets/connection_persistent/connection_plugins/persistent.py

raw
1 from __future__ import annotations
2
3 DOCUMENTATION = """
4 options:
5 persistent_connect_timeout:
6 type: int
7 default: 30
8 ini:
9 - section: persistent_connection
10 key: connect_timeout
11 env:
12 - name: ANSIBLE_PERSISTENT_CONNECT_TIMEOUT
13 vars:
14 - name: ansible_connect_timeout
15 persistent_command_timeout:
16 type: int
17 default: 30
18 ini:
19 - section: persistent_connection
20 key: command_timeout
21 env:
22 - name: ANSIBLE_PERSISTENT_COMMAND_TIMEOUT
23 vars:
24 - name: ansible_command_timeout
25 persistent_log_messages:
26 type: boolean
27 ini:
28 - section: persistent_connection
29 key: log_messages
30 env:
31 - name: ANSIBLE_PERSISTENT_LOG_MESSAGES
32 vars:
33 - name: ansible_persistent_log_messages
34 """
35
36 import json
37 import os
38 import pickle
39
40 from ansible.playbook.play_context import PlayContext
41 from ansible.plugins.connection import NetworkConnectionBase
42
43
44 class Connection(NetworkConnectionBase):
45 transport = 'persistent'
46 supports_persistence = True
47
48 def _connect(self):
49 self._connected = True
50
51 def update_play_context(self, pc_data):
52 """
53 This is to ensure that the PlayContext.deserialize method remains functional,
54 preventing it from breaking the network connection plugins that rely on it.
55
56 See:
57 https://github.com/ansible-collections/ansible.netcommon/blob/50fafb6875bb2f57e932a7a50123513b48bd4fd5/plugins/connection/httpapi.py#L258
58 """
59 pc = self._play_context = PlayContext()
60
61 pc.deserialize(
62 pickle.loads(
63 pc_data.encode(errors='surrogateescape')
64 )
65 )
66
67 def get_capabilities(self, *args, **kwargs):
68 return json.dumps({
69 'pid': os.getpid(),
70 'ppid': os.getppid(),
71 **self._play_context.dump_attrs()
72 })