Skip to content

Commit

Permalink
[stable-2.9] Normalize ConfigParser between Python2 and Python3 (ansi…
Browse files Browse the repository at this point in the history
…ble#73715)

* Normalize config parser between py2 and py3

* Add tests and changelog

* Use different config entry, since we supply certain env vars.
(cherry picked from commit 950ab74)

Co-authored-by: Matt Martz <matt@sivel.net>
  • Loading branch information
sivel committed Feb 25, 2021
1 parent 733704f commit 27db203
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/73709-normalize-configparser.yml
@@ -0,0 +1,3 @@
bugfixes:
- ConfigManager - Normalize ConfigParser between Python2 and Python3 to for handling comments
(https://github.com/ansible/ansible/issues/73709)
5 changes: 4 additions & 1 deletion lib/ansible/config/manager.py
Expand Up @@ -316,7 +316,10 @@ def _parse_config_file(self, cfile=None):
ftype = get_config_type(cfile)
if cfile is not None:
if ftype == 'ini':
self._parsers[cfile] = configparser.ConfigParser()
kwargs = {}
if PY3:
kwargs['inline_comment_prefixes'] = (';',)
self._parsers[cfile] = configparser.ConfigParser(**kwargs)
with open(to_bytes(cfile), 'rb') as f:
try:
cfg_text = to_text(f.read(), errors='surrogate_or_strict')
Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/config/inline_comment_ansible.cfg
@@ -0,0 +1,2 @@
[defaults]
cowsay_enabled_stencils = ansibull ; BOOM
11 changes: 7 additions & 4 deletions test/integration/targets/config/runme.sh
Expand Up @@ -9,7 +9,10 @@ ANSIBLE_TIMEOUT= ansible -m ping testhost -i ../../inventory "$@"
# env var is wrong type, this should be a fatal error pointing at the setting
ANSIBLE_TIMEOUT='lola' ansible -m ping testhost -i ../../inventory "$@" 2>&1|grep 'Invalid type for configuration option setting: DEFAULT_TIMEOUT'

# https://github.com/ansible/ansible/issues/69577
ANSIBLE_REMOTE_TMP="$HOME/.ansible/directory_with_no_space" ansible -m ping testhost -i ../../inventory "$@"

ANSIBLE_REMOTE_TMP="$HOME/.ansible/directory with space" ansible -m ping testhost -i ../../inventory "$@"
# https://github.com/ansible/ansible/issues/69577
ANSIBLE_REMOTE_TMP="$HOME/.ansible/directory_with_no_space" ansible -m ping testhost -i ../../inventory "$@"

ANSIBLE_REMOTE_TMP="$HOME/.ansible/directory with space" ansible -m ping testhost -i ../../inventory "$@"

# https://github.com/ansible/ansible/pull/73715
ANSIBLE_CONFIG=inline_comment_ansible.cfg ansible-config dump --only-changed | grep "'ansibull'"

0 comments on commit 27db203

Please sign in to comment.