From 03541baba7726acd62f383b564daffee786cbe45 Mon Sep 17 00:00:00 2001 From: Jeroen Hoekx Date: Wed, 2 May 2012 09:56:58 +0200 Subject: [PATCH] Define 'inventory_hostname' variable for hosts. --- lib/ansible/inventory.py | 5 ++++- test/TestInventory.py | 27 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/ansible/inventory.py b/lib/ansible/inventory.py index ef790b6d31..a9d1aec26a 100644 --- a/lib/ansible/inventory.py +++ b/lib/ansible/inventory.py @@ -81,7 +81,10 @@ class Inventory(object): def get_variables(self, host): """ Return the variables associated with this host. """ - variables = {} + variables = { + 'inventory_hostname': host, + } + if host in self._variables: variables.update(self._variables[host].copy()) diff --git a/test/TestInventory.py b/test/TestInventory.py index c136430d18..15517fcda3 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -85,13 +85,16 @@ class TestInventory(unittest.TestCase): inventory = self.simple_inventory() vars = inventory.get_variables('thor') - assert vars == {'group_names': ['norse']} + assert vars == {'group_names': ['norse'], + 'inventory_hostname': 'thor'} def test_simple_port(self): inventory = self.simple_inventory() vars = inventory.get_variables('hera') - assert vars == {'ansible_ssh_port': 3000, 'group_names': ['greek']} + assert vars == {'ansible_ssh_port': 3000, + 'group_names': ['greek'], + 'inventory_hostname': 'hera'} ### Inventory API tests @@ -146,7 +149,9 @@ class TestInventory(unittest.TestCase): inventory = self.script_inventory() vars = inventory.get_variables('thor') - assert vars == {"hammer":True, 'group_names': ['norse']} + assert vars == {'hammer':True, + 'group_names': ['norse'], + 'inventory_hostname': 'thor'} ### Tests for yaml inventory file @@ -204,8 +209,10 @@ class TestInventory(unittest.TestCase): def test_yaml_vars(self): inventory = self.yaml_inventory() vars = inventory.get_variables('thor') - - assert vars == {"hammer":True, 'group_names': ['norse']} + print vars + assert vars == {'group_names': ['norse'], + 'hammer':True, + 'inventory_hostname': 'thor'} def test_yaml_change_vars(self): inventory = self.yaml_inventory() @@ -214,14 +221,17 @@ class TestInventory(unittest.TestCase): vars["hammer"] = False vars = inventory.get_variables('thor') - assert vars == {"hammer":True, 'group_names': ['norse']} + assert vars == {'hammer':True, + 'inventory_hostname': 'thor', + 'group_names': ['norse']} def test_yaml_host_vars(self): inventory = self.yaml_inventory() vars = inventory.get_variables('saturn') - assert vars == {"moon":"titan", - "moon2":"enceladus", + assert vars == {'inventory_hostname': 'saturn', + 'moon': 'titan', + 'moon2': 'enceladus', 'group_names': ['multiple']} def test_yaml_port(self): @@ -229,6 +239,7 @@ class TestInventory(unittest.TestCase): vars = inventory.get_variables('hera') assert vars == {'ansible_ssh_port': 3000, + 'inventory_hostname': 'hera', 'ntp_server': 'olympus.example.com', 'group_names': ['greek']}