Debian Codegraph @

test/integration/targets/gathering_facts/library/file_utils.py

raw
1 from __future__ import annotations
2
3 from ansible.module_utils.basic import AnsibleModule
4 from ansible.module_utils.facts.utils import (
5 get_file_content,
6 get_file_lines,
7 get_mount_size,
8 )
9
10
11 def main():
12 module = AnsibleModule(
13 argument_spec=dict(
14 test=dict(type='str', default='strip'),
15 touch_file=dict(type='str', default='/dev/null'),
16 line_sep_file=dict(type='str', default='/dev/null'),
17 line_sep_sep=dict(type='str', default='\n'),
18 )
19 )
20
21 test = module.params['test']
22 facts = {}
23
24 if test == 'strip':
25 etc_passwd = get_file_content('/etc/passwd')
26 etc_passwd_unstripped = get_file_content('/etc/passwd', strip=False)
27 facts['etc_passwd_newlines'] = etc_passwd.count('\n')
28 facts['etc_passwd_newlines_unstripped'] = etc_passwd_unstripped.count('\n')
29
30 elif test == 'default':
31 path = module.params['touch_file']
32 facts['touch_default'] = get_file_content(path, default='i am a default')
33
34 elif test == 'line_sep':
35 path = module.params['line_sep_file']
36 sep = module.params['line_sep_sep']
37 facts['line_sep'] = get_file_lines(path, line_sep=sep)
38
39 elif test == 'invalid_mountpoint':
40 facts['invalid_mountpoint'] = get_mount_size('/doesnotexist')
41
42 result = {
43 'changed': False,
44 'ansible_facts': facts,
45 }
46
47 module.exit_json(**result)
48
49
50 main()