Debian Codegraph @

lib/ansible/modules/slurp.py

raw
1 # -*- coding: utf-8 -*-
2
3 # (c) 2012, Michael DeHaan <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8
9 DOCUMENTATION = r"""
10 ---
11 module: slurp
12 version_added: historical
13 short_description: Slurps a file from remote nodes
14 description:
15 - This module works like M(ansible.builtin.fetch). It is used for fetching a base64-
16 encoded blob containing the data in a remote file.
17 - This module is also supported for Windows targets.
18 options:
19 src:
20 description:
21 - The file on the remote system to fetch. This I(must) be a file, not a directory.
22 type: path
23 required: true
24 aliases: [ path ]
25 armor:
26 description:
27 - To safely deliver the data requested, armor it by base64 encoding it.
28 - This is mainly done to avoid issues with binary data or to preserve non-UTF-8 encodings,
29 as the content will be converted to UTF-8 due to the JSON transport used.
30 - For windows support, check the ansible.windows collection.
31 type: bool
32 default: true
33 version_added: '2.21'
34 extends_documentation_fragment:
35 - action_common_attributes
36 attributes:
37 check_mode:
38 support: full
39 diff_mode:
40 support: none
41 platform:
42 platforms: posix, windows
43 notes:
44 - This module returns an 'in memory' base64 encoded version of the file, take
45 into account that this will require at least twice the RAM as the original file size.
46 seealso:
47 - module: ansible.builtin.fetch
48 author:
49 - Ansible Core Team
50 - Michael DeHaan (@mpdehaan)
51 """
52
53 EXAMPLES = r"""
54 - name: Find out what the remote machine's mounts are
55 ansible.builtin.slurp:
56 src: /proc/mounts
57 register: mounts
58
59 - name: Print returned information
60 ansible.builtin.debug:
61 msg: "{{ mounts['content'] | b64decode }}"
62
63 - name: Find out what the remote machine's mounts are, no armor
64 ansible.builtin.slurp:
65 src: /proc/mounts
66 armor: false
67 register: mounts
68
69 - name: Print returned information
70 ansible.builtin.debug:
71 msg: "{{ mounts['content'] }}"
72
73 # From the commandline, find the pid of the remote machine's sshd
74 # $ ansible host -m ansible.builtin.slurp -a 'src=/var/run/sshd.pid'
75 # host | SUCCESS => {
76 # "changed": false,
77 # "content": "MjE3OQo=",
78 # "encoding": "base64",
79 # "source": "/var/run/sshd.pid"
80 # }
81 # $ echo MjE3OQo= | base64 -d
82 # 2179
83 """
84
85 RETURN = r"""
86 content:
87 description: Encoded file content
88 returned: success
89 type: str
90 sample: "MjE3OQo="
91 encoding:
92 description: Current encoding of the file content, it can be C(base64)
93 or C(UTF-8) depending on the value of the O(armor) option.
94 returned: success
95 type: str
96 sample: "base64"
97 source:
98 description: Actual path of file slurped
99 returned: success
100 type: str
101 sample: "/var/run/sshd.pid"
102 """
103
104 import base64
105 import errno
106
107 from ansible.module_utils.basic import AnsibleModule
108
109
110 def main():
111 module = AnsibleModule(
112 argument_spec=dict(
113 src=dict(type='path', required=True, aliases=['path']),
114 armor=dict(type='bool', default=True),
115 ),
116 supports_check_mode=True,
117 )
118 source = module.params['src']
119 armor = module.params['armor']
120
121 try:
122 with open(source, 'rb') as source_fh:
123 if armor:
124 encoding = 'base64'
125 data = base64.b64encode(source_fh.read())
126 else:
127 # not current file encoding, but will be once passed as JSON
128 encoding = 'utf-8'
129 data = source_fh.read()
130 except OSError as ex:
131 if ex.errno == errno.ENOENT:
132 msg = f"File not found: {source}"
133 elif ex.errno == errno.EACCES:
134 msg = f"File is not readable: {source}"
135 elif ex.errno == errno.EISDIR:
136 msg = f"Source is a directory and must be a file: {source}"
137 else:
138 msg = "Unable to slurp file: {source}"
139
140 module.fail_json(msg, exception=ex)
141
142 module.exit_json(content=data, source=source, encoding=encoding)
143
144
145 if __name__ == '__main__':
146 main()