summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/modules/lbu.py
blob: c961b6060d83b9c219eee5243b9d74f35b92132a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2019, Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = '''
---
module: lbu

short_description: Local Backup Utility for Alpine Linux

version_added: '0.2.0'

description:
  - Manage Local Backup Utility of Alpine Linux in run-from-RAM mode

extends_documentation_fragment:
  - community.general.attributes

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none

options:
  commit:
    description:
    - Control whether to commit changed files.
    type: bool
  exclude:
    description:
    - List of paths to exclude.
    type: list
    elements: str
  include:
    description:
    - List of paths to include.
    type: list
    elements: str

author:
  - Kaarle Ritvanen (@kunkku)
'''

EXAMPLES = '''
# Commit changed files (if any)
- name: Commit
  community.general.lbu:
    commit: true

# Exclude path and commit
- name: Exclude directory
  community.general.lbu:
    commit: true
    exclude:
    - /etc/opt

# Include paths without committing
- name: Include file and directory
  community.general.lbu:
    include:
    - /root/.ssh/authorized_keys
    - /var/lib/misc
'''

RETURN = '''
msg:
  description: Error message
  type: str
  returned: on failure
'''

from ansible.module_utils.basic import AnsibleModule

import os.path


def run_module():
    module = AnsibleModule(
        argument_spec={
            'commit': {'type': 'bool'},
            'exclude': {'type': 'list', 'elements': 'str'},
            'include': {'type': 'list', 'elements': 'str'}
        },
        supports_check_mode=True
    )

    changed = False

    def run_lbu(*args):
        code, stdout, stderr = module.run_command(
            [module.get_bin_path('lbu', required=True)] + list(args)
        )
        if code:
            module.fail_json(changed=changed, msg=stderr)
        return stdout

    update = False
    commit = False

    for param in ('include', 'exclude'):
        if module.params[param]:
            paths = run_lbu(param, '-l').split('\n')
            for path in module.params[param]:
                if os.path.normpath('/' + path)[1:] not in paths:
                    update = True

    if module.params['commit']:
        commit = update or run_lbu('status') > ''

    if module.check_mode:
        module.exit_json(changed=update or commit)

    if update:
        for param in ('include', 'exclude'):
            if module.params[param]:
                run_lbu(param, *module.params[param])
                changed = True

    if commit:
        run_lbu('commit')
        changed = True

    module.exit_json(changed=changed)


def main():
    run_module()


if __name__ == '__main__':
    main()