summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorToshio くらとみ <toshio@batcave01.phx2.fedoraproject.org>2015-12-08 16:37:10 +0000
committerToshio くらとみ <toshio@batcave01.phx2.fedoraproject.org>2015-12-08 16:37:10 +0000
commita5d4e048f0e7b5a2cf852ae2cc14fcf9f5935742 (patch)
tree267736401f16e745236935efd1818443ec0c0a3b /scripts
parenta19946ccba80256af83b023847b4c5a70b58fedf (diff)
downloadansible-a5d4e048f0e7b5a2cf852ae2cc14fcf9f5935742.tar.gz
ansible-a5d4e048f0e7b5a2cf852ae2cc14fcf9f5935742.tar.xz
ansible-a5d4e048f0e7b5a2cf852ae2cc14fcf9f5935742.zip
Port to work with both ansible 1.x and 2.x
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/ansible-vars55
1 files changed, 46 insertions, 9 deletions
diff --git a/scripts/ansible-vars b/scripts/ansible-vars
index 53ba48e02..617121f22 100755
--- a/scripts/ansible-vars
+++ b/scripts/ansible-vars
@@ -21,12 +21,37 @@
########################################################
import sys
+from distutils.version import LooseVersion
+
import yaml
import ansible.constants as C
-from ansible import utils
from ansible import errors
from ansible import inventory
+from ansible import __version__
+
+try:
+ # ansible 2.0
+ from ansible.cli import SortedOptParser
+except ImportError:
+ # ansible 1.x
+ from ansible.utils import SortedOptParser
+
+try:
+ # ansible 2.0
+ from ansible.parsing.utils.jsonify import jsonify
+ from ansible.parsing.yaml.dumper import AnsibleDumper as Dumper
+except ImportError:
+ # ansible 1.x
+ from ansible.utils import jsonify
+ from yaml import SafeDumper as Dumper
+
+ANSIBLE2 = False
+if LooseVersion(__version__) >= LooseVersion('2'):
+ from ansible.vars import VariableManager
+ from ansible.parsing.dataloader import DataLoader
+ ANSIBLE2 = True
+
########################################################
@@ -51,7 +76,7 @@ class Cli(object):
def parse(self):
''' create an options parser for bin/ansible '''
- parser = utils.SortedOptParser(
+ parser = SortedOptParser(
usage='%prog <host-pattern> [options]'
)
parser.add_option('-i', '--inventory-file', dest='inventory',
@@ -81,8 +106,15 @@ class Cli(object):
def run(self, options, args):
pattern = args[0]
-
- I = inventory.Inventory(options.inventory)
+
+ if ANSIBLE2:
+ loader = DataLoader()
+ variable_manager = VariableManager()
+ I = inventory.Inventory(loader=loader,
+ variable_manager=variable_manager,
+ host_list=options.inventory)
+ else:
+ I = inventory.Inventory(options.inventory)
if options.subset:
I.subset(options.subset)
hosts = I.list_hosts(pattern)
@@ -95,7 +127,7 @@ class Cli(object):
for host in hosts:
print '%s' % host
sys.exit(0)
-
+
if options.listgroups:
group_subset = []
for host in hosts:
@@ -109,8 +141,13 @@ class Cli(object):
results = { }
for host in hosts:
- vars = I.get_variables(host)
- results.update({host: vars})
+ if ANSIBLE2:
+ name = host.get_name()
+ vars = variable_manager.get_vars(loader, host=host)['vars']
+ else:
+ name = host
+ vars = I.get_variables(host)
+ results.update({name: vars})
return results
@@ -126,7 +163,7 @@ if __name__ == '__main__':
print "ERROR: %s" % str(e)
sys.exit(1)
if options.json and not options.yaml:
- print utils.jsonify(results, format=True)
+ print jsonify(results, format=True)
else:
- print yaml.safe_dump(results)
+ print yaml.dump(results, Dumper=Dumper, allow_unicode=True)