mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 05:12:45 +00:00
Add YAML inventory format.
See test/yaml_hosts for an example. Hosts can be part of multiple groups. Groups can also have variables, inherited by the hosts. There is no variable scope, last variable seen wins.
This commit is contained in:
@@ -12,6 +12,7 @@ class TestInventory(unittest.TestCase):
|
||||
|
||||
self.inventory_file = os.path.join(self.test_dir, 'simple_hosts')
|
||||
self.inventory_script = os.path.join(self.test_dir, 'inventory_api.py')
|
||||
self.inventory_yaml = os.path.join(self.test_dir, 'yaml_hosts')
|
||||
|
||||
os.chmod(self.inventory_script, 0755)
|
||||
|
||||
@@ -26,6 +27,9 @@ class TestInventory(unittest.TestCase):
|
||||
def script_inventory(self):
|
||||
return Inventory( self.inventory_script )
|
||||
|
||||
def yaml_inventory(self):
|
||||
return Inventory( self.inventory_yaml )
|
||||
|
||||
def test_simple(self):
|
||||
inventory = self.simple_inventory()
|
||||
hosts = inventory.list_hosts()
|
||||
@@ -149,6 +153,76 @@ class TestInventory(unittest.TestCase):
|
||||
|
||||
assert vars == {"hammer":True, "simple": "yes"}
|
||||
|
||||
### Tests for yaml inventory file
|
||||
|
||||
def test_yaml(self):
|
||||
inventory = self.yaml_inventory()
|
||||
hosts = inventory.list_hosts()
|
||||
print hosts
|
||||
expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
||||
assert 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']
|
||||
assert hosts == expected_hosts
|
||||
|
||||
def test_yaml_norse(self):
|
||||
inventory = self.yaml_inventory()
|
||||
hosts = inventory.list_hosts("norse")
|
||||
|
||||
expected_hosts=['thor', 'odin', 'loki']
|
||||
assert hosts == expected_hosts
|
||||
|
||||
def test_yaml_combined(self):
|
||||
inventory = self.yaml_inventory()
|
||||
hosts = inventory.list_hosts("norse:greek")
|
||||
|
||||
expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
||||
assert hosts == expected_hosts
|
||||
|
||||
def test_yaml_restrict(self):
|
||||
inventory = self.yaml_inventory()
|
||||
|
||||
restricted_hosts = ['hera', 'poseidon', 'thor']
|
||||
expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
||||
|
||||
inventory.restrict_to(restricted_hosts)
|
||||
hosts = inventory.list_hosts("norse:greek")
|
||||
|
||||
assert hosts == restricted_hosts
|
||||
|
||||
inventory.lift_restriction()
|
||||
hosts = inventory.list_hosts("norse:greek")
|
||||
|
||||
assert hosts == expected_hosts
|
||||
|
||||
def test_yaml_vars(self):
|
||||
inventory = self.yaml_inventory()
|
||||
vars = inventory.get_variables('thor')
|
||||
|
||||
assert vars == {"hammer":True}
|
||||
|
||||
def test_yaml_host_vars(self):
|
||||
inventory = self.yaml_inventory()
|
||||
vars = inventory.get_variables('saturn')
|
||||
|
||||
assert vars == {"moon":"titan"}
|
||||
|
||||
def test_yaml_extra_vars(self):
|
||||
inventory = self.yaml_inventory()
|
||||
vars = inventory.get_variables('thor', 'a=5')
|
||||
|
||||
assert vars == {"hammer":True}
|
||||
|
||||
def test_yaml_port(self):
|
||||
inventory = self.yaml_inventory()
|
||||
vars = inventory.get_variables('hera')
|
||||
|
||||
assert vars == {'ansible_ssh_port': 3000}
|
||||
|
||||
### Test Runner class method
|
||||
|
||||
def test_class_method(self):
|
||||
|
||||
26
test/yaml_hosts
Normal file
26
test/yaml_hosts
Normal file
@@ -0,0 +1,26 @@
|
||||
---
|
||||
|
||||
- jupiter
|
||||
- host: saturn
|
||||
vars:
|
||||
- moon: titan
|
||||
|
||||
- group: greek
|
||||
hosts:
|
||||
- zeus
|
||||
- hera
|
||||
- poseidon
|
||||
vars:
|
||||
- ansible_ssh_port: 3000
|
||||
|
||||
- group: norse
|
||||
hosts:
|
||||
- host: thor
|
||||
vars:
|
||||
- hammer: True
|
||||
- odin
|
||||
- loki
|
||||
|
||||
- group: multiple
|
||||
hosts:
|
||||
- saturn
|
||||
Reference in New Issue
Block a user