Skip to content

Commit

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

* [stable-2.10] Normalize ConfigParser between Python2 and Python3 (#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)

* Update config entry
  • Loading branch information
sivel committed Mar 8, 2021
1 parent ebe0ed9 commit 65037d4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
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 @@ -325,7 +325,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]
cow_whitelist = ansibull ; BOOM
3 changes: 3 additions & 0 deletions test/integration/targets/config/runme.sh
Expand Up @@ -15,3 +15,6 @@ ANSIBLE_REMOTE_TMP="$HOME/.ansible/directory_with_no_space" ansible -m ping tes
ANSIBLE_REMOTE_TMP="$HOME/.ansible/directory with space" ansible -m ping testhost -i ../../inventory "$@"

ANSIBLE_CONFIG=nonexistent.cfg ansible-config dump --only-changed -v | grep 'No config file found; using defaults'

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

0 comments on commit 65037d4

Please sign in to comment.