mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
9.2 KiB
Plaintext
296 lines
9.2 KiB
Plaintext
11 years ago
|
#!/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
|
||
11 years ago
|
version_added: "1.4"
|
||
|
short_description: Sets and retrieves file ACL information.
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Sets and retrieves file ACL information.
|
||
11 years ago
|
options:
|
||
|
name:
|
||
|
required: true
|
||
11 years ago
|
default: null
|
||
11 years ago
|
description:
|
||
11 years ago
|
- The full path of the file or object.
|
||
11 years ago
|
aliases: ['path']
|
||
11 years ago
|
|
||
11 years ago
|
state:
|
||
|
required: false
|
||
11 years ago
|
default: query
|
||
|
choices: [ 'query', 'present', 'absent' ]
|
||
11 years ago
|
description:
|
||
10 years ago
|
- 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.
|
||
11 years ago
|
|
||
11 years ago
|
follow:
|
||
|
required: false
|
||
|
default: yes
|
||
|
choices: [ 'yes', 'no' ]
|
||
|
description:
|
||
11 years ago
|
- whether to follow symlinks on the path if a symlink is encountered.
|
||
11 years ago
|
|
||
|
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.
|
||
|
|
||
11 years ago
|
etype:
|
||
11 years ago
|
version_added: "1.5"
|
||
|
required: false
|
||
|
default: null
|
||
|
choices: [ 'user', 'group', 'mask', 'other' ]
|
||
|
description:
|
||
11 years ago
|
- the entity type of the ACL to apply, see setfacl documentation for more info.
|
||
11 years ago
|
|
||
11 years ago
|
|
||
|
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)
|
||
|
|
||
11 years ago
|
entry:
|
||
11 years ago
|
required: false
|
||
|
default: null
|
||
|
description:
|
||
11 years ago
|
- 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.
|
||
11 years ago
|
|
||
11 years ago
|
author: Brian Coca
|
||
|
notes:
|
||
11 years ago
|
- The "acl" module requires that acls are enabled on the target filesystem and that the setfacl and getfacl binaries are installed.
|
||
11 years ago
|
'''
|
||
|
|
||
|
EXAMPLES = '''
|
||
11 years ago
|
# Grant user Joe read access to a file
|
||
11 years ago
|
- acl: name=/etc/foo.conf entity=joe etype=user permissions="r" state=present
|
||
11 years ago
|
|
||
11 years ago
|
# Removes the acl for Joe on a specific file
|
||
11 years ago
|
- acl: name=/etc/foo.conf entity=joe etype=user state=absent
|
||
11 years ago
|
|
||
|
# Sets default acl for joe on foo.d
|
||
11 years ago
|
- acl: name=/etc/foo.d entity=joe etype=user permissions=rw default=yes state=present
|
||
11 years ago
|
|
||
|
# Same as previous but using entry shorthand
|
||
11 years ago
|
- acl: name=/etc/foo.d entry="default:user:joe:rw-" state=present
|
||
11 years ago
|
|
||
|
# Obtain the acl for a specific file
|
||
|
- acl: name=/etc/foo.conf
|
||
|
register: acl_info
|
||
11 years ago
|
'''
|
||
|
|
||
11 years ago
|
def normalize_permissions(p):
|
||
|
perms = ['-','-','-']
|
||
|
for char in p:
|
||
|
if char == 'r':
|
||
|
perms[0] = 'r'
|
||
|
if char == 'w':
|
||
|
perms[1] = 'w'
|
||
|
if char == 'x':
|
||
|
perms[2] = 'x'
|
||
|
return ''.join(perms)
|
||
|
|
||
11 years ago
|
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
|
||
|
|
||
11 years ago
|
if d:
|
||
|
d = True
|
||
|
|
||
11 years ago
|
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
|
||
|
|
||
11 years ago
|
p = normalize_permissions(p)
|
||
11 years ago
|
|
||
|
return [d,t,e,p]
|
||
|
|
||
|
def get_acls(module,path,follow):
|
||
11 years ago
|
|
||
|
cmd = [ module.get_bin_path('getfacl', True) ]
|
||
|
if not follow:
|
||
|
cmd.append('-h')
|
||
|
# prevents absolute path warnings and removes headers
|
||
11 years ago
|
cmd.append('--omit-header')
|
||
|
cmd.append('--absolute-names')
|
||
11 years ago
|
cmd.append(path)
|
||
|
|
||
|
return _run_acl(module,cmd)
|
||
|
|
||
11 years ago
|
def set_acl(module,path,entry,follow,default):
|
||
11 years ago
|
|
||
|
cmd = [ module.get_bin_path('setfacl', True) ]
|
||
|
if not follow:
|
||
|
cmd.append('-h')
|
||
11 years ago
|
if default:
|
||
|
cmd.append('-d')
|
||
11 years ago
|
cmd.append('-m "%s"' % entry)
|
||
|
cmd.append(path)
|
||
|
|
||
|
return _run_acl(module,cmd)
|
||
|
|
||
11 years ago
|
def rm_acl(module,path,entry,follow,default):
|
||
11 years ago
|
|
||
|
cmd = [ module.get_bin_path('setfacl', True) ]
|
||
|
if not follow:
|
||
|
cmd.append('-h')
|
||
11 years ago
|
if default:
|
||
|
cmd.append('-k')
|
||
11 years ago
|
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):
|
||
|
|
||
11 years ago
|
try:
|
||
11 years ago
|
(rc, out, err) = module.run_command(' '.join(cmd), check_rc=check_rc)
|
||
|
except Exception, e:
|
||
|
module.fail_json(msg=e.strerror)
|
||
|
|
||
11 years ago
|
# trim last line as it is always empty
|
||
|
ret = out.splitlines()
|
||
|
return ret[0:len(ret)-1]
|
||
11 years ago
|
|
||
11 years ago
|
def main():
|
||
|
module = AnsibleModule(
|
||
|
argument_spec = dict(
|
||
11 years ago
|
name = dict(required=True,aliases=['path'], type='str'),
|
||
11 years ago
|
entry = dict(required=False, etype='str'),
|
||
11 years ago
|
entity = dict(required=False, type='str', default=''),
|
||
11 years ago
|
etype = dict(required=False, choices=['other', 'user', 'group', 'mask'], type='str'),
|
||
11 years ago
|
permissions = dict(required=False, type='str'),
|
||
11 years ago
|
state = dict(required=False, default='query', choices=[ 'query', 'present', 'absent' ], type='str'),
|
||
11 years ago
|
follow = dict(required=False, type='bool', default=True),
|
||
11 years ago
|
default= dict(required=False, type='bool', default=False),
|
||
11 years ago
|
),
|
||
|
supports_check_mode=True,
|
||
|
)
|
||
11 years ago
|
|
||
10 years ago
|
path = os.path.expanduser(module.params.get('name'))
|
||
11 years ago
|
entry = module.params.get('entry')
|
||
11 years ago
|
entity = module.params.get('entity')
|
||
11 years ago
|
etype = module.params.get('etype')
|
||
10 years ago
|
permissions = module.params.get('permissions')
|
||
11 years ago
|
state = module.params.get('state')
|
||
|
follow = module.params.get('follow')
|
||
11 years ago
|
default = module.params.get('default')
|
||
11 years ago
|
|
||
10 years ago
|
if permissions:
|
||
|
permissions = normalize_permissions(permissions)
|
||
|
|
||
11 years ago
|
if not os.path.exists(path):
|
||
|
module.fail_json(msg="path not found or not accessible!")
|
||
|
|
||
11 years ago
|
if state in ['present','absent']:
|
||
11 years ago
|
if not entry and not etype:
|
||
11 years ago
|
module.fail_json(msg="%s requires either etype and permissions or just entry be set" % state)
|
||
11 years ago
|
|
||
|
if entry:
|
||
11 years ago
|
if etype or entity or permissions:
|
||
11 years ago
|
module.fail_json(msg="entry and another incompatible field (entity, etype or permissions) are also set")
|
||
11 years ago
|
if entry.count(":") not in [2,3]:
|
||
|
module.fail_json(msg="Invalid entry: '%s', it requires 3 or 4 sections divided by ':'" % entry)
|
||
|
|
||
11 years ago
|
default, etype, entity, permissions = split_entry(entry)
|
||
11 years ago
|
|
||
|
changed=False
|
||
|
msg = ""
|
||
11 years ago
|
currentacls = get_acls(module,path,follow)
|
||
11 years ago
|
|
||
11 years ago
|
if (state == 'present'):
|
||
11 years ago
|
matched = False
|
||
11 years ago
|
for oldentry in currentacls:
|
||
|
if oldentry.count(":") == 0:
|
||
|
continue
|
||
|
old_default, old_type, old_entity, old_permissions = split_entry(oldentry)
|
||
|
if old_default == default:
|
||
11 years ago
|
if old_type == etype:
|
||
|
if etype in ['user', 'group']:
|
||
11 years ago
|
if old_entity == entity:
|
||
|
matched = True
|
||
|
if not old_permissions == permissions:
|
||
|
changed = True
|
||
|
break
|
||
|
else:
|
||
11 years ago
|
matched = True
|
||
11 years ago
|
if not old_permissions == permissions:
|
||
|
changed = True
|
||
|
break
|
||
11 years ago
|
if not matched:
|
||
11 years ago
|
changed=True
|
||
|
|
||
|
if changed and not module.check_mode:
|
||
11 years ago
|
set_acl(module,path,':'.join([etype, str(entity), permissions]),follow,default)
|
||
|
msg="%s is present" % ':'.join([etype, str(entity), permissions])
|
||
11 years ago
|
|
||
11 years ago
|
elif state == 'absent':
|
||
11 years ago
|
for oldentry in currentacls:
|
||
|
if oldentry.count(":") == 0:
|
||
|
continue
|
||
|
old_default, old_type, old_entity, old_permissions = split_entry(oldentry)
|
||
|
if old_default == default:
|
||
11 years ago
|
if old_type == etype:
|
||
|
if etype in ['user', 'group']:
|
||
11 years ago
|
if old_entity == entity:
|
||
|
changed=True
|
||
|
break
|
||
|
else:
|
||
|
changed=True
|
||
11 years ago
|
break
|
||
11 years ago
|
if changed and not module.check_mode:
|
||
11 years ago
|
rm_acl(module,path,':'.join([etype, entity, '---']),follow,default)
|
||
|
msg="%s is absent" % ':'.join([etype, entity, '---'])
|
||
11 years ago
|
else:
|
||
|
msg="current acl"
|
||
|
|
||
11 years ago
|
if changed:
|
||
|
currentacls = get_acls(module,path,follow)
|
||
11 years ago
|
|
||
11 years ago
|
module.exit_json(changed=changed, msg=msg, acl=currentacls)
|
||
11 years ago
|
|
||
11 years ago
|
# import module snippets
|
||
11 years ago
|
from ansible.module_utils.basic import *
|
||
11 years ago
|
|
||
|
main()
|