-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws_ec2 inventory: include_filters and exclude_filters (#328)
aws_ec2 inventory: add includes_filters and excludes_filters Reviewed-by: https://github.com/apps/ansible-zuul
- Loading branch information
Showing
5 changed files
with
206 additions
and
7 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
changelogs/fragments/328_aws_ec2_inventory_includes_entries_matching.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
minor_changes: | ||
- "aws_ec2 inventory - expose to new keys called ``include_filters`` and ``exclude_filters`` to give the user the ability to compose an inventory with multiple queries (https://github.com/ansible-collections/amazon.aws/pull/328)." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...inventory_aws_ec2/playbooks/test_populating_inventory_with_include_or_exclude_filters.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--- | ||
- hosts: 127.0.0.1 | ||
connection: local | ||
gather_facts: no | ||
environment: "{{ ansible_test.environment }}" | ||
tasks: | ||
|
||
- module_defaults: | ||
group/aws: | ||
aws_access_key: '{{ aws_access_key }}' | ||
aws_secret_key: '{{ aws_secret_key }}' | ||
security_token: '{{ security_token | default(omit) }}' | ||
region: '{{ aws_region }}' | ||
block: | ||
|
||
# Create VPC, subnet, security group, and find image_id to create instance | ||
|
||
- include_tasks: setup.yml | ||
|
||
# Create new host, refresh inventory | ||
- name: create a new host (1/3) | ||
ec2: | ||
image: '{{ image_id }}' | ||
exact_count: 1 | ||
count_tag: | ||
Name: '{{ resource_prefix }}_1' | ||
instance_tags: | ||
Name: '{{ resource_prefix }}_1' | ||
tag_instance1: foo | ||
instance_type: t2.micro | ||
wait: yes | ||
group_id: '{{ sg_id }}' | ||
vpc_subnet_id: '{{ subnet_id }}' | ||
register: setup_instance_1 | ||
|
||
- name: create a new host (2/3) | ||
ec2: | ||
image: '{{ image_id }}' | ||
exact_count: 1 | ||
count_tag: | ||
Name: '{{ resource_prefix }}_2' | ||
instance_tags: | ||
Name: '{{ resource_prefix }}_2' | ||
tag_instance2: bar | ||
instance_type: t2.micro | ||
wait: yes | ||
group_id: '{{ sg_id }}' | ||
vpc_subnet_id: '{{ subnet_id }}' | ||
register: setup_instance_2 | ||
|
||
- name: create a new host (3/3) | ||
ec2: | ||
image: '{{ image_id }}' | ||
exact_count: 1 | ||
count_tag: | ||
Name: '{{ resource_prefix }}_3' | ||
instance_tags: | ||
Name: '{{ resource_prefix }}_3' | ||
tag_instance2: bar | ||
instance_type: t2.micro | ||
wait: yes | ||
group_id: '{{ sg_id }}' | ||
vpc_subnet_id: '{{ subnet_id }}' | ||
register: setup_instance_3 | ||
|
||
- meta: refresh_inventory | ||
|
||
- name: assert the keyed groups and groups from constructed config were added to inventory and composite var added to hostvars | ||
assert: | ||
that: | ||
# There are 9 groups: all, ungrouped, aws_ec2, sg keyed group, 3 tag keyed group (one per tag), arch keyed group, constructed group | ||
- "groups['all'] | length == 2" | ||
- "'{{ resource_prefix }}_1' in groups['all']" | ||
- "'{{ resource_prefix }}_2' in groups['all']" | ||
- "not ('{{ resource_prefix }}_3' in groups['all'])" | ||
|
||
always: | ||
|
||
- name: remove setup ec2 instance (1/3) | ||
ec2: | ||
instance_type: t2.micro | ||
instance_ids: '{{ setup_instance_1.instance_ids }}' | ||
state: absent | ||
wait: yes | ||
instance_tags: | ||
Name: '{{ resource_prefix }}' | ||
group_id: "{{ sg_id }}" | ||
vpc_subnet_id: "{{ subnet_id }}" | ||
ignore_errors: yes | ||
when: setup_instance_1 is defined | ||
|
||
- name: remove setup ec2 instance (2/3) | ||
ec2: | ||
instance_type: t2.micro | ||
instance_ids: '{{ setup_instance_2.instance_ids }}' | ||
state: absent | ||
wait: yes | ||
instance_tags: | ||
Name: '{{ resource_prefix }}' | ||
group_id: "{{ sg_id }}" | ||
vpc_subnet_id: "{{ subnet_id }}" | ||
ignore_errors: yes | ||
when: setup_instance_2 is defined | ||
|
||
- name: remove setup ec2 instance (3/3) | ||
ec2: | ||
instance_type: t2.micro | ||
instance_ids: '{{ setup_instance_3.instance_ids }}' | ||
state: absent | ||
wait: yes | ||
instance_tags: | ||
Name: '{{ resource_prefix }}' | ||
group_id: "{{ sg_id }}" | ||
vpc_subnet_id: "{{ subnet_id }}" | ||
ignore_errors: yes | ||
when: setup_instance_3 is defined | ||
|
||
- include_tasks: tear_down.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...tion/targets/inventory_aws_ec2/templates/inventory_with_include_or_exclude_filters.yml.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
plugin: amazon.aws.aws_ec2 | ||
aws_access_key_id: '{{ aws_access_key }}' | ||
aws_secret_access_key: '{{ aws_secret_key }}' | ||
{% if security_token | default(false) %} | ||
aws_security_token: '{{ security_token }}' | ||
{% endif %} | ||
regions: | ||
- '{{ aws_region }}' | ||
filters: | ||
tag:Name: | ||
- '{{ resource_prefix }}_1' | ||
- '{{ resource_prefix }}_3' | ||
include_filters: | ||
- tag:Name: | ||
- '{{ resource_prefix }}_2' | ||
- '{{ resource_prefix }}_4' | ||
exclude_filters: | ||
- tag:Name: | ||
- '{{ resource_prefix }}_3' | ||
- '{{ resource_prefix }}_4' | ||
hostnames: | ||
- tag:Name | ||
- dns-name |