mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
e7226e104e
The file module would in some cases return dest=/path/to/file, and in other cases return path=/path/to/file, this change makes it more consistent and in line with all the other file-related modules (i.e. copy, assemble, template, ...) by using dest= instead of path= as the input and output argument. (Of course, path is still there as an alias for compatibility).
330 lines
11 KiB
Python
330 lines
11 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (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
|
|
try:
|
|
import selinux
|
|
HAVE_SELINUX=True
|
|
except ImportError:
|
|
HAVE_SELINUX=False
|
|
|
|
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 M(file) module - including M(copy), M(template), and M(assemble).
|
|
options:
|
|
dest:
|
|
description:
|
|
- defines the file being managed, unless when used with C(state=link), and then sets the destination to create a symbolic link to using I(src)
|
|
required: true
|
|
default: []
|
|
aliases: ['name', 'path']
|
|
state:
|
|
description:
|
|
- 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. Use C(hard)
|
|
for hardlinks. If C(absent), directories will be recursively deleted,
|
|
and files or symlinks will be unlinked.
|
|
required: false
|
|
default: file
|
|
choices: [ file, link, directory, hard, absent ]
|
|
mode:
|
|
required: false
|
|
default: null
|
|
choices: []
|
|
description:
|
|
- mode the file or directory should be, such as 0644 as would be fed to I(chmod)
|
|
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
|
|
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" ]
|
|
description:
|
|
- 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.
|
|
recurse:
|
|
required: false
|
|
default: "no"
|
|
choices: [ "yes", "no" ]
|
|
version_added: "1.1"
|
|
description:
|
|
- recursively set the specified file attributes (applies only to state=directory)
|
|
force:
|
|
required: false
|
|
default: "no"
|
|
choices: [ "yes", "no" ]
|
|
description:
|
|
- 'force the creation of the symlinks in two cases: the source file does
|
|
not exist (but will appear later); the destination exists and a file (so, we need to unlink the
|
|
"dest" file and create symlink to the "src" file in place of it).'
|
|
notes:
|
|
- See also M(copy), M(template), M(assemble)
|
|
requirements: [ ]
|
|
author: Michael DeHaan
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
- file: dest=/etc/foo.conf owner=foo group=foo mode=0644
|
|
- file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link
|
|
'''
|
|
|
|
def dolink(src, dest, state, module):
|
|
try:
|
|
if state == 'hard':
|
|
os.link(src,dest)
|
|
else:
|
|
os.symlink(src, dest)
|
|
except OSError, e:
|
|
module.fail_json(dest=dest, msg='Error while linking: %s' % str(e))
|
|
|
|
def main():
|
|
|
|
# FIXME: pass this around, should not use global
|
|
global module
|
|
|
|
module = AnsibleModule(
|
|
argument_spec = dict(
|
|
state = dict(choices=['file','directory','link','hard','absent'], default='file'),
|
|
dest = dict(aliases=['name', 'path'], required=True),
|
|
recurse = dict(default='no', type='bool'),
|
|
force = dict(required=False,default=False,type='bool'),
|
|
diff_peek = dict(default=None),
|
|
validate = dict(required=False, default=None),
|
|
),
|
|
add_file_common_args=True,
|
|
supports_check_mode=True
|
|
)
|
|
|
|
params = module.params
|
|
state = params['state']
|
|
force = params['force']
|
|
params['dest'] = dest = os.path.expanduser(params['dest'])
|
|
|
|
# short-circuit for diff_peek
|
|
if params.get('diff_peek', None) is not None:
|
|
appears_binary = False
|
|
try:
|
|
f = open(dest)
|
|
b = f.read(8192)
|
|
f.close()
|
|
if b.find("\x00") != -1:
|
|
appears_binary = True
|
|
except:
|
|
pass
|
|
module.exit_json(dest=dest, changed=False, appears_binary=appears_binary)
|
|
|
|
# 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
|
|
|
|
src = params.get('src', None)
|
|
if src:
|
|
src = os.path.expanduser(src)
|
|
|
|
if src is not None and os.path.isdir(dest) and state != "link":
|
|
params['dest'] = dest = os.path.join(dest, os.path.basename(src))
|
|
|
|
file_args = module.load_file_common_arguments(params)
|
|
|
|
if state in ['link','hard'] and (src is None or dest is None):
|
|
module.fail_json(msg='src and dest are required for creating links')
|
|
elif dest is None:
|
|
module.fail_json(msg='path is required')
|
|
|
|
changed = False
|
|
|
|
prev_state = 'absent'
|
|
|
|
if os.path.lexists(dest):
|
|
if os.path.islink(dest):
|
|
prev_state = 'link'
|
|
elif os.path.isdir(dest):
|
|
prev_state = 'directory'
|
|
else:
|
|
prev_state = 'file'
|
|
|
|
if prev_state != 'absent' and state == 'absent':
|
|
try:
|
|
if prev_state == 'directory':
|
|
if os.path.islink(dest):
|
|
if module.check_mode:
|
|
module.exit_json(changed=True)
|
|
os.unlink(dest)
|
|
else:
|
|
try:
|
|
if module.check_mode:
|
|
module.exit_json(changed=True)
|
|
shutil.rmtree(dest, ignore_errors=False)
|
|
except:
|
|
module.exit_json(msg="rmtree failed")
|
|
else:
|
|
if module.check_mode:
|
|
module.exit_json(changed=True)
|
|
os.unlink(dest)
|
|
except Exception, e:
|
|
module.fail_json(dest=dest, msg=str(e))
|
|
module.exit_json(dest=dest, changed=True)
|
|
|
|
if prev_state != 'absent' and prev_state != state:
|
|
if force and prev_state == 'file' and state == 'link':
|
|
pass
|
|
else:
|
|
module.fail_json(dest=dest, msg='refusing to convert between %s and %s for %s' % (prev_state, state, src))
|
|
|
|
if prev_state == 'absent' and state == 'absent':
|
|
module.exit_json(dest=dest, changed=False)
|
|
|
|
if state == 'file':
|
|
|
|
if prev_state != 'file':
|
|
module.fail_json(dest=dest, msg='file (%s) does not exist, use copy or template module to create' % dest)
|
|
|
|
changed = module.set_file_attributes_if_different(file_args, changed)
|
|
module.exit_json(dest=dest, changed=changed)
|
|
|
|
elif state == 'directory':
|
|
if prev_state == 'absent':
|
|
if module.check_mode:
|
|
module.exit_json(changed=True)
|
|
os.makedirs(dest)
|
|
changed = True
|
|
|
|
changed = module.set_directory_attributes_if_different(file_args, changed)
|
|
recurse = params['recurse']
|
|
if recurse:
|
|
for root,dirs,files in os.walk( file_args['dest'] ):
|
|
for dir in dirs:
|
|
dirname=os.path.join(root,dir)
|
|
tmp_file_args = file_args.copy()
|
|
tmp_file_args['dest']=dirname
|
|
changed = module.set_directory_attributes_if_different(tmp_file_args, changed)
|
|
for file in files:
|
|
filename=os.path.join(root,file)
|
|
tmp_file_args = file_args.copy()
|
|
tmp_file_args['dest']=filename
|
|
changed = module.set_file_attributes_if_different(tmp_file_args, changed)
|
|
module.exit_json(dest=dest, changed=changed)
|
|
|
|
elif state in ['link','hard']:
|
|
|
|
if os.path.isabs(src):
|
|
abs_src = src
|
|
else:
|
|
module.fail_json(msg="absolute paths are required")
|
|
|
|
if not os.path.exists(abs_src) and not force:
|
|
module.fail_json(dest=dest, src=src, msg='src file does not exist')
|
|
|
|
if prev_state == 'absent':
|
|
if module.check_mode:
|
|
module.exit_json(changed=True)
|
|
dolink(src, dest, state, module)
|
|
changed = True
|
|
elif prev_state == 'link':
|
|
old_src = os.readlink(dest)
|
|
if not os.path.isabs(old_src):
|
|
old_src = os.path.join(os.path.dirname(dest), old_src)
|
|
if old_src != src:
|
|
if module.check_mode:
|
|
module.exit_json(changed=True)
|
|
os.unlink(dest)
|
|
dolink(src, dest, state, module)
|
|
changed = True
|
|
elif prev_state == 'file':
|
|
if not force:
|
|
module.fail_json(dest=dest, src=src, msg='Cannot link, file exists at destination')
|
|
else:
|
|
if module.check_mode:
|
|
module.exit_json(changed=True)
|
|
os.unlink(dest)
|
|
dolink(src, dest, state, module)
|
|
changed = True
|
|
else:
|
|
module.fail_json(dest=dest, src=src, msg='unexpected position reached')
|
|
|
|
# set modes owners and context as needed
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
changed = module.set_context_if_different(dest, file_args['secontext'], changed)
|
|
changed = module.set_owner_if_different(dest, file_args['owner'], changed)
|
|
changed = module.set_group_if_different(dest, file_args['group'], changed)
|
|
changed = module.set_mode_if_different(dest, file_args['mode'], changed)
|
|
|
|
module.exit_json(dest=dest, src=src, changed=changed)
|
|
|
|
module.fail_json(dest=dest, msg='unexpected position reached')
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
main()
|
|
|