From c51a4bc57dee629793847f726f1138083adf3275 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 30 Aug 2017 10:53:40 -0400 Subject: [PATCH] ansible-config view fixes --- lib/ansible/cli/config.py | 28 ++++++++++---------- lib/ansible/config/manager.py | 48 +++++++++++++++++------------------ 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/lib/ansible/cli/config.py b/lib/ansible/cli/config.py index e8b5d01980..7cc7716b90 100644 --- a/lib/ansible/cli/config.py +++ b/lib/ansible/cli/config.py @@ -26,7 +26,7 @@ import sys import yaml from ansible.cli import CLI -from ansible.config.manager import ConfigManager, Setting +from ansible.config.manager import ConfigManager, Setting, find_ini_config_file from ansible.errors import AnsibleError, AnsibleOptionsError from ansible.module_utils._text import to_native, to_text, to_bytes from ansible.parsing.yaml.dumper import AnsibleDumper @@ -86,23 +86,23 @@ class ConfigCLI(CLI): super(ConfigCLI, self).run() if self.options.config_file: - self.config_file = unfrackpath(self.options.config_file, follow=False) + self.config_file = to_bytes(unfrackpath(self.options.config_file, follow=False)) self.config = ConfigManager(self.config_file) else: self.config = ConfigManager() - self.config_file = to_bytes(self.config.data.get_setting('ANSIBLE_CONFIG')) - try: - if not os.path.exists(self.config_file): - raise AnsibleOptionsError("%s does not exist or is not accessible" % (self.config_file)) - elif not os.path.isfile(self.config_file): - raise AnsibleOptionsError("%s is not a valid file" % (self.config_file)) + self.config_file = to_bytes(find_ini_config_file()) + try: + if not os.path.exists(self.config_file): + raise AnsibleOptionsError("%s does not exist or is not accessible" % (self.config_file)) + elif not os.path.isfile(self.config_file): + raise AnsibleOptionsError("%s is not a valid file" % (self.config_file)) - os.environ['ANSIBLE_CONFIG'] = self.config_file - except: - if self.action in ['view']: - raise - elif self.action in ['edit', 'update']: - display.warning("File does not exist, used empty file: %s" % self.config_file) + os.environ['ANSIBLE_CONFIG'] = to_native(self.config_file) + except: + if self.action in ['view']: + raise + elif self.action in ['edit', 'update']: + display.warning("File does not exist, used empty file: %s" % self.config_file) self.execute() diff --git a/lib/ansible/config/manager.py b/lib/ansible/config/manager.py index de3f5a52ee..11f33b400c 100644 --- a/lib/ansible/config/manager.py +++ b/lib/ansible/config/manager.py @@ -118,6 +118,29 @@ def get_ini_config_value(p, entry): pass return value +def find_ini_config_file(): + ''' 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() + "/ansible.cfg" + except OSError: + path1 = None + path2 = unfrackpath("~/.ansible.cfg", follow=False) + path3 = "/etc/ansible/ansible.cfg" + + for path in [path0, path1, path2, path3]: + if path is not None and os.path.exists(path): + break + else: + path = None + + return path class ConfigManager(object): @@ -145,7 +168,7 @@ class ConfigManager(object): if self._config_file is None: # set config using ini - self._config_file = self._find_ini_config_file() + self._config_file = find_ini_config_file() if self._config_file: if os.path.exists(self._config_file): @@ -182,29 +205,6 @@ class ConfigManager(object): ''' Load YAML Config Files in order, check merge flags, keep origin of settings''' pass - def _find_ini_config_file(self): - ''' 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() + "/ansible.cfg" - except OSError: - path1 = None - path2 = unfrackpath("~/.ansible.cfg", follow=False) - path3 = "/etc/ansible/ansible.cfg" - - for path in [path0, path1, path2, path3]: - if path is not None and os.path.exists(path): - break - else: - path = None - - return path def get_configuration_definitions(self, plugin_type=None, name=None): ''' just list the possible settings, either base or for specific plugins or plugin '''