"force=" code under the file module is problematic, remove it
This commit is contained in:
parent
f3407dec0e
commit
7583704144
2 changed files with 1 additions and 85 deletions
27
library/file
27
library/file
|
@ -113,12 +113,6 @@ options:
|
||||||
description:
|
description:
|
||||||
- accepts only C(default) as value. This will restore a file's SELinux context
|
- 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.
|
in the policy. Does nothing if no default value is available.
|
||||||
force:
|
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
choices: []
|
|
||||||
description:
|
|
||||||
- force is required when changing an existing file to a directory, or a link to a directory, and so on. Use this with caution.
|
|
||||||
examples:
|
examples:
|
||||||
- code: file path=/etc/foo.conf owner=foo group=foo mode=0644
|
- code: file path=/etc/foo.conf owner=foo group=foo mode=0644
|
||||||
description: Example from Ansible Playbooks
|
description: Example from Ansible Playbooks
|
||||||
|
@ -325,7 +319,6 @@ def main():
|
||||||
state = dict(choices=['file','directory','link','absent'], default='file'),
|
state = dict(choices=['file','directory','link','absent'], default='file'),
|
||||||
path = dict(aliases=['dest', 'name'], required=True),
|
path = dict(aliases=['dest', 'name'], required=True),
|
||||||
src = dict(),
|
src = dict(),
|
||||||
force = dict(default='no', choices=['yes', 'no']),
|
|
||||||
mode = dict(),
|
mode = dict(),
|
||||||
owner = dict(),
|
owner = dict(),
|
||||||
group = dict(),
|
group = dict(),
|
||||||
|
@ -346,7 +339,6 @@ def main():
|
||||||
src = params.get('src', None)
|
src = params.get('src', None)
|
||||||
if src:
|
if src:
|
||||||
src = os.path.expanduser(src)
|
src = os.path.expanduser(src)
|
||||||
force = module.boolean(params['force'])
|
|
||||||
|
|
||||||
if src is not None and os.path.isdir(path):
|
if src is not None and os.path.isdir(path):
|
||||||
params['path'] = path = os.path.join(path, os.path.basename(src))
|
params['path'] = path = os.path.join(path, os.path.basename(src))
|
||||||
|
@ -399,7 +391,7 @@ def main():
|
||||||
module_fail_json(path=path, msg=str(e))
|
module_fail_json(path=path, msg=str(e))
|
||||||
module_exit_json(path=path, changed=True)
|
module_exit_json(path=path, changed=True)
|
||||||
|
|
||||||
if prev_state != 'absent' and prev_state != state and not force:
|
if prev_state != 'absent' and prev_state != state:
|
||||||
module_fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, src))
|
module_fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, src))
|
||||||
|
|
||||||
if prev_state == 'absent' and state == 'absent':
|
if prev_state == 'absent' and state == 'absent':
|
||||||
|
@ -423,13 +415,6 @@ def main():
|
||||||
if prev_state == 'absent':
|
if prev_state == 'absent':
|
||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
changed = True
|
changed = True
|
||||||
elif prev_state != 'directory' and force:
|
|
||||||
try:
|
|
||||||
os.unlink(path)
|
|
||||||
except Exception, e:
|
|
||||||
module_fail_json(path=path, msg=str(e))
|
|
||||||
os.makedirs(path)
|
|
||||||
changed = True
|
|
||||||
|
|
||||||
# set modes owners and context as needed
|
# set modes owners and context as needed
|
||||||
changed = set_context_if_different(path, secontext, changed)
|
changed = set_context_if_different(path, secontext, changed)
|
||||||
|
@ -451,16 +436,6 @@ def main():
|
||||||
if prev_state == 'absent':
|
if prev_state == 'absent':
|
||||||
os.symlink(src, path)
|
os.symlink(src, path)
|
||||||
changed = True
|
changed = True
|
||||||
elif prev_state != 'link' and force:
|
|
||||||
try:
|
|
||||||
if os.path.isfile(path):
|
|
||||||
os.unlink(path)
|
|
||||||
else:
|
|
||||||
shutil.rmtree(path, ignore_errors=False, onerror=rmtree_error)
|
|
||||||
except Exception, e:
|
|
||||||
module_fail_json(path=path, msg=str(e))
|
|
||||||
os.symlink(src, path)
|
|
||||||
changed = True
|
|
||||||
elif prev_state == 'link':
|
elif prev_state == 'link':
|
||||||
old_src = os.readlink(path)
|
old_src = os.readlink(path)
|
||||||
if not os.path.isabs(old_src):
|
if not os.path.isabs(old_src):
|
||||||
|
|
|
@ -224,65 +224,6 @@ class TestRunner(unittest.TestCase):
|
||||||
assert not self._run('file', ['dest=' + filedemo, 'state=absent'])['changed']
|
assert not self._run('file', ['dest=' + filedemo, 'state=absent'])['changed']
|
||||||
os.rmdir(tmp_dir)
|
os.rmdir(tmp_dir)
|
||||||
|
|
||||||
|
|
||||||
filedemo = tempfile.mkstemp()[1]
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'state=directory', 'force=yes'])['changed']
|
|
||||||
assert os.path.isdir(filedemo)
|
|
||||||
os.rmdir(filedemo)
|
|
||||||
|
|
||||||
filedemo = tempfile.mkstemp()[1]
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'src=/dev/null', 'state=link', 'force=yes'])['changed']
|
|
||||||
assert os.path.islink(filedemo)
|
|
||||||
os.unlink(filedemo)
|
|
||||||
|
|
||||||
filedemo = tempfile.mkstemp()[1]
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'mode=604', 'state=file', 'force=yes'])['changed']
|
|
||||||
assert os.path.isfile(filedemo) and os.stat(filedemo).st_mode == 0100604
|
|
||||||
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'state=absent', 'force=yes'])['changed']
|
|
||||||
assert not os.path.exists(filedemo)
|
|
||||||
assert not self._run('file', ['dest=' + filedemo, 'state=absent', 'force=yes'])['changed']
|
|
||||||
|
|
||||||
|
|
||||||
filedemo = tempfile.mkdtemp()
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'state=file', 'force=yes'])['failed']
|
|
||||||
assert os.path.isdir(filedemo)
|
|
||||||
|
|
||||||
result = self._run('file', ['dest=' + filedemo, 'src=/dev/null', 'state=link', 'force=yes'])
|
|
||||||
assert result['changed']
|
|
||||||
print result
|
|
||||||
assert os.path.islink(filedemo)
|
|
||||||
os.unlink(filedemo)
|
|
||||||
|
|
||||||
filedemo = tempfile.mkdtemp()
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'mode=701', 'state=directory', 'force=yes'])['changed']
|
|
||||||
assert os.path.isdir(filedemo) and os.stat(filedemo).st_mode == 040701
|
|
||||||
os.path.isdir(filedemo)
|
|
||||||
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'state=absent', 'force=yes'])['changed']
|
|
||||||
assert not os.path.exists(filedemo)
|
|
||||||
assert not self._run('file', ['dest=' + filedemo, 'state=absent', 'force=yes'])['changed']
|
|
||||||
|
|
||||||
|
|
||||||
tmp_dir = tempfile.mkdtemp()
|
|
||||||
filedemo = os.path.join(tmp_dir, 'link')
|
|
||||||
os.symlink('/dev/zero', filedemo)
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'state=file', 'force=yes'])['failed']
|
|
||||||
assert os.path.islink(filedemo)
|
|
||||||
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'state=directory', 'force=yes'])['changed']
|
|
||||||
assert os.path.isdir(filedemo)
|
|
||||||
os.rmdir(filedemo)
|
|
||||||
|
|
||||||
os.symlink('/dev/zero', filedemo)
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'src=/dev/null', 'state=link', 'force=yes'])['changed']
|
|
||||||
assert os.path.islink(filedemo) and os.path.realpath(filedemo) == '/dev/null'
|
|
||||||
|
|
||||||
assert self._run('file', ['dest=' + filedemo, 'state=absent', 'force=yes'])['changed']
|
|
||||||
assert not os.path.exists(filedemo)
|
|
||||||
assert not self._run('file', ['dest=' + filedemo, 'state=absent', 'force=yes'])['changed']
|
|
||||||
os.rmdir(tmp_dir)
|
|
||||||
|
|
||||||
def test_large_output(self):
|
def test_large_output(self):
|
||||||
large_path = "/usr/share/dict/words"
|
large_path = "/usr/share/dict/words"
|
||||||
if not os.path.exists(large_path):
|
if not os.path.exists(large_path):
|
||||||
|
|
Loading…
Reference in a new issue