Debian Codegraph @

ansible_collections/community/general/plugins/modules/lbu.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2019, Kaarle Ritvanen <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: lbu
11
12 short_description: Local Backup Utility for Alpine Linux
13
14 version_added: '0.2.0'
15
16 description:
17 - Manage Local Backup Utility of Alpine Linux in run-from-RAM mode.
18 extends_documentation_fragment:
19 - community.general._attributes
20
21 attributes:
22 check_mode:
23 support: full
24 diff_mode:
25 support: none
26
27 options:
28 commit:
29 description:
30 - Control whether to commit changed files.
31 type: bool
32 exclude:
33 description:
34 - List of paths to exclude.
35 type: list
36 elements: str
37 include:
38 description:
39 - List of paths to include.
40 type: list
41 elements: str
42
43 author:
44 - Kaarle Ritvanen (@kunkku)
45 """
46
47 EXAMPLES = r"""
48 # Commit changed files (if any)
49 - name: Commit
50 community.general.lbu:
51 commit: true
52
53 # Exclude path and commit
54 - name: Exclude directory
55 community.general.lbu:
56 commit: true
57 exclude:
58 - /etc/opt
59
60 # Include paths without committing
61 - name: Include file and directory
62 community.general.lbu:
63 include:
64 - /root/.ssh/authorized_keys
65 - /var/lib/misc
66 """
67
68 RETURN = r"""
69 msg:
70 description: Error message.
71 type: str
72 returned: on failure
73 """
74
75 import os.path
76
77 from ansible.module_utils.basic import AnsibleModule
78
79
80 def run_module():
81 module = AnsibleModule(
82 argument_spec={
83 "commit": {"type": "bool"},
84 "exclude": {"type": "list", "elements": "str"},
85 "include": {"type": "list", "elements": "str"},
86 },
87 supports_check_mode=True,
88 )
89 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
90
91 changed = False
92
93 def run_lbu(*args):
94 code, stdout, stderr = module.run_command([module.get_bin_path("lbu", required=True)] + list(args))
95 if code:
96 module.fail_json(changed=changed, msg=stderr)
97 return stdout
98
99 update = False
100 commit = False
101
102 for param in ("include", "exclude"):
103 if module.params[param]:
104 paths = run_lbu(param, "-l").split("\n")
105 for path in module.params[param]:
106 if os.path.normpath(f"/{path}")[1:] not in paths:
107 update = True
108
109 if module.params["commit"]:
110 commit = update or run_lbu("status") > ""
111
112 if module.check_mode:
113 module.exit_json(changed=update or commit)
114
115 if update:
116 for param in ("include", "exclude"):
117 if module.params[param]:
118 run_lbu(param, *module.params[param])
119 changed = True
120
121 if commit:
122 run_lbu("commit")
123 changed = True
124
125 module.exit_json(changed=changed)
126
127
128 def main():
129 run_module()
130
131
132 if __name__ == "__main__":
133 main()