2012-03-16 02:53:14 +01:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import shutil
|
|
|
|
import stat
|
|
|
|
import grp
|
|
|
|
import pwd
|
2012-04-12 19:33:10 +02:00
|
|
|
try:
|
|
|
|
import selinux
|
|
|
|
HAVE_SELINUX=True
|
|
|
|
except ImportError:
|
|
|
|
HAVE_SELINUX=False
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-09-19 16:09:26 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: file
|
|
|
|
short_description: Sets attributes of files
|
|
|
|
description:
|
|
|
|
- Sets attributes of files, symlinks, and directories, or removes
|
|
|
|
files/symlinks/directories. Many other modules support the same options as
|
|
|
|
the file module - including M(copy), M(template), and M(assmeble).
|
|
|
|
options:
|
2012-10-13 04:08:07 +02:00
|
|
|
path:
|
2012-09-28 03:06:31 +02:00
|
|
|
description:
|
|
|
|
- defines the file being managed, unless when used with I(state=link), and then sets the destination to create a symbolic link to using I(src)
|
|
|
|
required: true
|
|
|
|
default: []
|
|
|
|
aliases: []
|
|
|
|
state:
|
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- If C(directory), all immediate subdirectories will be created if they
|
|
|
|
do not exist. If C(file), the file will NOT be created if it does not
|
|
|
|
exist, see the M(copy) or M(template) module if you want that behavior.
|
|
|
|
If C(link), the symbolic link will be created or changed. If C(absent),
|
|
|
|
directories will be recursively deleted, and files or symlinks will be
|
|
|
|
unlinked.
|
2012-09-28 03:06:31 +02:00
|
|
|
required: false
|
|
|
|
default: file
|
|
|
|
choices: [ file, link, directory, absent ]
|
|
|
|
mode:
|
2012-10-01 09:18:54 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
description:
|
|
|
|
- mode the file or directory should be, such as 0644 as would be fed to
|
|
|
|
owner:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
description:
|
|
|
|
- name of the user that should own the file/directory, as would be fed to I(chown)
|
|
|
|
group:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
description:
|
|
|
|
- name of the group that should own the file/directory, as would be fed to I(chown)
|
|
|
|
src:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
description:
|
|
|
|
- path of the file to link to (applies only to C(state=link)).
|
|
|
|
seuser:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
description:
|
|
|
|
- user part of SELinux file context. Will default to system policy, if
|
|
|
|
applicable. If set to C(_default), it will use the C(user) portion of the
|
|
|
|
the policy if available
|
|
|
|
serole:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
description:
|
|
|
|
- role part of SELinux file context, C(_default) feature works as for I(seuser).
|
|
|
|
setype:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: []
|
|
|
|
description:
|
|
|
|
- type part of SELinux file context, C(_default) feature works as for I(seuser).
|
|
|
|
selevel:
|
|
|
|
required: false
|
|
|
|
default: "s0"
|
|
|
|
choices: []
|
|
|
|
description:
|
|
|
|
- level part of the SELinux file context. This is the MLS/MCS attribute,
|
|
|
|
sometimes known as the C(range). C(_default) feature works as for
|
|
|
|
I(seuser).
|
|
|
|
context:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: [ "default" ]
|
2012-09-28 03:06:31 +02:00
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- accepts only C(default) as value. This will restore a file's SELinux context
|
|
|
|
in the policy. Does nothing if no default value is available.
|
2012-09-19 16:09:26 +02:00
|
|
|
examples:
|
|
|
|
- code: file path=/etc/foo.conf owner=foo group=foo mode=0644
|
|
|
|
description: Example from Ansible Playbooks
|
2012-10-01 09:18:54 +02:00
|
|
|
- code: file src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link
|
2012-09-19 16:09:26 +02:00
|
|
|
notes:
|
|
|
|
- See also M(copy), M(template), M(assemble)
|
|
|
|
requirements: [ ]
|
2012-10-01 09:18:54 +02:00
|
|
|
author: Michael DeHaan
|
2012-09-19 16:09:26 +02:00
|
|
|
'''
|
|
|
|
|
2012-03-16 03:04:04 +01:00
|
|
|
def add_path_info(kwargs):
|
|
|
|
path = kwargs['path']
|
|
|
|
if os.path.exists(path):
|
|
|
|
(user, group) = user_and_group(path)
|
2012-08-01 06:21:36 +02:00
|
|
|
kwargs['owner'] = user
|
2012-03-16 03:04:04 +01:00
|
|
|
kwargs['group'] = group
|
|
|
|
st = os.stat(path)
|
2012-04-26 14:55:31 +02:00
|
|
|
kwargs['mode'] = oct(stat.S_IMODE(st[stat.ST_MODE]))
|
2012-03-16 03:04:04 +01:00
|
|
|
# secontext not yet supported
|
2012-04-02 01:10:23 +02:00
|
|
|
if os.path.islink(path):
|
|
|
|
kwargs['state'] = 'link'
|
2012-08-21 19:36:48 +02:00
|
|
|
elif os.path.isdir(path):
|
2012-03-16 03:04:04 +01:00
|
|
|
kwargs['state'] = 'directory'
|
2012-08-21 19:36:48 +02:00
|
|
|
else:
|
|
|
|
kwargs['state'] = 'file'
|
2012-04-26 16:23:18 +02:00
|
|
|
if HAVE_SELINUX and selinux_enabled():
|
2012-04-12 19:33:10 +02:00
|
|
|
kwargs['secontext'] = ':'.join(selinux_context(path))
|
2012-03-16 03:04:04 +01:00
|
|
|
else:
|
|
|
|
kwargs['state'] = 'absent'
|
2012-08-07 02:07:02 +02:00
|
|
|
return kwargs
|
2012-04-26 16:23:18 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
def module_exit_json(**kwargs):
|
|
|
|
add_path_info(kwargs)
|
|
|
|
module.exit_json(**kwargs)
|
|
|
|
|
|
|
|
def module_fail_json(**kwargs):
|
|
|
|
add_path_info(kwargs)
|
|
|
|
module.fail_json(**kwargs)
|
|
|
|
|
2012-04-26 16:23:18 +02:00
|
|
|
# Detect whether using selinux that is MLS-aware.
|
|
|
|
# While this means you can set the level/range with
|
|
|
|
# selinux.lsetfilecon(), it may or may not mean that you
|
|
|
|
# will get the selevel as part of the context returned
|
|
|
|
# by selinux.lgetfilecon().
|
|
|
|
def selinux_mls_enabled():
|
|
|
|
if not HAVE_SELINUX:
|
|
|
|
return False
|
|
|
|
if selinux.is_selinux_mls_enabled() == 1:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def selinux_enabled():
|
|
|
|
if not HAVE_SELINUX:
|
|
|
|
return False
|
|
|
|
if selinux.is_selinux_enabled() == 1:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Determine whether we need a placeholder for selevel/mls
|
|
|
|
def selinux_initial_context():
|
|
|
|
context = [None, None, None]
|
|
|
|
if selinux_mls_enabled():
|
|
|
|
context.append(None)
|
|
|
|
return context
|
|
|
|
|
2012-04-22 08:27:34 +02:00
|
|
|
# If selinux fails to find a default, return an array of None
|
|
|
|
def selinux_default_context(path, mode=0):
|
2012-04-26 16:23:18 +02:00
|
|
|
context = selinux_initial_context()
|
|
|
|
if not HAVE_SELINUX or not selinux_enabled():
|
2012-04-22 08:27:34 +02:00
|
|
|
return context
|
|
|
|
try:
|
|
|
|
ret = selinux.matchpathcon(path, mode)
|
|
|
|
except OSError:
|
|
|
|
return context
|
|
|
|
if ret[0] == -1:
|
|
|
|
return context
|
|
|
|
context = ret[1].split(':')
|
|
|
|
return context
|
|
|
|
|
2012-04-25 18:39:11 +02:00
|
|
|
def selinux_context(path):
|
2012-04-26 16:23:18 +02:00
|
|
|
context = selinux_initial_context()
|
|
|
|
if not HAVE_SELINUX or not selinux_enabled():
|
2012-04-25 18:39:11 +02:00
|
|
|
return context
|
|
|
|
try:
|
|
|
|
ret = selinux.lgetfilecon(path)
|
|
|
|
except:
|
2012-07-31 03:50:32 +02:00
|
|
|
module_fail_json(path=path, msg='failed to retrieve selinux context')
|
2012-04-25 18:39:11 +02:00
|
|
|
if ret[0] == -1:
|
|
|
|
return context
|
|
|
|
context = ret[1].split(':')
|
|
|
|
return context
|
|
|
|
|
2012-03-16 02:53:14 +01:00
|
|
|
# ===========================================
|
|
|
|
# support functions
|
|
|
|
|
|
|
|
def user_and_group(filename):
|
|
|
|
st = os.stat(filename)
|
|
|
|
uid = st.st_uid
|
|
|
|
gid = st.st_gid
|
2012-06-30 02:05:23 +02:00
|
|
|
try:
|
|
|
|
user = pwd.getpwuid(uid)[0]
|
|
|
|
except KeyError:
|
2012-07-01 23:38:17 +02:00
|
|
|
user = str(uid)
|
2012-06-30 02:05:23 +02:00
|
|
|
try:
|
|
|
|
group = grp.getgrgid(gid)[0]
|
|
|
|
except KeyError:
|
2012-07-01 23:38:17 +02:00
|
|
|
group = str(gid)
|
2012-03-16 02:53:14 +01:00
|
|
|
return (user, group)
|
|
|
|
|
|
|
|
def set_context_if_different(path, context, changed):
|
2012-04-26 16:23:18 +02:00
|
|
|
if not HAVE_SELINUX or not selinux_enabled():
|
2012-04-12 19:33:10 +02:00
|
|
|
return changed
|
|
|
|
cur_context = selinux_context(path)
|
2012-04-22 08:27:34 +02:00
|
|
|
new_context = list(cur_context)
|
2012-04-26 16:23:18 +02:00
|
|
|
# Iterate over the current context instead of the
|
|
|
|
# argument context, which may have selevel.
|
|
|
|
for i in range(len(cur_context)):
|
2012-04-12 19:33:10 +02:00
|
|
|
if context[i] is not None and context[i] != cur_context[i]:
|
|
|
|
new_context[i] = context[i]
|
|
|
|
if cur_context != new_context:
|
|
|
|
try:
|
|
|
|
rc = selinux.lsetfilecon(path, ':'.join(new_context))
|
|
|
|
except OSError:
|
2012-07-31 03:50:32 +02:00
|
|
|
module_fail_json(path=path, msg='invalid selinux context')
|
2012-04-12 19:33:10 +02:00
|
|
|
if rc != 0:
|
2012-07-31 03:50:32 +02:00
|
|
|
module_fail_json(path=path, msg='set selinux context failed')
|
2012-04-12 19:33:10 +02:00
|
|
|
changed = True
|
|
|
|
return changed
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-16 02:53:14 +01:00
|
|
|
def set_owner_if_different(path, owner, changed):
|
2012-08-11 18:35:58 +02:00
|
|
|
if owner is None:
|
|
|
|
return changed
|
|
|
|
user, group = user_and_group(path)
|
|
|
|
if owner != user:
|
|
|
|
try:
|
|
|
|
uid = pwd.getpwnam(owner).pw_uid
|
|
|
|
except KeyError:
|
|
|
|
module_fail_json(path=path, msg='chown failed: failed to look up user %s' % owner)
|
|
|
|
try:
|
|
|
|
os.chown(path, uid, -1)
|
|
|
|
except OSError:
|
|
|
|
module_fail_json(path=path, msg='chown failed')
|
|
|
|
return True
|
|
|
|
|
|
|
|
return changed
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-16 02:53:14 +01:00
|
|
|
def set_group_if_different(path, group, changed):
|
2012-08-11 18:35:58 +02:00
|
|
|
if group is None:
|
|
|
|
return changed
|
|
|
|
old_user, old_group = user_and_group(path)
|
|
|
|
if old_group != group:
|
|
|
|
try:
|
|
|
|
gid = grp.getgrnam(group).gr_gid
|
|
|
|
except KeyError:
|
|
|
|
module_fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group)
|
|
|
|
try:
|
|
|
|
os.chown(path, -1, gid)
|
|
|
|
except OSError:
|
|
|
|
module_fail_json(path=path, msg='chgrp failed')
|
|
|
|
return True
|
|
|
|
return changed
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
def set_mode_if_different(path, mode, changed):
|
2012-08-11 18:35:58 +02:00
|
|
|
if mode is None:
|
|
|
|
return changed
|
|
|
|
try:
|
|
|
|
# FIXME: support English modes
|
|
|
|
mode = int(mode, 8)
|
|
|
|
except Exception, e:
|
|
|
|
module_fail_json(path=path, msg='mode needs to be something octalish', details=str(e))
|
2012-03-16 03:35:59 +01:00
|
|
|
|
2012-08-11 18:35:58 +02:00
|
|
|
st = os.stat(path)
|
|
|
|
prev_mode = stat.S_IMODE(st[stat.ST_MODE])
|
2012-03-16 03:35:59 +01:00
|
|
|
|
2012-08-11 18:35:58 +02:00
|
|
|
if prev_mode != mode:
|
|
|
|
# FIXME: comparison against string above will cause this to be executed
|
|
|
|
# every time
|
|
|
|
try:
|
|
|
|
os.chmod(path, mode)
|
|
|
|
except Exception, e:
|
2012-08-12 00:39:09 +02:00
|
|
|
module_fail_json(path=path, msg='chmod failed', details=str(e))
|
2012-08-11 18:35:58 +02:00
|
|
|
|
|
|
|
st = os.stat(path)
|
|
|
|
new_mode = stat.S_IMODE(st[stat.ST_MODE])
|
|
|
|
|
|
|
|
if new_mode != prev_mode:
|
|
|
|
return True
|
|
|
|
return changed
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-04-02 01:10:23 +02:00
|
|
|
|
2012-03-16 02:53:14 +01:00
|
|
|
def rmtree_error(func, path, exc_info):
|
2012-07-31 03:50:32 +02:00
|
|
|
module_fail_json(path=path, msg='failed to remove directory')
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
def main():
|
|
|
|
|
2012-08-11 18:35:58 +02:00
|
|
|
# FIXME: pass this around, should not use global
|
2012-07-31 03:50:32 +02:00
|
|
|
global module
|
2012-08-11 18:35:58 +02:00
|
|
|
|
2012-08-02 01:42:31 +02:00
|
|
|
module = AnsibleModule(
|
|
|
|
check_invalid_arguments = False,
|
2012-07-31 03:50:32 +02:00
|
|
|
argument_spec = dict(
|
|
|
|
state = dict(choices=['file','directory','link','absent'], default='file'),
|
|
|
|
path = dict(aliases=['dest', 'name'], required=True),
|
|
|
|
src = dict(),
|
|
|
|
mode = dict(),
|
|
|
|
owner = dict(),
|
|
|
|
group = dict(),
|
|
|
|
seuser = dict(),
|
|
|
|
serole = dict(),
|
|
|
|
selevel = dict(),
|
2012-08-02 07:24:09 +02:00
|
|
|
setype = dict(),
|
2012-07-31 03:50:32 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
|
|
|
state = params['state']
|
|
|
|
path = os.path.expanduser(params['path'])
|
2012-10-13 02:07:05 +02:00
|
|
|
|
|
|
|
# source is both the source of a symlink or an informational passing of the src for a template module
|
|
|
|
# or copy module, even if this module never uses it, it is needed to key off some things
|
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
src = params.get('src', None)
|
|
|
|
if src:
|
|
|
|
src = os.path.expanduser(src)
|
|
|
|
|
2012-10-13 02:07:05 +02:00
|
|
|
if src is not None and os.path.isdir(path):
|
|
|
|
params['path'] = path = os.path.join(path, os.path.basename(src))
|
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
mode = params.get('mode', None)
|
|
|
|
owner = params.get('owner', None)
|
|
|
|
group = params.get('group', None)
|
|
|
|
|
|
|
|
# selinux related options
|
|
|
|
seuser = params.get('seuser', None)
|
|
|
|
serole = params.get('serole', None)
|
|
|
|
setype = params.get('setype', None)
|
|
|
|
selevel = params.get('serange', 's0')
|
|
|
|
secontext = [seuser, serole, setype]
|
|
|
|
if selinux_mls_enabled():
|
|
|
|
secontext.append(selevel)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
default_secontext = selinux_default_context(path)
|
|
|
|
for i in range(len(default_secontext)):
|
|
|
|
if i is not None and secontext[i] == '_default':
|
|
|
|
secontext[i] = default_secontext[i]
|
|
|
|
|
|
|
|
if state == 'link' and (src is None or path is None):
|
|
|
|
module_fail_json(msg='src and dest are required for "link" state')
|
|
|
|
elif path is None:
|
|
|
|
module_fail_json(msg='path is required')
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
prev_state = 'absent'
|
2012-10-13 02:07:05 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
if os.path.lexists(path):
|
|
|
|
if os.path.islink(path):
|
|
|
|
prev_state = 'link'
|
2012-08-21 19:36:48 +02:00
|
|
|
elif os.path.isdir(path):
|
2012-07-31 03:50:32 +02:00
|
|
|
prev_state = 'directory'
|
2012-08-21 19:36:48 +02:00
|
|
|
else:
|
|
|
|
prev_state = 'file'
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
if prev_state != 'absent' and state == 'absent':
|
|
|
|
try:
|
|
|
|
if prev_state == 'directory':
|
|
|
|
if os.path.islink(path):
|
|
|
|
os.unlink(path)
|
|
|
|
else:
|
|
|
|
shutil.rmtree(path, ignore_errors=False, onerror=rmtree_error)
|
|
|
|
else:
|
|
|
|
os.unlink(path)
|
|
|
|
except Exception, e:
|
|
|
|
module_fail_json(path=path, msg=str(e))
|
|
|
|
module_exit_json(path=path, changed=True)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-10-17 00:14:42 +02:00
|
|
|
if prev_state != 'absent' and prev_state != state:
|
2012-10-13 02:07:05 +02:00
|
|
|
module_fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, src))
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
if prev_state == 'absent' and state == 'absent':
|
|
|
|
module_exit_json(path=path, changed=False)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
if state == 'file':
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-09-19 03:02:16 +02:00
|
|
|
if prev_state != 'file':
|
2012-10-13 02:07:05 +02:00
|
|
|
module_fail_json(path=path, msg='file (%s) does not exist, use copy or template module to create' % path)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
# set modes owners and context as needed
|
|
|
|
changed = set_context_if_different(path, secontext, changed)
|
|
|
|
changed = set_owner_if_different(path, owner, changed)
|
|
|
|
changed = set_group_if_different(path, group, changed)
|
|
|
|
changed = set_mode_if_different(path, mode, changed)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
module_exit_json(path=path, changed=changed)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
elif state == 'directory':
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
if prev_state == 'absent':
|
|
|
|
os.makedirs(path)
|
|
|
|
changed = True
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
# set modes owners and context as needed
|
|
|
|
changed = set_context_if_different(path, secontext, changed)
|
|
|
|
changed = set_owner_if_different(path, owner, changed)
|
|
|
|
changed = set_group_if_different(path, group, changed)
|
|
|
|
changed = set_mode_if_different(path, mode, changed)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
module_exit_json(path=path, changed=changed)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
elif state == 'link':
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
if os.path.isabs(src):
|
|
|
|
abs_src = src
|
|
|
|
else:
|
2012-08-10 03:46:03 +02:00
|
|
|
module.fail_json(msg="absolute paths are required")
|
2012-07-31 03:50:32 +02:00
|
|
|
if not os.path.exists(abs_src):
|
2012-08-02 07:23:02 +02:00
|
|
|
module_fail_json(path=path, src=src, msg='src file does not exist')
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
if prev_state == 'absent':
|
|
|
|
os.symlink(src, path)
|
|
|
|
changed = True
|
|
|
|
elif prev_state == 'link':
|
|
|
|
old_src = os.readlink(path)
|
|
|
|
if not os.path.isabs(old_src):
|
|
|
|
old_src = os.path.join(os.path.dirname(path), old_src)
|
|
|
|
if old_src != src:
|
|
|
|
os.unlink(path)
|
|
|
|
os.symlink(src, path)
|
2012-09-19 03:02:16 +02:00
|
|
|
changed = True
|
2012-07-31 03:50:32 +02:00
|
|
|
else:
|
|
|
|
module_fail_json(dest=path, src=src, msg='unexpected position reached')
|
|
|
|
|
|
|
|
# set modes owners and context as needed
|
|
|
|
changed = set_context_if_different(path, secontext, changed)
|
|
|
|
changed = set_owner_if_different(path, owner, changed)
|
|
|
|
changed = set_group_if_different(path, group, changed)
|
|
|
|
changed = set_mode_if_different(path, mode, changed)
|
2012-04-02 01:10:23 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
module.exit_json(dest=path, src=src, changed=changed)
|
2012-04-02 01:10:23 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
module_fail_json(path=path, msg='unexpected position reached')
|
2012-04-02 01:10:23 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
2012-08-01 06:21:36 +02:00
|
|
|
main()
|
2012-03-16 02:53:14 +01:00
|
|
|
|