From 8d008d00ed55490768fac5bf1f00ba628cadd97b Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Thu, 29 Mar 2018 14:58:19 -0700 Subject: [PATCH] Py3 vargrant inv (#37803) * Fix dynamic inventory for vagrant does not work on python3 (#37631) * Fix dynamic inventory for vagrant does not work on python3 #35129 (cherry picked from commit 1b121fc9e405cc31638357cc869768aa106cf534) * Add vagrant python3 fix to the changelog --- changelogs/fragments/py3-vagrant-inv.yaml | 2 ++ contrib/inventory/vagrant.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/py3-vagrant-inv.yaml diff --git a/changelogs/fragments/py3-vagrant-inv.yaml b/changelogs/fragments/py3-vagrant-inv.yaml new file mode 100644 index 0000000000..472a8b63f3 --- /dev/null +++ b/changelogs/fragments/py3-vagrant-inv.yaml @@ -0,0 +1,2 @@ +bugfixes: +- Fix bytes/text handling in vagrant dynamic inventory https://github.com/ansible/ansible/pull/37631 diff --git a/contrib/inventory/vagrant.py b/contrib/inventory/vagrant.py index c6e0ff3b75..072340015e 100755 --- a/contrib/inventory/vagrant.py +++ b/contrib/inventory/vagrant.py @@ -38,14 +38,17 @@ import os.path import subprocess import re from paramiko import SSHConfig -from cStringIO import StringIO from optparse import OptionParser from collections import defaultdict try: import json -except: +except Exception: import simplejson as json +from ansible.module_utils._text import to_text +from ansible.module_utils.six.moves import StringIO + + _group = 'vagrant' # a default group _ssh_to_ansible = [('user', 'ansible_ssh_user'), ('hostname', 'ansible_ssh_host'), @@ -74,7 +77,8 @@ def get_ssh_config(): # list all the running boxes def list_running_boxes(): - output = subprocess.check_output(["vagrant", "status"]).split('\n') + + output = to_text(subprocess.check_output(["vagrant", "status"]), errors='surrogate_or_strict').split('\n') boxes = [] @@ -90,7 +94,7 @@ def list_running_boxes(): def get_a_ssh_config(box_name): """Gives back a map of all the machine's ssh configurations""" - output = subprocess.check_output(["vagrant", "ssh-config", box_name]) + output = to_text(subprocess.check_output(["vagrant", "ssh-config", box_name]), errors='surrogate_or_strict') config = SSHConfig() config.parse(StringIO(output)) host_config = config.lookup(box_name) @@ -104,6 +108,7 @@ def get_a_ssh_config(box_name): return dict((v, host_config[k]) for k, v in _ssh_to_ansible) + # List out servers that vagrant has running # ------------------------------ if options.list: