diff --git a/plugins/inventory/terraform_state.py b/plugins/inventory/terraform_state.py index 2e81f9a6..03196f57 100644 --- a/plugins/inventory/terraform_state.py +++ b/plugins/inventory/terraform_state.py @@ -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: @@ -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) @@ -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"), ) diff --git a/tests/integration/targets/inventory_terraform_state_aws/templates/inventory_with_constructed.yml.j2 b/tests/integration/targets/inventory_terraform_state_aws/templates/inventory_with_constructed.yml.j2 index 9b4b4163..38cf941b 100644 --- a/tests/integration/targets/inventory_terraform_state_aws/templates/inventory_with_constructed.yml.j2 +++ b/tests/integration/targets/inventory_terraform_state_aws/templates/inventory_with_constructed.yml.j2 @@ -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 == "" diff --git a/tests/integration/targets/inventory_terraform_state_aws/test.yml b/tests/integration/targets/inventory_terraform_state_aws/test.yml index 8ca54827..8e3237ad 100644 --- a/tests/integration/targets/inventory_terraform_state_aws/test.yml +++ b/tests/integration/targets/inventory_terraform_state_aws/test.yml @@ -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 diff --git a/tests/unit/plugins/inventory/test_terraform_state.py b/tests/unit/plugins/inventory/test_terraform_state.py index 72101b1f..d000b0c4 100644 --- a/tests/unit/plugins/inventory/test_terraform_state.py +++ b/tests/unit/plugins/inventory/test_terraform_state.py @@ -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() @@ -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) @@ -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: @@ -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"), )