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

Define 'inventory_hostname' variable for hosts. #294

Merged
merged 1 commit into from
May 2, 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
5 changes: 4 additions & 1 deletion lib/ansible/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def lift_restriction(self):
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())

Expand Down
27 changes: 19 additions & 8 deletions test/TestInventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ def test_simple_vars(self):
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

Expand Down Expand Up @@ -146,7 +149,9 @@ def test_script_vars(self):
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

Expand Down Expand Up @@ -204,8 +209,10 @@ def test_yaml_restrict(self):
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()
Expand All @@ -214,21 +221,25 @@ def test_yaml_change_vars(self):
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):
inventory = self.yaml_inventory()
vars = inventory.get_variables('hera')

assert vars == {'ansible_ssh_port': 3000,
'inventory_hostname': 'hera',
'ntp_server': 'olympus.example.com',
'group_names': ['greek']}

Expand Down