Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly add ungrouped hosts to 'ungrouped' in YAML inventory. #512

Merged
merged 1 commit into from
Jun 27, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 25 additions & 16 deletions lib/ansible/inventory/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,21 @@ def _parse(self, data):
all.add_child_group(ungrouped)

self.groups = dict(all=all, ungrouped=ungrouped)
grouped_hosts = []

yaml = utils.parse_yaml(data)
for item in yaml:

if type(item) in [ str, unicode ]:
host = self._make_host(item)
ungrouped.add_host(host)

elif type(item) == dict and 'host' in item:
host = self._make_host(item['host'])
vars = item.get('vars', {})
if type(vars)==list:
varlist, vars = vars, {}
for subitem in varlist:
vars.update(subitem)
for (k,v) in vars.items():
host.set_variable(k,v)

elif type(item) == dict and 'group' in item:
# first add all groups
for item in yaml:
if type(item) == dict and 'group' in item:
group = Group(item['group'])

for subresult in item.get('hosts',[]):

if type(subresult) in [ str, unicode ]:
host = self._make_host(subresult)
group.add_host(host)
grouped_hosts.append(host)
elif type(subresult) == dict:
host = self._make_host(subresult['host'])
vars = subresult.get('vars',{})
Expand All @@ -92,6 +81,7 @@ def _parse(self, data):
else:
raise errors.AnsibleError("unexpected type for variable")
group.add_host(host)
grouped_hosts.append(host)

vars = item.get('vars',{})
if type(vars) == dict:
Expand All @@ -106,3 +96,22 @@ def _parse(self, data):

self.groups[group.name] = group
all.add_child_group(group)

# add host definitions
for item in yaml:
if type(item) in [ str, unicode ]:
host = self._make_host(item)
if host not in grouped_hosts:
ungrouped.add_host(host)

elif type(item) == dict and 'host' in item:
host = self._make_host(item['host'])
vars = item.get('vars', {})
if type(vars)==list:
varlist, vars = vars, {}
for subitem in varlist:
vars.update(subitem)
for (k,v) in vars.items():
host.set_variable(k,v)
if host not in grouped_hosts:
ungrouped.add_host(host)
6 changes: 3 additions & 3 deletions test/TestInventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ def test_yaml(self):
inventory = self.yaml_inventory()
hosts = inventory.list_hosts()
print hosts
expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
expected_hosts=['jupiter', 'saturn', 'mars', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
self.compare(hosts, expected_hosts)

def test_yaml_all(self):
inventory = self.yaml_inventory()
hosts = inventory.list_hosts('all')

expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
expected_hosts=['jupiter', 'saturn', 'mars', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
self.compare(hosts, expected_hosts)

def test_yaml_norse(self):
Expand All @@ -243,7 +243,7 @@ def test_yaml_ungrouped(self):
inventory = self.yaml_inventory()
hosts = inventory.list_hosts("ungrouped")

expected_hosts=['jupiter']
expected_hosts=['jupiter', 'mars']
self.compare(hosts, expected_hosts)

def test_yaml_combined(self):
Expand Down
2 changes: 2 additions & 0 deletions test/yaml_hosts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
moon: titan
moon2: enceladus

- host: mars

- host: zeus
vars:
- ansible_ssh_port: 3001
Expand Down