27
27
import configparser
28
28
except ImportError :
29
29
import ConfigParser as configparser
30
- try :
31
- from itertools import ifilter
32
- except ImportError :
33
- from itertools import filter as ifilter
34
30
import json
31
+
35
32
import yaml
36
- import os
37
- import sys
38
33
39
34
40
35
valid = {
@@ -86,7 +81,7 @@ def main():
86
81
config .read ('ansible.cfg' )
87
82
88
83
for host_types in hosts ['hosts' ]:
89
- for host_type , providers in host_types .iteritems ():
84
+ for host_type , providers in host_types .items ():
90
85
export [host_type ] = {}
91
86
export [host_type ]['hosts' ] = []
92
87
@@ -96,8 +91,8 @@ def main():
96
91
}
97
92
98
93
for provider in providers :
99
- for provider_name , hosts in provider .iteritems ():
100
- for host , metadata in hosts .iteritems ():
94
+ for provider_name , hosts in provider .items ():
95
+ for host , metadata in hosts .items ():
101
96
102
97
# some hosts have metadata appended to provider
103
98
# which requires underscore
@@ -111,9 +106,9 @@ def main():
111
106
112
107
try :
113
108
parsed_host = parse_host (hostname )
114
- for k , v in parsed_host .iteritems ():
109
+ for k , v in parsed_host .items ():
115
110
c .update ({k : v [0 ] if type (v ) is dict else v })
116
- except Exception , e :
111
+ except Exception as e :
117
112
raise Exception ('Failed to parse host: %s' % e )
118
113
119
114
c .update ({'ansible_host' : metadata ['ip' ]})
@@ -135,13 +130,13 @@ def main():
135
130
c .update ({'vs' : metadata ['vs' ]})
136
131
137
132
# add specific options from config
138
- for option in ifilter ( lambda s : s . startswith ( 'hosts:' ),
139
- config . sections ()):
140
- # remove `hosts:`
141
- if option [ 6 :] in hostname :
142
- for o in config .items (option ):
143
- # configparser returns tuples of key, value
144
- c .update ({o [ 0 ]: o [ 1 ] })
133
+ for section in filter (
134
+ lambda s : s . startswith ( 'hosts:' ) and s [ 6 :] in hostname ,
135
+ config . sections ()
136
+ ) :
137
+ for k , v in config .items (section , raw = True ):
138
+ # configparser returns tuples of key, value
139
+ c .update ({k : v })
145
140
146
141
export ['_meta' ]['hostvars' ][hostname ] = {}
147
142
export ['_meta' ]['hostvars' ][hostname ].update (c )
@@ -175,7 +170,6 @@ def has_metadata(info):
175
170
metadata by underscore. Not used anywhere at the moment for anything
176
171
other than descriptiveness"""
177
172
178
- param = dict ()
179
173
metadata = info .split ('_' , 1 )
180
174
181
175
try :
0 commit comments