mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Only print warning when ansible.cfg is actually skipped (#43583)
Only print warning when ansible.cfg is actually skipped * Also add unittests for the find_ini_config_file function * Add documentation on world writable current working directory config files can no longer be loaded from a world writable current working directory but the end user is allowed to specify that explicitly. Give appropriate warnings and information on how. Fixes #42388
This commit is contained in:
parent
48280463f2
commit
30662bedad
6 changed files with 314 additions and 26 deletions
|
@ -6,6 +6,7 @@ __metaclass__ = type
|
|||
|
||||
import io
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import stat
|
||||
import tempfile
|
||||
|
@ -152,31 +153,59 @@ def find_ini_config_file(warnings=None):
|
|||
''' Load INI Config File order(first found is used): ENV, CWD, HOME, /etc/ansible '''
|
||||
# FIXME: eventually deprecate ini configs
|
||||
|
||||
path0 = os.getenv("ANSIBLE_CONFIG", None)
|
||||
if path0 is not None:
|
||||
path0 = unfrackpath(path0, follow=False)
|
||||
if os.path.isdir(path0):
|
||||
path0 += "/ansible.cfg"
|
||||
try:
|
||||
path1 = os.getcwd()
|
||||
perms1 = os.stat(path1)
|
||||
if perms1.st_mode & stat.S_IWOTH:
|
||||
if warnings is not None:
|
||||
warnings.add("Ansible is in a world writable directory (%s), ignoring it as an ansible.cfg source." % to_text(path1))
|
||||
path1 = None
|
||||
else:
|
||||
path1 += "/ansible.cfg"
|
||||
except OSError:
|
||||
path1 = None
|
||||
path2 = unfrackpath("~/.ansible.cfg", follow=False)
|
||||
path3 = "/etc/ansible/ansible.cfg"
|
||||
if warnings is None:
|
||||
# Note: In this case, warnings does nothing
|
||||
warnings = set()
|
||||
|
||||
for path in [path0, path1, path2, path3]:
|
||||
if path is not None and os.path.exists(path):
|
||||
# A value that can never be a valid path so that we can tell if ANSIBLE_CONFIG was set later
|
||||
# We can't use None because we could set path to None.
|
||||
SENTINEL = object
|
||||
|
||||
potential_paths = []
|
||||
|
||||
# Environment setting
|
||||
path_from_env = os.getenv("ANSIBLE_CONFIG", SENTINEL)
|
||||
if path_from_env is not SENTINEL:
|
||||
path_from_env = unfrackpath(path_from_env, follow=False)
|
||||
if os.path.isdir(path_from_env):
|
||||
path_from_env = os.path.join(path_from_env, "ansible.cfg")
|
||||
potential_paths.append(path_from_env)
|
||||
|
||||
# Current working directory
|
||||
warn_cmd_public = False
|
||||
try:
|
||||
cwd = os.getcwd()
|
||||
perms = os.stat(cwd)
|
||||
if perms.st_mode & stat.S_IWOTH:
|
||||
warn_cmd_public = True
|
||||
else:
|
||||
potential_paths.append(os.path.join(cwd, "ansible.cfg"))
|
||||
except OSError:
|
||||
# If we can't access cwd, we'll simply skip it as a possible config source
|
||||
pass
|
||||
|
||||
# Per user location
|
||||
potential_paths.append(unfrackpath("~/.ansible.cfg", follow=False))
|
||||
|
||||
# System location
|
||||
potential_paths.append("/etc/ansible/ansible.cfg")
|
||||
|
||||
for path in potential_paths:
|
||||
if os.path.exists(path):
|
||||
break
|
||||
else:
|
||||
path = None
|
||||
|
||||
# Emit a warning if all the following are true:
|
||||
# * We did not use a config from ANSIBLE_CONFIG
|
||||
# * There's an ansible.cfg in the current working directory that we skipped
|
||||
if path_from_env != path and warn_cmd_public:
|
||||
warnings.add(u"Ansible is being run in a world writable directory (%s),"
|
||||
u" ignoring it as an ansible.cfg source."
|
||||
u" For more information see"
|
||||
u" https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir"
|
||||
% to_text(cwd))
|
||||
|
||||
return path
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue