-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alina Buzachis <[email protected]>
- Loading branch information
1 parent
b04f4e0
commit 0891fba
Showing
5 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...rgets/inventory_aws_ec2/playbooks/test_populating_inventory_with_hostnames_using_tags.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,62 @@ | ||
--- | ||
- hosts: 127.0.0.1 | ||
connection: local | ||
gather_facts: false | ||
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 | ||
- name: create a new host | ||
ec2_instance: | ||
image_id: '{{ image_id }}' | ||
name: '{{ resource_prefix }}' | ||
tags: | ||
Tag1: Test1 | ||
Tag2: Test2 | ||
instance_type: t2.micro | ||
security_groups: '{{ sg_id }}' | ||
vpc_subnet_id: '{{ subnet_id }}' | ||
wait: false | ||
register: setup_instance | ||
|
||
# refresh inventory | ||
- meta: refresh_inventory | ||
|
||
- debug: | ||
var: groups | ||
|
||
- name: assert groups and hostvars were populated with inventory | ||
assert: | ||
that: | ||
- "'aws_ec2' in groups" | ||
- groups['aws_ec2'] | length == 2 | ||
- "'Tag1_Test1' in groups['aws_ec2']" | ||
- "'Tag2_Test2' in groups['aws_ec2']" | ||
- "'Tag1_Test1' in hostvars" | ||
- "'Tag2_Test2' in hostvars" | ||
|
||
always: | ||
|
||
- name: remove ec2 instance | ||
ec2_instance: | ||
instance_type: t2.micro | ||
instance_ids: '{{ setup_instance.instance_ids }}' | ||
state: absent | ||
name: '{{ resource_prefix }}' | ||
security_groups: "{{ sg_id }}" | ||
vpc_subnet_id: "{{ subnet_id }}" | ||
ignore_errors: true | ||
when: setup_instance 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
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
21 changes: 21 additions & 0 deletions
21
...ntegration/targets/inventory_aws_ec2/templates/inventory_with_hostnames_using_tags.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,21 @@ | ||
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 }}' | ||
keyed_groups: | ||
- prefix: tag | ||
key: tags | ||
hostnames: | ||
# can also be specified using | ||
# - tag:Tag1,Tag2 | ||
# or | ||
# - tag:Tag1 | ||
# - tag:Tag2 | ||
# or | ||
- tag:Tag1=Test1,Tag2=Test2 | ||
compose: | ||
ansible_host: private_ip_address |
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 |
---|---|---|
|
@@ -18,4 +18,4 @@ hostnames: | |
- tag:Tag1 | ||
- tag:Tag2 | ||
compose: | ||
ansible_host: private_ip_address | ||
ansible_host: private_ip_address |