From d419a13ca75bd7b87ab79b224249c421c5af641f Mon Sep 17 00:00:00 2001 From: Jeroen Hoekx Date: Tue, 8 May 2012 09:19:55 +0200 Subject: [PATCH] Inventory: support list of vars in host. --- lib/ansible/inventory_parser_yaml.py | 7 ++++++- test/TestInventory.py | 8 ++++++++ test/yaml_hosts | 4 +++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/ansible/inventory_parser_yaml.py b/lib/ansible/inventory_parser_yaml.py index 0a8c282b01..bdf69b17c3 100644 --- a/lib/ansible/inventory_parser_yaml.py +++ b/lib/ansible/inventory_parser_yaml.py @@ -63,7 +63,12 @@ class InventoryParserYaml(object): elif type(item) == dict and 'host' in item: host = self._make_host(item['host']) - for (k,v) in item.get('vars',{}).items(): + 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: diff --git a/test/TestInventory.py b/test/TestInventory.py index 2a8930277c..fa8568028b 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -258,6 +258,14 @@ class TestInventory(unittest.TestCase): 'hammer':True, 'inventory_hostname': 'thor'} + def test_yaml_list_vars(self): + inventory = self.yaml_inventory() + vars = inventory.get_variables('zeus') + assert vars == {'ansible_ssh_port': 3001, + 'group_names': ['greek', 'ruler'], + 'inventory_hostname': 'zeus', + 'ntp_server': 'olympus.example.com'} + def test_yaml_change_vars(self): inventory = self.yaml_inventory() vars = inventory.get_variables('thor') diff --git a/test/yaml_hosts b/test/yaml_hosts index 8d98d5133e..6f9af62362 100644 --- a/test/yaml_hosts +++ b/test/yaml_hosts @@ -6,7 +6,9 @@ moon: titan moon2: enceladus -- zeus +- host: zeus + vars: + - ansible_ssh_port: 3001 - group: greek hosts: