summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/modules/xattr.py
blob: 0b44fdaadfc598441c4e851378358c0830b49ae8 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2017, Ansible Project
# 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: xattr
short_description: Manage user defined extended attributes
description:
  - Manages filesystem user defined extended attributes.
  - Requires that extended attributes are enabled on the target filesystem
    and that the setfattr/getfattr utilities are present.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  path:
    description:
      - The full path of the file/object to get the facts of.
      - Before 2.3 this option was only usable as I(name).
    type: path
    required: true
    aliases: [ name ]
  namespace:
    description:
      - Namespace of the named name/key.
    type: str
    default: user
  key:
    description:
      - The name of a specific Extended attribute key to set/retrieve.
    type: str
  value:
    description:
      - The value to set the named name/key to, it automatically sets the I(state) to C(present).
    type: str
  state:
    description:
      - defines which state you want to do.
        C(read) retrieves the current value for a I(key) (default)
        C(present) sets I(path) to C(value), default if value is set
        C(all) dumps all data
        C(keys) retrieves all keys
        C(absent) deletes the key
    type: str
    choices: [ absent, all, keys, present, read ]
    default: read
  follow:
    description:
      - If C(true), dereferences symlinks and sets/gets attributes on symlink target,
        otherwise acts on symlink itself.
    type: bool
    default: true
notes:
  - As of Ansible 2.3, the I(name) option has been changed to I(path) as default, but I(name) still works as well.
author:
  - Brian Coca (@bcoca)
'''

EXAMPLES = '''
- name: Obtain the extended attributes  of /etc/foo.conf
  community.general.xattr:
    path: /etc/foo.conf

- name: Set the key 'user.foo' to value 'bar'
  community.general.xattr:
    path: /etc/foo.conf
    key: foo
    value: bar

- name: Set the key 'trusted.glusterfs.volume-id' to value '0x817b94343f164f199e5b573b4ea1f914'
  community.general.xattr:
    path: /mnt/bricks/brick1
    namespace: trusted
    key: glusterfs.volume-id
    value: "0x817b94343f164f199e5b573b4ea1f914"

- name: Remove the key 'user.foo'
  community.general.xattr:
    path: /etc/foo.conf
    key: foo
    state: absent

- name: Remove the key 'trusted.glusterfs.volume-id'
  community.general.xattr:
    path: /mnt/bricks/brick1
    namespace: trusted
    key: glusterfs.volume-id
    state: absent
'''

import os

# import module snippets
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native


def get_xattr_keys(module, path, follow):
    cmd = [module.get_bin_path('getfattr', True), '--absolute-names']

    if not follow:
        cmd.append('-h')
    cmd.append(path)

    return _run_xattr(module, cmd)


def get_xattr(module, path, key, follow):
    cmd = [module.get_bin_path('getfattr', True), '--absolute-names']

    if not follow:
        cmd.append('-h')
    if key is None:
        cmd.append('-d')
    else:
        cmd.append('-n')
        cmd.append(key)
    cmd.append(path)

    return _run_xattr(module, cmd, False)


def set_xattr(module, path, key, value, follow):

    cmd = [module.get_bin_path('setfattr', True)]
    if not follow:
        cmd.append('-h')
    cmd.append('-n')
    cmd.append(key)
    cmd.append('-v')
    cmd.append(value)
    cmd.append(path)

    return _run_xattr(module, cmd)


def rm_xattr(module, path, key, follow):

    cmd = [module.get_bin_path('setfattr', True)]
    if not follow:
        cmd.append('-h')
    cmd.append('-x')
    cmd.append(key)
    cmd.append(path)

    return _run_xattr(module, cmd, False)


def _run_xattr(module, cmd, check_rc=True):

    try:
        (rc, out, err) = module.run_command(cmd, check_rc=check_rc)
    except Exception as e:
        module.fail_json(msg="%s!" % to_native(e))

    # result = {'raw': out}
    result = {}
    for line in out.splitlines():
        if line.startswith('#') or line == '':
            pass
        elif '=' in line:
            (key, val) = line.split('=', 1)
            result[key] = val.strip('"')
        else:
            result[line] = ''
    return result


def main():
    module = AnsibleModule(
        argument_spec=dict(
            path=dict(type='path', required=True, aliases=['name']),
            namespace=dict(type='str', default='user'),
            key=dict(type='str', no_log=False),
            value=dict(type='str'),
            state=dict(type='str', default='read', choices=['absent', 'all', 'keys', 'present', 'read']),
            follow=dict(type='bool', default=True),
        ),
        supports_check_mode=True,
    )
    path = module.params.get('path')
    namespace = module.params.get('namespace')
    key = module.params.get('key')
    value = module.params.get('value')
    state = module.params.get('state')
    follow = module.params.get('follow')

    if not os.path.exists(path):
        module.fail_json(msg="path not found or not accessible!")

    changed = False
    msg = ""
    res = {}

    if key is None and state in ['absent', 'present']:
        module.fail_json(msg="%s needs a key parameter" % state)

    # Prepend the key with the namespace if defined
    if (
            key is not None and
            namespace is not None and
            len(namespace) > 0 and
            not (namespace == 'user' and key.startswith('user.'))):
        key = '%s.%s' % (namespace, key)

    if (state == 'present' or value is not None):
        current = get_xattr(module, path, key, follow)
        if current is None or key not in current or value != current[key]:
            if not module.check_mode:
                res = set_xattr(module, path, key, value, follow)
            changed = True
        res = current
        msg = "%s set to %s" % (key, value)
    elif state == 'absent':
        current = get_xattr(module, path, key, follow)
        if current is not None and key in current:
            if not module.check_mode:
                res = rm_xattr(module, path, key, follow)
            changed = True
        res = current
        msg = "%s removed" % (key)
    elif state == 'keys':
        res = get_xattr_keys(module, path, follow)
        msg = "returning all keys"
    elif state == 'all':
        res = get_xattr(module, path, None, follow)
        msg = "dumping all"
    else:
        res = get_xattr(module, path, key, follow)
        msg = "returning %s" % key

    module.exit_json(changed=changed, msg=msg, xattr=res)


if __name__ == '__main__':
    main()