1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Adding namespace option into the xattr module (#42755)

* Adding namespace option into the xattr module

* Include namespace into the tests

* Make it backward compatible, remove re and operator module
This commit is contained in:
Jiri Tyr 2018-07-16 18:26:06 +01:00 committed by Brian Coca
parent 7f0fe1898e
commit 92c58e9a66
2 changed files with 42 additions and 17 deletions

View file

@ -25,6 +25,11 @@ options:
- Before 2.3 this option was only usable as I(name). - Before 2.3 this option was only usable as I(name).
aliases: [ name ] aliases: [ name ]
required: true required: true
namespace:
description:
- Namespace of the named name/key.
default: user
version_added: "2.7"
key: key:
description: description:
- The name of a specific Extended attribute key to set/retrieve. - The name of a specific Extended attribute key to set/retrieve.
@ -58,22 +63,34 @@ EXAMPLES = '''
xattr: xattr:
path: /etc/foo.conf path: /etc/foo.conf
- name: Sets the key 'foo' to value 'bar' - name: Set the key 'user.foo' to value 'bar'
xattr: xattr:
path: /etc/foo.conf path: /etc/foo.conf
key: user.foo key: foo
value: bar value: bar
- name: Removes the key 'foo' - name: Set the key 'trusted.glusterfs.volume-id' to value '0x817b94343f164f199e5b573b4ea1f914'
xattr:
path: /mnt/bricks/brick1
namespace: trusted
key: glusterfs.volume-id
value: "0x817b94343f164f199e5b573b4ea1f914"
- name: Remove the key 'user.foo'
xattr: xattr:
path: /etc/foo.conf path: /etc/foo.conf
key: user.foo key: foo
state: absent
- name: Remove the key 'trusted.glusterfs.volume-id'
xattr:
path: /mnt/bricks/brick1
namespace: trusted
key: glusterfs.volume-id
state: absent state: absent
''' '''
import operator
import os import os
import re
# import module snippets # import module snippets
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -140,10 +157,10 @@ def _run_xattr(module, cmd, check_rc=True):
# result = {'raw': out} # result = {'raw': out}
result = {} result = {}
for line in out.splitlines(): for line in out.splitlines():
if re.match("^#", line) or line == "": if line.startswith('#') or line == '':
pass pass
elif re.search('=', line): elif '=' in line:
(key, val) = line.split("=") (key, val) = line.split('=')
result[key] = val.strip('"') result[key] = val.strip('"')
else: else:
result[line] = '' result[line] = ''
@ -154,6 +171,7 @@ def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
path=dict(type='path', required=True, aliases=['name']), path=dict(type='path', required=True, aliases=['name']),
namespace=dict(type='str', default='user'),
key=dict(type='str'), key=dict(type='str'),
value=dict(type='str'), value=dict(type='str'),
state=dict(type='str', default='read', choices=['absent', 'all', 'keys', 'present', 'read']), state=dict(type='str', default='read', choices=['absent', 'all', 'keys', 'present', 'read']),
@ -162,6 +180,7 @@ def main():
supports_check_mode=True, supports_check_mode=True,
) )
path = module.params.get('path') path = module.params.get('path')
namespace = module.params.get('namespace')
key = module.params.get('key') key = module.params.get('key')
value = module.params.get('value') value = module.params.get('value')
state = module.params.get('state') state = module.params.get('state')
@ -177,9 +196,13 @@ def main():
if key is None and state in ['absent', 'present']: if key is None and state in ['absent', 'present']:
module.fail_json(msg="%s needs a key parameter" % state) module.fail_json(msg="%s needs a key parameter" % state)
# All xattr must begin in user namespace # Prepend the key with the namespace if defined
if key is not None and not re.match(r'^user\.', key): if (
key = 'user.%s' % key 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): if (state == 'present' or value is not None):
current = get_xattr(module, path, key, follow) current = get_xattr(module, path, key, follow)

View file

@ -13,7 +13,7 @@
- name: Get specific attribute - name: Get specific attribute
xattr: xattr:
path: "{{ test_file }}" path: "{{ test_file }}"
key: user.foo key: foo
register: xattr_get_specific_result register: xattr_get_specific_result
- assert: - assert:
@ -27,7 +27,8 @@
- name: Set attribute again - name: Set attribute again
xattr: xattr:
path: "{{ test_file }}" path: "{{ test_file }}"
key: user.foo namespace: user
key: foo
value: bar value: bar
register: xattr_set_again_result register: xattr_set_again_result
@ -38,11 +39,11 @@
- name: Unset attribute - name: Unset attribute
xattr: xattr:
path: "{{ test_file }}" path: "{{ test_file }}"
key: user.foo key: foo
state: absent state: absent
register: xattr_unset_result register: xattr_unset_result
- name: get attributes - name: Get attributes
xattr: xattr:
path: "{{ test_file }}" path: "{{ test_file }}"
register: xattr_get_after_unset_result register: xattr_get_after_unset_result
@ -56,7 +57,8 @@
- name: Unset attribute again - name: Unset attribute again
xattr: xattr:
path: "{{ test_file }}" path: "{{ test_file }}"
key: user.foo namespace: user
key: foo
state: absent state: absent
register: xattr_unset_result register: xattr_unset_result