Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion plugins/inventory/terraform_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ def create_inventory(
hostnames: Optional[List[Any]],
compose: Optional[Dict[str, str]],
keyed_groups: List[Dict[str, Any]],
groups: Dict[str, Any],
strict: bool,
) -> None:
for instance in instances:
Expand All @@ -537,6 +538,9 @@ def create_inventory(
# Create groups based on variable values and add the corresponding hosts to it
self._add_host_to_keyed_groups(keyed_groups, host_vars, name, strict=strict)

# Create groups based on jinja2 conditionals
self._add_host_to_composed_groups(groups, host_vars, name, strict=strict)

def parse(self, inventory, loader, path, cache=False): # type: ignore # mypy ignore
super(InventoryModule, self).parse(inventory, loader, path, cache=cache)

Expand Down Expand Up @@ -575,5 +579,10 @@ def parse(self, inventory, loader, path, cache=False): # type: ignore # mypy i
providers,
)
self.create_inventory(
instances, cfg.get("hostnames"), cfg.get("compose"), cfg.get("keyed_groups"), cfg.get("strict")
instances,
cfg.get("hostnames"),
cfg.get("compose"),
cfg.get("keyed_groups"),
cfg.get("groups"),
cfg.get("strict"),
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ backend_config:
key: ansible/terraform.tfstate
region: {{ aws_region }}
keyed_groups:
- key: instance_state
prefix: state
- prefix: tag
key: tags
- key: instance_state
prefix: state
- prefix: tag
key: tags
groups:
no_public_ip: public_ip == ""
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
- default_hostname in groups.tag_Phase_integration
- "'state_running' in groups"
- default_hostname in groups.state_running
- "'no_public_ip' in groups"
- default_hostname in groups.no_public_ip

always:
- name: Delete temporary file
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/plugins/inventory/test_terraform_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def create_instance(self, name, values):
def test_create_inventory(self, inventory_plugin, mocker):
hostnames = MagicMock()
keyed_groups = MagicMock()
groups = MagicMock()
strict = MagicMock()
compose = MagicMock()

Expand All @@ -270,9 +271,10 @@ def test_create_inventory(self, inventory_plugin, mocker):

inventory_plugin._set_composite_vars = MagicMock()
inventory_plugin._add_host_to_keyed_groups = MagicMock()
inventory_plugin._add_host_to_composed_groups = MagicMock()
inventory_plugin.inventory = self.ansibleInventory()

inventory_plugin.create_inventory(instances, hostnames, compose, keyed_groups, strict)
inventory_plugin.create_inventory(instances, hostnames, compose, keyed_groups, groups, strict)

for name, value in config.items():
inventory_plugin.inventory.assert_value(name, value)
Expand All @@ -289,6 +291,10 @@ def test_create_inventory(self, inventory_plugin, mocker):
[call(keyed_groups, vars, name, strict=strict) for name, vars in config.items()],
any_order=True,
)
inventory_plugin._add_host_to_composed_groups.assert_has_calls(
[call(groups, vars, name, strict=strict) for name, vars in config.items()],
any_order=True,
)


class TestWriteTerraformConfig:
Expand Down Expand Up @@ -428,6 +434,7 @@ def assert_calls(self, config, super_parse_patch, read_config_data_patch):
config.get("hostnames"),
config.get("compose"),
config.get("keyed_groups"),
config.get("groups"),
config.get("strict"),
)

Expand Down