1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

"force=" code under the file module is problematic, remove it

This commit is contained in:
Michael DeHaan 2012-10-16 18:14:42 -04:00
parent f3407dec0e
commit 7583704144
2 changed files with 1 additions and 85 deletions

View file

@ -113,12 +113,6 @@ options:
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.
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:
- code: file path=/etc/foo.conf owner=foo group=foo mode=0644
description: Example from Ansible Playbooks
@ -325,7 +319,6 @@ def main():
state = dict(choices=['file','directory','link','absent'], default='file'),
path = dict(aliases=['dest', 'name'], required=True),
src = dict(),
force = dict(default='no', choices=['yes', 'no']),
mode = dict(),
owner = dict(),
group = dict(),
@ -346,7 +339,6 @@ def main():
src = params.get('src', None)
if src:
src = os.path.expanduser(src)
force = module.boolean(params['force'])
if src is not None and os.path.isdir(path):
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_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))
if prev_state == 'absent' and state == 'absent':
@ -423,13 +415,6 @@ def main():
if prev_state == 'absent':
os.makedirs(path)
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
changed = set_context_if_different(path, secontext, changed)
@ -451,16 +436,6 @@ def main():
if prev_state == 'absent':
os.symlink(src, path)
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':
old_src = os.readlink(path)
if not os.path.isabs(old_src):

View file

@ -224,65 +224,6 @@ class TestRunner(unittest.TestCase):
assert not self._run('file', ['dest=' + filedemo, 'state=absent'])['changed']
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):
large_path = "/usr/share/dict/words"
if not os.path.exists(large_path):