2013-07-21 17:18:31 +02:00
#!/usr/bin/python
# 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:
2013-10-14 17:36:48 +02:00
- Sets and retrieves file ACL information.
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-02-06 00:36:29 +01: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 superceeded by entity, type and permissions fields.
2013-12-09 17:14:09 +01:00
2013-09-11 05:13:36 +02:00
author: Brian Coca
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
'''
2013-12-09 17:14:09 +01:00
def split_entry(entry):
''' splits entry and ensures normalized return'''
a = entry.split(':')
a.reverse()
if len(a) == 3:
a.append(False)
try:
p,e,t,d = a
except ValueError, e:
print "wtf?? %s => %s" % (entry,a)
raise e
2014-03-15 06:10:15 +01:00
if d:
d = True
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
perms = ['-','-','-']
for char in p:
if char == 'r':
perms[0] = 'r'
if char == 'w':
perms[1] = 'w'
if char == 'x':
perms[2] = 'x'
p = ''.join(perms)
return [d,t,e,p]
def get_acls(module,path,follow):
2013-09-11 05:13:36 +02:00
cmd = [ module.get_bin_path('getfacl', True) ]
if not follow:
cmd.append('-h')
# prevents absolute path warnings and removes headers
2013-10-14 16:48:30 +02:00
cmd.append('--omit-header')
cmd.append('--absolute-names')
2013-09-11 05:13:36 +02:00
cmd.append(path)
return _run_acl(module,cmd)
2013-12-09 17:14:09 +01:00
def set_acl(module,path,entry,follow,default):
2013-09-11 05:13:36 +02:00
cmd = [ module.get_bin_path('setfacl', True) ]
if not follow:
cmd.append('-h')
2013-12-09 17:14:09 +01:00
if default:
cmd.append('-d')
2013-09-11 05:13:36 +02:00
cmd.append('-m "%s"' % entry)
cmd.append(path)
return _run_acl(module,cmd)
2013-12-09 17:14:09 +01:00
def rm_acl(module,path,entry,follow,default):
2013-09-11 05:13:36 +02:00
cmd = [ module.get_bin_path('setfacl', True) ]
if not follow:
cmd.append('-h')
2013-12-09 17:14:09 +01:00
if default:
cmd.append('-k')
2013-09-11 05:13:36 +02:00
entry = entry[0:entry.rfind(':')]
cmd.append('-x "%s"' % entry)
cmd.append(path)
return _run_acl(module,cmd,False)
def _run_acl(module,cmd,check_rc=True):
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)
2013-12-09 17:14:09 +01:00
# trim last line as it is always empty
ret = out.splitlines()
return ret[0:len(ret)-1]
2013-08-23 05:35:24 +02:00
2013-07-21 17:18:31 +02:00
def main():
module = AnsibleModule(
argument_spec = dict(
2013-12-09 17:14:09 +01:00
name = dict(required=True,aliases=['path'], type='str'),
2014-02-06 00:36:29 +01:00
entry = dict(required=False, etype='str'),
2013-12-09 17:14:09 +01:00
entity = dict(required=False, type='str', default=''),
2014-02-06 00:36:29 +01:00
etype = dict(required=False, choices=['other', 'user', 'group', 'mask'], type='str'),
2013-12-09 17:14:09 +01:00
permissions = dict(required=False, type='str'),
2013-09-11 05:13:36 +02:00
state = dict(required=False, default='query', choices=[ 'query', 'present', 'absent' ], type='str'),
2013-07-21 17:18:31 +02:00
follow = dict(required=False, type='bool', default=True),
2013-12-09 17:14:09 +01:00
default= 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
2013-07-21 17:18:31 +02:00
path = module.params.get('name')
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')
2013-12-09 17:14:09 +01: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')
2013-07-21 17:18:31 +02:00
if not os.path.exists(path):
module.fail_json(msg="path not found or not accessible!")
2013-12-09 17:14:09 +01:00
if state in ['present','absent']:
2014-02-06 00:36:29 +01:00
if not entry and not etype:
2014-03-16 17:24:04 +01:00
module.fail_json(msg="%s requires either etype and permissions or just entry be set" % 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-02-06 00:36:29 +01:00
module.fail_json(msg="entry and another incompatible field (entity, etype or permissions) are also set")
2013-12-09 17:14:09 +01:00
if entry.count(":") not in [2,3]:
module.fail_json(msg="Invalid entry: '%s', it requires 3 or 4 sections divided by ':'" % entry)
2014-02-06 00:36:29 +01:00
default, etype, entity, permissions = split_entry(entry)
2013-07-21 17:18:31 +02:00
changed=False
msg = ""
2013-12-09 17:14:09 +01:00
currentacls = get_acls(module,path,follow)
2013-08-23 05:35:24 +02:00
2013-07-21 17:18:31 +02:00
if (state == 'present'):
2013-09-11 05:13:36 +02:00
matched = False
2013-12-09 17:14:09 +01:00
for oldentry in currentacls:
if oldentry.count(":") == 0:
continue
old_default, old_type, old_entity, old_permissions = split_entry(oldentry)
if old_default == default:
2014-02-06 00:36:29 +01:00
if old_type == etype:
if etype in ['user', 'group']:
2013-12-09 17:14:09 +01:00
if old_entity == entity:
matched = True
if not old_permissions == permissions:
changed = True
break
else:
2013-07-21 17:18:31 +02:00
matched = True
2013-12-09 17:14:09 +01:00
if not old_permissions == permissions:
changed = True
break
2013-09-11 05:13:36 +02:00
if not matched:
2013-12-09 17:14:09 +01:00
changed=True
if changed and not module.check_mode:
2014-02-06 00:36:29 +01:00
set_acl(module,path,':'.join([etype, str(entity), permissions]),follow,default)
msg="%s is present" % ':'.join([etype, str(entity), permissions])
2013-12-09 17:14:09 +01:00
2013-07-21 17:18:31 +02:00
elif state == 'absent':
2013-12-09 17:14:09 +01:00
for oldentry in currentacls:
if oldentry.count(":") == 0:
continue
old_default, old_type, old_entity, old_permissions = split_entry(oldentry)
if old_default == default:
2014-02-06 00:36:29 +01:00
if old_type == etype:
if etype in ['user', 'group']:
2013-12-09 17:14:09 +01:00
if old_entity == entity:
changed=True
break
else:
changed=True
2013-07-21 17:18:31 +02:00
break
2013-12-09 17:14:09 +01:00
if changed and not module.check_mode:
2014-02-06 00:36:29 +01:00
rm_acl(module,path,':'.join([etype, entity, '---']),follow,default)
msg="%s is absent" % ':'.join([etype, entity, '---'])
2013-07-21 17:18:31 +02:00
else:
msg="current acl"
2013-12-09 17:14:09 +01:00
if changed:
currentacls = get_acls(module,path,follow)
2013-07-21 17:18:31 +02:00
2013-12-09 17:14:09 +01:00
module.exit_json(changed=changed, msg=msg, acl=currentacls)
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()