2012-03-16 01:53:14 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-03-16 01:53:14 +00: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 17:33:10 +00:00
|
|
|
try:
|
|
|
|
import selinux
|
|
|
|
HAVE_SELINUX=True
|
|
|
|
except ImportError:
|
|
|
|
HAVE_SELINUX=False
|
2012-03-16 01:53:14 +00:00
|
|
|
|
2012-09-19 14:09:26 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: file
|
2013-11-18 23:55:49 +00:00
|
|
|
version_added: "historical"
|
2012-09-19 14:09:26 +00:00
|
|
|
short_description: Sets attributes of files
|
2014-04-03 20:51:13 +00:00
|
|
|
extends_documentation_fragment: files
|
2012-09-19 14:09:26 +00:00
|
|
|
description:
|
|
|
|
- Sets attributes of files, symlinks, and directories, or removes
|
|
|
|
files/symlinks/directories. Many other modules support the same options as
|
2012-11-22 06:23:10 +00:00
|
|
|
the M(file) module - including M(copy), M(template), and M(assemble).
|
2012-09-19 14:09:26 +00:00
|
|
|
notes:
|
|
|
|
- See also M(copy), M(template), M(assemble)
|
|
|
|
requirements: [ ]
|
2012-10-01 07:18:54 +00:00
|
|
|
author: Michael DeHaan
|
2012-09-19 14:09:26 +00:00
|
|
|
'''
|
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
EXAMPLES = '''
|
2013-07-20 22:08:42 +00:00
|
|
|
- file: path=/etc/foo.conf owner=foo group=foo mode=0644
|
2013-06-14 09:53:43 +00:00
|
|
|
- file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link
|
2014-04-23 14:00:22 +00:00
|
|
|
- file: src=/tmp/{{ item.path }} dest={{ item.dest }} state=link
|
2014-02-07 13:29:20 +00:00
|
|
|
with_items:
|
|
|
|
- { path: 'x', dest: 'y' }
|
|
|
|
- { path: 'z', dest: 'k' }
|
2013-06-14 09:53:43 +00:00
|
|
|
'''
|
|
|
|
|
2012-07-31 01:50:32 +00:00
|
|
|
def main():
|
|
|
|
|
2012-08-01 23:42:31 +00:00
|
|
|
module = AnsibleModule(
|
2012-07-31 01:50:32 +00:00
|
|
|
argument_spec = dict(
|
2013-11-01 13:31:32 +00:00
|
|
|
state = dict(choices=['file','directory','link','hard','touch','absent'], default=None),
|
2013-07-20 22:08:42 +00:00
|
|
|
path = dict(aliases=['dest', 'name'], required=True),
|
2013-10-14 19:10:27 +00:00
|
|
|
original_basename = dict(required=False), # Internal use only, for recursive ops
|
2013-02-27 01:57:36 +00:00
|
|
|
recurse = dict(default='no', type='bool'),
|
2013-05-30 10:53:24 +00:00
|
|
|
force = dict(required=False,default=False,type='bool'),
|
2013-03-27 03:12:56 +00:00
|
|
|
diff_peek = dict(default=None),
|
|
|
|
validate = dict(required=False, default=None),
|
2014-03-14 03:07:35 +00:00
|
|
|
src = dict(required=False, default=None),
|
2012-10-21 02:51:36 +00:00
|
|
|
),
|
2013-02-04 00:46:25 +00:00
|
|
|
add_file_common_args=True,
|
|
|
|
supports_check_mode=True
|
2012-07-31 01:50:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
|
|
|
state = params['state']
|
2013-05-30 10:53:24 +00:00
|
|
|
force = params['force']
|
2014-03-14 03:07:35 +00:00
|
|
|
diff_peek = params['diff_peek']
|
|
|
|
src = params['src']
|
|
|
|
|
|
|
|
# modify source as we later reload and pass, specially relevant when used by other modules.
|
2013-07-20 22:08:42 +00:00
|
|
|
params['path'] = path = os.path.expanduser(params['path'])
|
2012-10-13 00:07:05 +00:00
|
|
|
|
2013-02-25 22:32:52 +00:00
|
|
|
# short-circuit for diff_peek
|
2014-03-14 03:07:35 +00:00
|
|
|
if diff_peek is not None:
|
2013-02-25 22:32:52 +00:00
|
|
|
appears_binary = False
|
|
|
|
try:
|
2013-07-20 22:08:42 +00:00
|
|
|
f = open(path)
|
2013-02-25 22:32:52 +00:00
|
|
|
b = f.read(8192)
|
|
|
|
f.close()
|
2014-01-23 15:02:17 +00:00
|
|
|
if "\x00" in b:
|
2013-02-25 22:32:52 +00:00
|
|
|
appears_binary = True
|
|
|
|
except:
|
|
|
|
pass
|
2013-07-20 22:08:42 +00:00
|
|
|
module.exit_json(path=path, changed=False, appears_binary=appears_binary)
|
2013-02-25 22:32:52 +00:00
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
# Find out current state
|
2013-11-01 13:31:32 +00:00
|
|
|
prev_state = 'absent'
|
|
|
|
if os.path.lexists(path):
|
|
|
|
if os.path.islink(path):
|
|
|
|
prev_state = 'link'
|
|
|
|
elif os.path.isdir(path):
|
|
|
|
prev_state = 'directory'
|
|
|
|
elif os.stat(path).st_nlink > 1:
|
|
|
|
prev_state = 'hard'
|
|
|
|
else:
|
|
|
|
# could be many other things, but defaulting to file
|
|
|
|
prev_state = 'file'
|
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
# state should default to file, but since that creates many conflicts,
|
|
|
|
# default to 'current' when it exists.
|
|
|
|
if state is None:
|
|
|
|
if prev_state != 'absent':
|
|
|
|
state = prev_state
|
|
|
|
else:
|
|
|
|
state = 'file'
|
2013-11-01 13:31:32 +00:00
|
|
|
|
2012-10-13 00:07:05 +00: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
|
2014-03-14 03:07:35 +00:00
|
|
|
if src is not None:
|
2012-07-31 01:50:32 +00:00
|
|
|
src = os.path.expanduser(src)
|
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
# original_basename is used by other modules that depend on file.
|
2014-03-19 02:29:22 +00:00
|
|
|
if os.path.isdir(path) and state not in ["link", "absent"]:
|
2014-03-14 03:07:35 +00:00
|
|
|
if params['original_basename']:
|
|
|
|
basename = params['original_basename']
|
|
|
|
else:
|
|
|
|
basename = os.path.basename(src)
|
|
|
|
params['path'] = path = os.path.join(path, basename)
|
|
|
|
else:
|
|
|
|
if state in ['link','hard']:
|
|
|
|
module.fail_json(msg='src and dest are required for creating links')
|
2012-10-13 00:07:05 +00:00
|
|
|
|
2012-10-21 02:51:36 +00:00
|
|
|
file_args = module.load_file_common_arguments(params)
|
2012-07-31 01:50:32 +00:00
|
|
|
changed = False
|
|
|
|
|
2013-10-12 01:56:14 +00:00
|
|
|
recurse = params['recurse']
|
2014-03-14 03:07:35 +00:00
|
|
|
if recurse and state != 'directory':
|
|
|
|
module.fail_json(path=path, msg="recurse option requires state to be 'directory'")
|
2013-10-12 01:56:14 +00:00
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
if state == 'absent':
|
|
|
|
if state != prev_state:
|
|
|
|
if not module.check_mode:
|
|
|
|
if prev_state == 'directory':
|
2012-10-21 02:51:36 +00:00
|
|
|
try:
|
2013-07-20 22:08:42 +00:00
|
|
|
shutil.rmtree(path, ignore_errors=False)
|
2013-12-04 12:13:49 +00:00
|
|
|
except Exception, e:
|
2013-12-13 20:34:02 +00:00
|
|
|
module.fail_json(msg="rmtree failed: %s" % str(e))
|
2014-03-14 03:07:35 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
os.unlink(path)
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(path=path, msg="unlinking failed: %s " % str(e))
|
|
|
|
module.exit_json(path=path, changed=True)
|
|
|
|
else:
|
|
|
|
module.exit_json(path=path, changed=False)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
elif state == 'file':
|
|
|
|
if state != prev_state:
|
|
|
|
# file is not absent and any other state is a conflict
|
|
|
|
module.fail_json(path=path, msg='file (%s) is %s, cannot continue' % (path, prev_state))
|
2012-03-16 01:53:14 +00:00
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
2013-07-20 22:08:42 +00:00
|
|
|
module.exit_json(path=path, changed=changed)
|
2012-03-16 01:53:14 +00:00
|
|
|
|
2012-07-31 01:50:32 +00:00
|
|
|
elif state == 'directory':
|
|
|
|
if prev_state == 'absent':
|
2013-04-26 01:33:47 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2012-07-31 01:50:32 +00:00
|
|
|
changed = True
|
2014-04-23 14:38:38 +00:00
|
|
|
curpath = ''
|
|
|
|
for dirname in path.split('/'):
|
|
|
|
curpath = '/'.join([curpath, dirname])
|
|
|
|
if not os.path.exists(curpath):
|
|
|
|
os.mkdir(curpath)
|
|
|
|
tmp_file_args = file_args.copy()
|
|
|
|
tmp_file_args['path']=curpath
|
|
|
|
changed = module.set_fs_attributes_if_different(tmp_file_args, changed)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
|
|
|
|
2013-02-09 17:01:11 +00:00
|
|
|
if recurse:
|
2013-07-20 22:08:42 +00:00
|
|
|
for root,dirs,files in os.walk( file_args['path'] ):
|
2014-03-14 03:07:35 +00:00
|
|
|
for fsobj in dirs + files:
|
|
|
|
fsname=os.path.join(root, fsobj)
|
2013-02-09 17:01:11 +00:00
|
|
|
tmp_file_args = file_args.copy()
|
2014-03-14 03:07:35 +00:00
|
|
|
tmp_file_args['path']=fsname
|
|
|
|
changed = module.set_fs_attributes_if_different(tmp_file_args, changed)
|
|
|
|
|
2013-07-20 22:08:42 +00:00
|
|
|
module.exit_json(path=path, changed=changed)
|
2012-03-16 01:53:14 +00:00
|
|
|
|
2013-06-15 15:14:34 +00:00
|
|
|
elif state in ['link','hard']:
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2014-06-05 03:48:08 +00:00
|
|
|
if os.path.isdir(path) and not os.path.islink(path):
|
2014-05-28 16:46:06 +00:00
|
|
|
relpath = path
|
|
|
|
else:
|
|
|
|
relpath = os.path.dirname(path)
|
2014-06-05 04:31:59 +00:00
|
|
|
|
2014-05-28 16:46:06 +00:00
|
|
|
absrc = os.path.normpath('%s/%s' % (relpath, os.path.basename(src)))
|
2014-06-05 04:31:59 +00:00
|
|
|
if not os.path.exists(src) and not os.path.exists(absrc) and not force:
|
2014-03-19 00:41:42 +00:00
|
|
|
module.fail_json(path=path, src=src, msg='src file does not exist, use "force=yes" if you really want to create the link: %s' % absrc)
|
2014-03-14 03:07:35 +00:00
|
|
|
|
2013-11-04 09:37:36 +00:00
|
|
|
if state == 'hard':
|
2014-03-14 03:07:35 +00:00
|
|
|
if not os.path.isabs(src):
|
2013-11-04 09:37:36 +00:00
|
|
|
module.fail_json(msg="absolute paths are required")
|
2014-05-02 19:45:51 +00:00
|
|
|
elif prev_state == 'directory':
|
|
|
|
if not force:
|
2014-05-14 21:31:20 +00:00
|
|
|
module.fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, path))
|
2014-05-02 19:45:51 +00:00
|
|
|
elif len(os.listdir(path)) > 0:
|
|
|
|
# refuse to replace a directory that has files in it
|
|
|
|
module.fail_json(path=path, msg='the directory %s is not empty, refusing to convert it' % path)
|
|
|
|
elif prev_state in ['file', 'hard'] and not force:
|
2014-05-14 21:54:26 +00:00
|
|
|
module.fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, path))
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-31 01:50:32 +00:00
|
|
|
if prev_state == 'absent':
|
|
|
|
changed = True
|
|
|
|
elif prev_state == 'link':
|
2013-07-20 22:08:42 +00:00
|
|
|
old_src = os.readlink(path)
|
2012-07-31 01:50:32 +00:00
|
|
|
if old_src != src:
|
2013-08-16 03:39:02 +00:00
|
|
|
changed = True
|
|
|
|
elif prev_state == 'hard':
|
|
|
|
if not (state == 'hard' and os.stat(path).st_ino == os.stat(src).st_ino):
|
2014-03-14 03:07:35 +00:00
|
|
|
changed = True
|
2013-08-16 03:39:02 +00:00
|
|
|
if not force:
|
|
|
|
module.fail_json(dest=path, src=src, msg='Cannot link, different hard link exists at destination')
|
2014-03-14 03:07:35 +00:00
|
|
|
elif prev_state in ['file', 'directory']:
|
2013-08-16 03:39:02 +00:00
|
|
|
changed = True
|
2013-12-12 14:44:29 +00:00
|
|
|
if not force:
|
2014-03-14 03:07:35 +00:00
|
|
|
module.fail_json(dest=path, src=src, msg='Cannot link, %s exists at destination' % prev_state)
|
2012-07-31 01:50:32 +00:00
|
|
|
else:
|
2013-07-20 22:08:42 +00:00
|
|
|
module.fail_json(dest=path, src=src, msg='unexpected position reached')
|
2012-07-31 01:50:32 +00:00
|
|
|
|
2013-08-16 03:39:02 +00:00
|
|
|
if changed and not module.check_mode:
|
|
|
|
if prev_state != 'absent':
|
2014-03-14 03:07:35 +00:00
|
|
|
# try to replace atomically
|
2014-03-19 13:11:17 +00:00
|
|
|
tmppath = '/'.join([os.path.dirname(path), ".%s.%s.tmp" % (os.getpid(),time.time())])
|
2013-08-16 03:39:02 +00:00
|
|
|
try:
|
2014-03-14 03:07:35 +00:00
|
|
|
if state == 'hard':
|
|
|
|
os.link(src,tmppath)
|
|
|
|
else:
|
|
|
|
os.symlink(src, tmppath)
|
|
|
|
os.rename(tmppath, path)
|
2013-08-16 03:39:02 +00:00
|
|
|
except OSError, e:
|
2014-03-20 12:14:24 +00:00
|
|
|
if os.path.exists(tmppath):
|
|
|
|
os.unlink(tmppath)
|
2014-03-14 03:07:35 +00:00
|
|
|
module.fail_json(path=path, msg='Error while replacing: %s' % str(e))
|
2014-03-19 13:11:17 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
if state == 'hard':
|
|
|
|
os.link(src,path)
|
|
|
|
else:
|
|
|
|
os.symlink(src, path)
|
|
|
|
except OSError, e:
|
|
|
|
module.fail_json(path=path, msg='Error while linking: %s' % str(e))
|
2012-04-01 23:10:23 +00:00
|
|
|
|
2014-06-19 13:47:46 +00:00
|
|
|
if module.check_mode and not os.path.exists(path):
|
|
|
|
module.exit_json(dest=path, src=src, changed=changed)
|
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
2013-07-20 22:08:42 +00:00
|
|
|
module.exit_json(dest=path, src=src, changed=changed)
|
2012-04-01 23:10:23 +00:00
|
|
|
|
2013-09-22 14:16:07 +00:00
|
|
|
elif state == 'touch':
|
2014-03-14 03:07:35 +00:00
|
|
|
if not module.check_mode:
|
|
|
|
|
|
|
|
if prev_state == 'absent':
|
|
|
|
try:
|
|
|
|
open(path, 'w').close()
|
|
|
|
except OSError, e:
|
|
|
|
module.fail_json(path=path, msg='Error, could not touch target: %s' % str(e))
|
|
|
|
elif prev_state in ['file', 'directory']:
|
|
|
|
try:
|
|
|
|
os.utime(path, None)
|
|
|
|
except OSError, e:
|
|
|
|
module.fail_json(path=path, msg='Error while touching existing target: %s' % str(e))
|
|
|
|
else:
|
|
|
|
module.fail_json(msg='Cannot touch other than files and directories')
|
|
|
|
|
|
|
|
module.set_fs_attributes_if_different(file_args, True)
|
2013-09-22 10:53:49 +00:00
|
|
|
|
2013-09-21 13:07:50 +00:00
|
|
|
module.exit_json(dest=path, changed=True)
|
|
|
|
|
2014-03-14 03:07:35 +00:00
|
|
|
module.fail_json(path=path, msg='unexpected position reached')
|
2012-04-01 23:10:23 +00:00
|
|
|
|
2013-12-02 20:13:49 +00:00
|
|
|
# import module snippets
|
2013-12-02 20:11:23 +00:00
|
|
|
from ansible.module_utils.basic import *
|
2012-08-01 04:21:36 +00:00
|
|
|
main()
|
2012-03-16 01:53:14 +00:00
|
|
|
|