| 1 |
# Indirect Node Counts in Ansible Collections |
| 2 |
|
| 3 |
## Overview |
| 4 |
|
| 5 |
In the context of Ansible automation, **indirect node counts** refer to the practice of verifying capacity or availability **by analyzing the output of Ansible modules that interact with external systems**, rather than directly querying the nodes themselves. This approach is especially useful in complex, public cloud, or on-premises environments. |
| 6 |
|
| 7 |
This file explains: |
| 8 |
- What indirect node counts are |
| 9 |
- Why they are required |
| 10 |
- What they enable |
| 11 |
- Their value |
| 12 |
|
| 13 |
## What Are Indirect Node Counts? |
| 14 |
|
| 15 |
Standard node counting relies on Ansible connecting to a host. However, in cloud or API-driven automation, the "managed nodes" are often manipulated via a centralized API (like AWS or Azure) rather than direct connections. |
| 16 |
|
| 17 |
**Indirect node counts** solve this by inspecting the **return values (JSON payload)** of specific Ansible modules. |
| 18 |
|
| 19 |
For example: |
| 20 |
1. A playbook runs `amazon.aws.ec2_instance_info` to gather facts about your cloud environment. |
| 21 |
2. The module queries the AWS API and returns a list of instances. |
| 22 |
3. The controller applies a specific filter (defined in `extensions/audit/event_query.yml`) to that output to count how many unique nodes were automated. |
| 23 |
|
| 24 |
This process is **passive**: it derives counts from the automation that is already running, without initiating separate API requests. |
| 25 |
|
| 26 |
## Why Are They Required? |
| 27 |
|
| 28 |
Directly connecting to every node: |
| 29 |
- Is **inefficient** in large-scale environments |
| 30 |
- May be **prohibited** due to security, network segmentation, or policy |
| 31 |
- **Doesn't scale** across multiple clusters or providers |
| 32 |
- Often leads to **incomplete or stale data** |
| 33 |
|
| 34 |
Using indirect node counts via cluster managers: |
| 35 |
- Enables **centralized insight** into resource usage |
| 36 |
- Works well in **managed or disconnected environments** |
| 37 |
- Allows **non-invasive** assessment (e.g., read-only API access) |
| 38 |
|
| 39 |
## What Does It Do? |
| 40 |
|
| 41 |
In practical terms, using indirect node counts in an Ansible collection: |
| 42 |
- Enables **guardrails** to verify what you are automating |
| 43 |
- Reduces operational risk and improves **predictability** of automation workflows |
| 44 |
|
| 45 |
## Why This Is a Good Practice |
| 46 |
|
| 47 |
| Benefit | Description | |
| 48 |
|--------|-------------| |
| 49 |
| ✅ Scalable | Works across many clusters and environments | |
| 50 |
| ✅ Secure | Limits direct access to sensitive nodes | |
| 51 |
| ✅ Efficient | Avoids per-node polling, uses cached or aggregated data | |
| 52 |
| ✅ Integrated | Leverages our existing Certified and Validated collections | |
| 53 |
| ✅ Reliable | Provides consistent data source for automation decisions | |
| 54 |
|
| 55 |
## Supported Resources for Amazon Web Services (AWS) |
| 56 |
|
| 57 |
The following table details the AWS resources currently supported by the NodeQuery logic within the **amazon.aws** collection. To ensure consistent reporting across different services, this extension maps service-specific attributes to a set of **Canonical Facts**. |
| 58 |
|
| 59 |
**What are Canonical Facts?** |
| 60 |
Canonical Facts are standardized fields (like `id`, `name`, `tags`, and `status`, and `status`) that provide a uniform way to identify and assess resources, regardless of the underlying AWS service. For example, whether querying an EC2 instance or a VPC, the unique identifier is always mapped to `id`. |
| 61 |
|
| 62 |
**Note:** Not all resources support every fact. For example, a Route Table may only have an `id` and `tags`, while an EC2 instance will have all four. |
| 63 |
|
| 64 |
The table below details how AWS resources are mapped to these canonical facts. |
| 65 |
|
| 66 |
| Category | AWS Resource | Ansible Module | Canonical Fact Mapping | |
| 67 |
| :--- | :--- | :--- | :--- | |
| 68 |
| **Compute** | **EC2 Instance** | `ec2_instance_info` | `.instance_id` → **id**<br>`.state` → **status**<br>`(.name // .tags.Name)` → **name** <br>`.tags` → **tags** | |
| 69 |
| **Database** | **RDS Instance** | `rds_instance_info` | `.db_instance_identifier` → **name**<br>`.dbi_resource_id` → **id**<br>`.db_instance_status` → **status**<br>`.tags` → **tags** | |
| 70 |
| **Database** | **RDS Cluster** | `rds_cluster_info` | `db_cluster_identifier` → **name**<br>`.db_cluster_resource_id` → **id**<br>`.status` → **status**<br>`.tags` → **tags** | |
| 71 |
| **Storage** | **S3 Bucket** | `s3_bucket_info` | `.name` → **name**<br>`.bucket_tagging` → **tags** | |
| 72 |
| **Storage** | **S3 Object** | `s3_object_info` | `.object_name` → **name**<br>`.object_tagging` → **tags** | |
| 73 |
| **Networking** | **Application / Network LB** | `elb_application_lb_info` | `.load_balancer_name` → **name**<br>`state` → **status** | |
| 74 |
| **Networking** | **Classic Load Balancer** | `elb_classic_lb_info` | `.load_balancer_name` → **name**<br>| |
| 75 |
| **Networking** | **VPC** | `ec2_vpc_net_info` | `id` → **id**<br>`state` → **status**<br>`.tags` → **tags** | |
| 76 |
| **Networking** | **VPC Subnet** | `ec2_vpc_subnet_info` | `.id` → **id**<br>`.state` → **status**<br>`.tags` → **tags** | |
| 77 |
| **Networking** | **NAT Gateway** | `ec2_vpc_nat_gateway_info` | `.nat_gateway_id` → **id**<br>`state` → **status**<br>`.tags` → **tags** | |
| 78 |
| **Networking** | **Internet Gateway** | `ec2_vpc_igw_info` | `.internet_gateway_id` → **id**<br>`.attachments.state` → **status**<br>`.tags` → **tags** | |
| 79 |
| **Networking** | **Virtual Private Gateway** | `ec2_vpc_vgw_info` | `.vpn_gateway_id` → **id**<br>`.state` → **status**<br>`.tags` → **tags** | |
| 80 |
| **Networking** | **Route Table** | `ec2_vpc_route_table_info` | `.route_table_id` → **id**<br>`.tags` → **tags** | |
| 81 |
| **Networking** | **VPC Peering Connection** | `ec2_vpc_peering_info` | `.vpc_peering_connection_id` → **id**<br>`.state.code` → **status**<br>`.tags` → **tags**| |
| 82 |
| **Networking** | **VPN Connection** | `ec2_vpc_vpn_info` | `.vpn_connection_id` → **id**<br>`.state` → **status**<br>`.tags` → **tags** | |
| 83 |
|
| 84 |
## Testing and Validation |
| 85 |
|
| 86 |
Reliability is ensured through integration testing. The **extensions/audit/event_query.yml** file is explicitly tested in this collection to verify that the node counting logic works as expected against the supported resources. |
| 87 |
|
| 88 |
### How to Run Integration Tests Locally |
| 89 |
|
| 90 |
To validate the node counting logic yourself, you can run any integration test target prefixed with **node_query_**. |
| 91 |
|
| 92 |
#### Prerequisites |
| 93 |
The indirect node counts integration tests have additional dependencies, specifically the `jq` Python bindings. Please run `pip install -r tests/integration/requirements.txt` to ensure all requirements are installed." |
| 94 |
|
| 95 |
#### Command |
| 96 |
**Example: Running the Networking Node Query Test** |
| 97 |
|
| 98 |
```bash |
| 99 |
ansible-test integration node_query_networking --no-temp-workdir |
| 100 |
``` |
| 101 |
|
| 102 |
**Important: Critical Note: The **--no-temp-workdir** Flag** |
| 103 |
|
| 104 |
When running these tests locally, you must include the **--no-temp-workdir** flag. |
| 105 |
|
| 106 |
- **The Problem**: The tests rely on a local helper role, **setup_node_query**, to prepare the environment. Standard **ansible-test** behavior creates a temporary, isolated testing directory that does not automatically copy this helper role (as it is not a production dependency listed in **meta/main.yml**). |
| 107 |
|
| 108 |
- **The Solution**: The **--no-temp-workdir** flag forces **ansible-test** to run in the current directory context, ensuring the test suite can access the **setup_node_query** role. |