Skip to content

Commit

Permalink
ansible: make code compatible with python3 (3.6)
Browse files Browse the repository at this point in the history
and `flake8` styled
  • Loading branch information
refack committed Jul 12, 2018
1 parent c47aa6a commit 2b5e063
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
32 changes: 13 additions & 19 deletions ansible/plugins/inventory/nodejs_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@
import configparser
except ImportError:
import ConfigParser as configparser
try:
from itertools import ifilter
except ImportError:
from itertools import filter as ifilter
import json

import yaml
import os
import sys


valid = {
Expand Down Expand Up @@ -86,7 +81,7 @@ def main():
config.read('ansible.cfg')

for host_types in hosts['hosts']:
for host_type, providers in host_types.iteritems():
for host_type, providers in host_types.items():
export[host_type] = {}
export[host_type]['hosts'] = []

Expand All @@ -96,8 +91,8 @@ def main():
}

for provider in providers:
for provider_name, hosts in provider.iteritems():
for host, metadata in hosts.iteritems():
for provider_name, hosts in provider.items():
for host, metadata in hosts.items():

# some hosts have metadata appended to provider
# which requires underscore
Expand All @@ -111,9 +106,9 @@ def main():

try:
parsed_host = parse_host(hostname)
for k, v in parsed_host.iteritems():
for k, v in parsed_host.items():
c.update({k: v[0] if type(v) is dict else v})
except Exception, e:
except Exception as e:
raise Exception('Failed to parse host: %s' % e)

c.update({'ansible_host': metadata['ip']})
Expand All @@ -135,13 +130,13 @@ def main():
c.update({'vs': metadata['vs']})

# add specific options from config
for option in ifilter(lambda s: s.startswith('hosts:'),
config.sections()):
# remove `hosts:`
if option[6:] in hostname:
for o in config.items(option):
# configparser returns tuples of key, value
c.update({o[0]: o[1]})
for section in filter(
lambda s: s.startswith('hosts:') and s[6:] in hostname,
config.sections()
):
for k, v in config.items(section, raw=True):
# configparser returns tuples of key, value
c.update({k: v})

export['_meta']['hostvars'][hostname] = {}
export['_meta']['hostvars'][hostname].update(c)
Expand Down Expand Up @@ -175,7 +170,6 @@ def has_metadata(info):
metadata by underscore. Not used anywhere at the moment for anything
other than descriptiveness"""

param = dict()
metadata = info.split('_', 1)

try:
Expand Down
6 changes: 4 additions & 2 deletions ansible/plugins/library/ssh_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
# IN THE SOFTWARE.
#

from ansible.module_utils.basic import *
from jinja2 import Environment, Template, filters
import os
import re

from ansible.module_utils.basic import AnsibleModule
from jinja2 import Environment


pre_match = '# begin: node.js template'
post_match = '# end: node.js template'
match = re.compile(r'^' + re.escape(pre_match) + '(.*)' + re.escape(post_match),
Expand Down

0 comments on commit 2b5e063

Please sign in to comment.