2013-07-21 17:18:31 +02:00
|
|
|
#!/usr/bin/python
|
2015-08-15 04:01:05 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-21 17:18:31 +02:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: acl
|
2013-10-14 17:36:48 +02:00
|
|
|
version_added: "1.4"
|
|
|
|
short_description: Sets and retrieves file ACL information.
|
2013-07-21 17:18:31 +02:00
|
|
|
description:
|
2015-08-19 02:24:08 +02:00
|
|
|
- Sets and retrieves file ACL information.
|
2015-08-14 22:46:59 +02:00
|
|
|
notes:
|
|
|
|
- As of Ansible 2.0, this module only supports Linux distributions.
|
2013-07-21 17:18:31 +02:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
required: true
|
2013-12-09 17:14:09 +01:00
|
|
|
default: null
|
2013-07-21 17:18:31 +02:00
|
|
|
description:
|
2013-10-14 17:36:48 +02:00
|
|
|
- The full path of the file or object.
|
2013-07-21 17:18:31 +02:00
|
|
|
aliases: ['path']
|
2013-10-14 17:36:48 +02:00
|
|
|
|
2013-07-21 17:18:31 +02:00
|
|
|
state:
|
|
|
|
required: false
|
2013-09-11 05:13:36 +02:00
|
|
|
default: query
|
|
|
|
choices: [ 'query', 'present', 'absent' ]
|
2013-07-21 17:18:31 +02:00
|
|
|
description:
|
2014-07-25 11:04:45 +02:00
|
|
|
- defines whether the ACL should be present or not. The C(query) state gets the current acl without changing it, for use in 'register' operations.
|
2013-12-09 17:14:09 +01:00
|
|
|
|
2013-07-21 17:18:31 +02:00
|
|
|
follow:
|
|
|
|
required: false
|
|
|
|
default: yes
|
|
|
|
choices: [ 'yes', 'no' ]
|
|
|
|
description:
|
2013-10-14 17:36:48 +02:00
|
|
|
- whether to follow symlinks on the path if a symlink is encountered.
|
2013-12-09 17:14:09 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
version_added: "1.5"
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ 'yes', 'no' ]
|
|
|
|
description:
|
|
|
|
- if the target is a directory, setting this to yes will make it the default acl for entities created inside the directory. It causes an error if name is a file.
|
|
|
|
|
|
|
|
entity:
|
|
|
|
version_added: "1.5"
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- actual user or group that the ACL applies to when matching entity types user or group are selected.
|
|
|
|
|
2014-02-06 00:36:29 +01:00
|
|
|
etype:
|
2013-12-09 17:14:09 +01:00
|
|
|
version_added: "1.5"
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: [ 'user', 'group', 'mask', 'other' ]
|
|
|
|
description:
|
2014-05-27 19:32:15 +02:00
|
|
|
- the entity type of the ACL to apply, see setfacl documentation for more info.
|
2014-02-06 00:36:29 +01:00
|
|
|
|
2013-12-09 17:14:09 +01:00
|
|
|
|
|
|
|
permissions:
|
|
|
|
version_added: "1.5"
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
|
|
|
- Permissions to apply/remove can be any combination of r, w and x (read, write and execute respectively)
|
|
|
|
|
2014-02-06 00:36:29 +01:00
|
|
|
entry:
|
2013-12-09 17:14:09 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
2014-04-29 16:41:05 +02:00
|
|
|
- DEPRECATED. The acl to set or remove. This must always be quoted in the form of '<etype>:<qualifier>:<perms>'. The qualifier may be empty for some types, but the type and perms are always requried. '-' can be used as placeholder when you do not care about permissions. This is now superseded by entity, type and permissions fields.
|
2013-12-09 17:14:09 +01:00
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
recursive:
|
2015-08-14 22:46:59 +02:00
|
|
|
version_added: "2.0"
|
2014-10-07 05:32:53 +02:00
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ 'yes', 'no' ]
|
|
|
|
description:
|
2015-08-15 01:40:47 +02:00
|
|
|
- Recursively sets the specified ACL (added in Ansible 2.0). Incompatible with C(state=query).
|
2015-08-15 04:01:05 +02:00
|
|
|
author:
|
|
|
|
- "Brian Coca (@bcoca)"
|
|
|
|
- "Jérémie Astori (@astorije)"
|
2013-09-11 05:13:36 +02:00
|
|
|
notes:
|
2013-10-14 17:36:48 +02:00
|
|
|
- The "acl" module requires that acls are enabled on the target filesystem and that the setfacl and getfacl binaries are installed.
|
2013-07-21 17:18:31 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-10-14 17:36:48 +02:00
|
|
|
# Grant user Joe read access to a file
|
2014-02-06 00:36:29 +01:00
|
|
|
- acl: name=/etc/foo.conf entity=joe etype=user permissions="r" state=present
|
2013-07-21 17:18:31 +02:00
|
|
|
|
2013-10-14 17:36:48 +02:00
|
|
|
# Removes the acl for Joe on a specific file
|
2014-02-06 00:36:29 +01:00
|
|
|
- acl: name=/etc/foo.conf entity=joe etype=user state=absent
|
2013-12-09 17:14:09 +01:00
|
|
|
|
|
|
|
# Sets default acl for joe on foo.d
|
2014-02-06 00:36:29 +01:00
|
|
|
- acl: name=/etc/foo.d entity=joe etype=user permissions=rw default=yes state=present
|
2013-12-09 17:14:09 +01:00
|
|
|
|
|
|
|
# Same as previous but using entry shorthand
|
2014-03-16 17:24:04 +01:00
|
|
|
- acl: name=/etc/foo.d entry="default:user:joe:rw-" state=present
|
2013-10-14 17:36:48 +02:00
|
|
|
|
|
|
|
# Obtain the acl for a specific file
|
|
|
|
- acl: name=/etc/foo.conf
|
|
|
|
register: acl_info
|
2013-07-21 17:18:31 +02:00
|
|
|
'''
|
|
|
|
|
2015-02-13 04:11:32 +01:00
|
|
|
RETURN = '''
|
|
|
|
acl:
|
|
|
|
description: Current acl on provided path (after changes, if any)
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: list
|
|
|
|
sample: [ "user::rwx", "group::rwx", "other::rwx" ]
|
|
|
|
'''
|
|
|
|
|
2015-08-19 02:24:08 +02:00
|
|
|
|
2013-12-09 17:14:09 +01:00
|
|
|
def split_entry(entry):
|
|
|
|
''' splits entry and ensures normalized return'''
|
|
|
|
|
|
|
|
a = entry.split(':')
|
2015-08-21 00:40:15 +02:00
|
|
|
if len(a) == 2:
|
|
|
|
a.append(None)
|
|
|
|
|
|
|
|
t, e, p = a
|
2013-12-09 17:14:09 +01:00
|
|
|
|
|
|
|
if t.startswith("u"):
|
|
|
|
t = "user"
|
|
|
|
elif t.startswith("g"):
|
|
|
|
t = "group"
|
|
|
|
elif t.startswith("m"):
|
|
|
|
t = "mask"
|
|
|
|
elif t.startswith("o"):
|
|
|
|
t = "other"
|
|
|
|
else:
|
|
|
|
t = None
|
|
|
|
|
2015-08-21 00:22:28 +02:00
|
|
|
return [t, e, p]
|
2013-12-09 17:14:09 +01:00
|
|
|
|
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
def build_entry(etype, entity, permissions=None):
|
|
|
|
'''Builds and returns an entry string. Does not include the permissions bit if they are not provided.'''
|
2015-08-14 22:46:59 +02:00
|
|
|
if permissions:
|
|
|
|
return etype + ':' + entity + ':' + permissions
|
|
|
|
else:
|
|
|
|
return etype + ':' + entity
|
2013-09-11 05:13:36 +02:00
|
|
|
|
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
def build_command(module, mode, path, follow, default, recursive, entry=''):
|
2015-08-19 02:24:08 +02:00
|
|
|
'''Builds and returns a getfacl/setfacl command.'''
|
2014-10-07 05:32:53 +02:00
|
|
|
if mode == 'set':
|
|
|
|
cmd = [module.get_bin_path('setfacl', True)]
|
|
|
|
cmd.append('-m "%s"' % entry)
|
|
|
|
elif mode == 'rm':
|
|
|
|
cmd = [module.get_bin_path('setfacl', True)]
|
|
|
|
cmd.append('-x "%s"' % entry)
|
|
|
|
else: # mode == 'get'
|
|
|
|
cmd = [module.get_bin_path('getfacl', True)]
|
2015-08-14 22:46:59 +02:00
|
|
|
# prevents absolute path warnings and removes headers
|
2014-10-07 05:32:53 +02:00
|
|
|
cmd.append('--omit-header')
|
|
|
|
cmd.append('--absolute-names')
|
2013-09-11 05:13:36 +02:00
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
if recursive:
|
|
|
|
cmd.append('--recursive')
|
2013-09-11 05:13:36 +02:00
|
|
|
|
|
|
|
if not follow:
|
2015-08-21 01:35:53 +02:00
|
|
|
cmd.append('--physical')
|
2014-10-07 05:32:53 +02:00
|
|
|
|
2013-12-09 17:14:09 +01:00
|
|
|
if default:
|
2014-10-07 05:32:53 +02:00
|
|
|
if(mode == 'rm'):
|
|
|
|
cmd.append('-k')
|
|
|
|
else: # mode == 'set' or mode == 'get'
|
|
|
|
cmd.append('-d')
|
2013-09-11 05:13:36 +02:00
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
cmd.append(path)
|
|
|
|
return cmd
|
2013-09-11 05:13:36 +02:00
|
|
|
|
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
def acl_changed(module, cmd):
|
|
|
|
'''Returns true if the provided command affects the existing ACLs, false otherwise.'''
|
|
|
|
cmd = cmd[:] # lists are mutables so cmd would be overriden without this
|
|
|
|
cmd.insert(1, '--test')
|
|
|
|
lines = run_acl(module, cmd)
|
2015-08-14 22:46:59 +02:00
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if not line.endswith('*,*'):
|
2015-08-19 02:25:18 +02:00
|
|
|
return True
|
|
|
|
return False
|
2013-09-11 05:13:36 +02:00
|
|
|
|
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
def run_acl(module, cmd, check_rc=True):
|
2013-09-11 05:13:36 +02:00
|
|
|
|
2013-08-23 05:35:24 +02:00
|
|
|
try:
|
2013-09-11 05:13:36 +02:00
|
|
|
(rc, out, err) = module.run_command(' '.join(cmd), check_rc=check_rc)
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg=e.strerror)
|
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
lines = out.splitlines()
|
|
|
|
if lines and not lines[-1].split():
|
|
|
|
# trim last line only when it is empty
|
|
|
|
return lines[:-1]
|
|
|
|
else:
|
|
|
|
return lines
|
|
|
|
|
2013-08-23 05:35:24 +02:00
|
|
|
|
2013-07-21 17:18:31 +02:00
|
|
|
def main():
|
2015-08-14 22:46:59 +02:00
|
|
|
if get_platform().lower() != 'linux':
|
|
|
|
module.fail_json(msg="The acl module is only available for Linux distributions.")
|
|
|
|
|
2013-07-21 17:18:31 +02:00
|
|
|
module = AnsibleModule(
|
2014-10-07 05:32:53 +02:00
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(required=True, aliases=['path'], type='str'),
|
|
|
|
entry=dict(required=False, type='str'),
|
|
|
|
entity=dict(required=False, type='str', default=''),
|
|
|
|
etype=dict(
|
|
|
|
required=False,
|
|
|
|
choices=['other', 'user', 'group', 'mask'],
|
|
|
|
type='str'
|
|
|
|
),
|
|
|
|
permissions=dict(required=False, type='str'),
|
|
|
|
state=dict(
|
|
|
|
required=False,
|
|
|
|
default='query',
|
|
|
|
choices=['query', 'present', 'absent'],
|
|
|
|
type='str'
|
|
|
|
),
|
|
|
|
follow=dict(required=False, type='bool', default=True),
|
|
|
|
default=dict(required=False, type='bool', default=False),
|
|
|
|
recursive=dict(required=False, type='bool', default=False),
|
2013-07-21 17:18:31 +02:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2013-07-26 03:51:29 +02:00
|
|
|
|
2014-08-25 01:16:15 +02:00
|
|
|
path = os.path.expanduser(module.params.get('name'))
|
2013-07-21 17:18:31 +02:00
|
|
|
entry = module.params.get('entry')
|
2013-12-09 17:14:09 +01:00
|
|
|
entity = module.params.get('entity')
|
2014-02-06 00:36:29 +01:00
|
|
|
etype = module.params.get('etype')
|
2014-08-09 19:02:24 +02:00
|
|
|
permissions = module.params.get('permissions')
|
2013-07-21 17:18:31 +02:00
|
|
|
state = module.params.get('state')
|
|
|
|
follow = module.params.get('follow')
|
2013-12-09 17:14:09 +01:00
|
|
|
default = module.params.get('default')
|
2014-10-07 05:32:53 +02:00
|
|
|
recursive = module.params.get('recursive')
|
2014-08-09 19:02:24 +02:00
|
|
|
|
2013-07-21 17:18:31 +02:00
|
|
|
if not os.path.exists(path):
|
2014-10-07 05:32:53 +02:00
|
|
|
module.fail_json(msg="Path not found or not accessible.")
|
|
|
|
|
|
|
|
if state == 'query' and recursive:
|
|
|
|
module.fail_json(msg="'recursive' MUST NOT be set when 'state=query'.")
|
|
|
|
|
|
|
|
if not entry:
|
|
|
|
if state == 'absent' and permissions:
|
|
|
|
module.fail_json(msg="'permissions' MUST NOT be set when 'state=absent'.")
|
2013-07-21 17:18:31 +02:00
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
if state == 'absent' and not entity:
|
|
|
|
module.fail_json(msg="'entity' MUST be set when 'state=absent'.")
|
|
|
|
|
|
|
|
if state in ['present', 'absent'] and not etype:
|
|
|
|
module.fail_json(msg="'etype' MUST be set when 'state=%s'." % state)
|
2013-12-09 17:14:09 +01:00
|
|
|
|
|
|
|
if entry:
|
2014-03-16 18:49:36 +01:00
|
|
|
if etype or entity or permissions:
|
2014-10-07 05:32:53 +02:00
|
|
|
module.fail_json(msg="'entry' MUST NOT be set when 'entity', 'etype' or 'permissions' are set.")
|
|
|
|
|
2015-08-19 02:26:04 +02:00
|
|
|
if state == 'present' and entry.count(":") != 2:
|
2014-10-07 05:32:53 +02:00
|
|
|
module.fail_json(msg="'entry' MUST have 3 sections divided by ':' when 'state=present'.")
|
|
|
|
|
2015-08-19 02:26:04 +02:00
|
|
|
if state == 'absent' and entry.count(":") != 1:
|
2014-10-07 05:32:53 +02:00
|
|
|
module.fail_json(msg="'entry' MUST have 2 sections divided by ':' when 'state=absent'.")
|
2013-12-09 17:14:09 +01:00
|
|
|
|
2015-08-21 00:43:15 +02:00
|
|
|
if state == 'query':
|
|
|
|
module.fail_json(msg="'entry' MUST NOT be set when 'state=query'.")
|
|
|
|
|
2015-08-21 00:22:28 +02:00
|
|
|
etype, entity, permissions = split_entry(entry)
|
2013-07-21 17:18:31 +02:00
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
changed = False
|
2013-07-21 17:18:31 +02:00
|
|
|
msg = ""
|
2014-10-07 05:32:53 +02:00
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
entry = build_entry(etype, entity, permissions)
|
|
|
|
command = build_command(
|
|
|
|
module, 'set', path, follow,
|
|
|
|
default, recursive, entry
|
|
|
|
)
|
|
|
|
changed = acl_changed(module, command)
|
2013-12-09 17:14:09 +01:00
|
|
|
|
|
|
|
if changed and not module.check_mode:
|
2014-10-07 05:32:53 +02:00
|
|
|
run_acl(module, command)
|
|
|
|
msg = "%s is present" % entry
|
2013-12-09 17:14:09 +01:00
|
|
|
|
2013-07-21 17:18:31 +02:00
|
|
|
elif state == 'absent':
|
2014-10-07 05:32:53 +02:00
|
|
|
entry = build_entry(etype, entity)
|
|
|
|
command = build_command(
|
|
|
|
module, 'rm', path, follow,
|
|
|
|
default, recursive, entry
|
|
|
|
)
|
|
|
|
changed = acl_changed(module, command)
|
|
|
|
|
2013-12-09 17:14:09 +01:00
|
|
|
if changed and not module.check_mode:
|
2014-10-07 05:32:53 +02:00
|
|
|
run_acl(module, command, False)
|
|
|
|
msg = "%s is absent" % entry
|
|
|
|
|
2015-08-14 22:46:59 +02:00
|
|
|
elif state == 'query':
|
2014-10-07 05:32:53 +02:00
|
|
|
msg = "current acl"
|
2013-07-21 17:18:31 +02:00
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
acl = run_acl(
|
|
|
|
module,
|
|
|
|
build_command(module, 'get', path, follow, default, recursive)
|
|
|
|
)
|
2013-07-21 17:18:31 +02:00
|
|
|
|
2014-10-07 05:32:53 +02:00
|
|
|
module.exit_json(changed=changed, msg=msg, acl=acl)
|
2013-07-21 17:18:31 +02:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-07-21 17:18:31 +02:00
|
|
|
|
|
|
|
main()
|