Skip to content

Commit 011fbdc

Browse files
Inventory terraform_state - supports groups (#121) (#133)
[Manual backport stable-2] Patchback/backports/stable 2/da9b61ea4eb30c804ec97ce0f3af91cca6a3456b/pr 121 improve: supports groups for inventory add tests (cherry picked from commit da9b61e) SUMMARY ISSUE TYPE Bugfix Pull Request Docs Pull Request Feature Pull Request New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Mike Graves <[email protected]>
1 parent d0c4e6a commit 011fbdc

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

plugins/inventory/terraform_state.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ def create_inventory(
518518
hostnames: Optional[List[Any]],
519519
compose: Optional[Dict[str, str]],
520520
keyed_groups: List[Dict[str, Any]],
521+
groups: Dict[str, Any],
521522
strict: bool,
522523
) -> None:
523524
for instance in instances:
@@ -537,6 +538,9 @@ def create_inventory(
537538
# Create groups based on variable values and add the corresponding hosts to it
538539
self._add_host_to_keyed_groups(keyed_groups, host_vars, name, strict=strict)
539540

541+
# Create groups based on jinja2 conditionals
542+
self._add_host_to_composed_groups(groups, host_vars, name, strict=strict)
543+
540544
def parse(self, inventory, loader, path, cache=False): # type: ignore # mypy ignore
541545
super(InventoryModule, self).parse(inventory, loader, path, cache=cache)
542546

@@ -575,5 +579,10 @@ def parse(self, inventory, loader, path, cache=False): # type: ignore # mypy i
575579
providers,
576580
)
577581
self.create_inventory(
578-
instances, cfg.get("hostnames"), cfg.get("compose"), cfg.get("keyed_groups"), cfg.get("strict")
582+
instances,
583+
cfg.get("hostnames"),
584+
cfg.get("compose"),
585+
cfg.get("keyed_groups"),
586+
cfg.get("groups"),
587+
cfg.get("strict"),
579588
)

tests/integration/targets/inventory_terraform_state_aws/templates/inventory_with_constructed.yml.j2

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ backend_config:
66
key: ansible/terraform.tfstate
77
region: {{ aws_region }}
88
keyed_groups:
9-
- key: instance_state
10-
prefix: state
11-
- prefix: tag
12-
key: tags
9+
- key: instance_state
10+
prefix: state
11+
- prefix: tag
12+
key: tags
13+
groups:
14+
no_public_ip: public_ip == ""

tests/integration/targets/inventory_terraform_state_aws/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
- default_hostname in groups.tag_Phase_integration
119119
- "'state_running' in groups"
120120
- default_hostname in groups.state_running
121+
- "'no_public_ip' in groups"
122+
- default_hostname in groups.no_public_ip
121123

122124
always:
123125
- name: Delete temporary file

tests/unit/plugins/inventory/test_terraform_state.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ def create_instance(self, name, values):
258258
def test_create_inventory(self, inventory_plugin, mocker):
259259
hostnames = MagicMock()
260260
keyed_groups = MagicMock()
261+
groups = MagicMock()
261262
strict = MagicMock()
262263
compose = MagicMock()
263264

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

271272
inventory_plugin._set_composite_vars = MagicMock()
272273
inventory_plugin._add_host_to_keyed_groups = MagicMock()
274+
inventory_plugin._add_host_to_composed_groups = MagicMock()
273275
inventory_plugin.inventory = self.ansibleInventory()
274276

275-
inventory_plugin.create_inventory(instances, hostnames, compose, keyed_groups, strict)
277+
inventory_plugin.create_inventory(instances, hostnames, compose, keyed_groups, groups, strict)
276278

277279
for name, value in config.items():
278280
inventory_plugin.inventory.assert_value(name, value)
@@ -289,6 +291,10 @@ def test_create_inventory(self, inventory_plugin, mocker):
289291
[call(keyed_groups, vars, name, strict=strict) for name, vars in config.items()],
290292
any_order=True,
291293
)
294+
inventory_plugin._add_host_to_composed_groups.assert_has_calls(
295+
[call(groups, vars, name, strict=strict) for name, vars in config.items()],
296+
any_order=True,
297+
)
292298

293299

294300
class TestWriteTerraformConfig:
@@ -428,6 +434,7 @@ def assert_calls(self, config, super_parse_patch, read_config_data_patch):
428434
config.get("hostnames"),
429435
config.get("compose"),
430436
config.get("keyed_groups"),
437+
config.get("groups"),
431438
config.get("strict"),
432439
)
433440

0 commit comments

Comments
 (0)