2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# 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
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: xattr
|
|
|
|
short_description: Manage user defined extended attributes
|
|
|
|
description:
|
2021-11-30 08:33:11 +01:00
|
|
|
- Manages filesystem user defined extended attributes.
|
|
|
|
- Requires that extended attributes are enabled on the target filesystem
|
|
|
|
and that the setfattr/getfattr utilities are present.
|
2023-02-20 17:28:47 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
path:
|
|
|
|
description:
|
|
|
|
- The full path of the file/object to get the facts of.
|
2023-06-18 19:48:01 +02:00
|
|
|
- Before 2.3 this option was only usable as O(name).
|
2020-03-09 10:11:07 +01:00
|
|
|
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:
|
2023-06-18 19:48:01 +02:00
|
|
|
- The value to set the named name/key to, it automatically sets the O(state) to V(present).
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- defines which state you want to do.
|
2023-06-18 19:48:01 +02:00
|
|
|
V(read) retrieves the current value for a O(key) (default)
|
|
|
|
V(present) sets O(path) to O(value), default if value is set
|
|
|
|
V(all) dumps all data
|
|
|
|
V(keys) retrieves all keys
|
|
|
|
V(absent) deletes the key
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
choices: [ absent, all, keys, present, read ]
|
|
|
|
default: read
|
|
|
|
follow:
|
|
|
|
description:
|
2023-06-18 19:48:01 +02:00
|
|
|
- If V(true), dereferences symlinks and sets/gets attributes on symlink target,
|
2020-03-09 10:11:07 +01:00
|
|
|
otherwise acts on symlink itself.
|
|
|
|
type: bool
|
2021-11-30 08:33:11 +01:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
2023-06-18 19:48:01 +02:00
|
|
|
- As of Ansible 2.3, the O(name) option has been changed to O(path) as default, but O(name) still works as well.
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
2021-11-30 08:33:11 +01:00
|
|
|
- Brian Coca (@bcoca)
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Obtain the extended attributes of /etc/foo.conf
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.xattr:
|
2020-03-09 10:11:07 +01:00
|
|
|
path: /etc/foo.conf
|
|
|
|
|
|
|
|
- name: Set the key 'user.foo' to value 'bar'
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.xattr:
|
2020-03-09 10:11:07 +01:00
|
|
|
path: /etc/foo.conf
|
|
|
|
key: foo
|
|
|
|
value: bar
|
|
|
|
|
|
|
|
- name: Set the key 'trusted.glusterfs.volume-id' to value '0x817b94343f164f199e5b573b4ea1f914'
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.xattr:
|
2020-03-09 10:11:07 +01:00
|
|
|
path: /mnt/bricks/brick1
|
|
|
|
namespace: trusted
|
|
|
|
key: glusterfs.volume-id
|
|
|
|
value: "0x817b94343f164f199e5b573b4ea1f914"
|
|
|
|
|
|
|
|
- name: Remove the key 'user.foo'
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.xattr:
|
2020-03-09 10:11:07 +01:00
|
|
|
path: /etc/foo.conf
|
|
|
|
key: foo
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: Remove the key 'trusted.glusterfs.volume-id'
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.xattr:
|
2020-03-09 10:11:07 +01:00
|
|
|
path: /mnt/bricks/brick1
|
|
|
|
namespace: trusted
|
|
|
|
key: glusterfs.volume-id
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_xattr_keys(module, path, follow):
|
2021-04-05 09:22:06 +02:00
|
|
|
cmd = [module.get_bin_path('getfattr', True), '--absolute-names']
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if not follow:
|
|
|
|
cmd.append('-h')
|
|
|
|
cmd.append(path)
|
|
|
|
|
|
|
|
return _run_xattr(module, cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def get_xattr(module, path, key, follow):
|
2021-04-05 09:22:06 +02:00
|
|
|
cmd = [module.get_bin_path('getfattr', True), '--absolute-names']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not follow:
|
|
|
|
cmd.append('-h')
|
|
|
|
if key is None:
|
|
|
|
cmd.append('-d')
|
|
|
|
else:
|
2021-11-30 08:33:11 +01:00
|
|
|
cmd.append('-n')
|
|
|
|
cmd.append(key)
|
2020-03-09 10:11:07 +01:00
|
|
|
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')
|
2021-11-30 08:33:11 +01:00
|
|
|
cmd.append('-n')
|
|
|
|
cmd.append(key)
|
|
|
|
cmd.append('-v')
|
|
|
|
cmd.append(value)
|
2020-03-09 10:11:07 +01:00
|
|
|
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')
|
2021-11-30 08:33:11 +01:00
|
|
|
cmd.append('-x')
|
|
|
|
cmd.append(key)
|
2020-03-09 10:11:07 +01:00
|
|
|
cmd.append(path)
|
|
|
|
|
|
|
|
return _run_xattr(module, cmd, False)
|
|
|
|
|
|
|
|
|
|
|
|
def _run_xattr(module, cmd, check_rc=True):
|
|
|
|
|
|
|
|
try:
|
2021-11-30 08:33:11 +01:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=check_rc)
|
2020-03-09 10:11:07 +01:00
|
|
|
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:
|
2021-11-09 06:08:15 +01:00
|
|
|
(key, val) = line.split('=', 1)
|
2020-03-09 10:11:07 +01:00
|
|
|
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'),
|
2021-03-12 08:51:47 +01:00
|
|
|
key=dict(type='str', no_log=False),
|
2020-03-09 10:11:07 +01:00
|
|
|
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()
|