-
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.
inventory/aws_ec2 - support jinja2 filters in hostnames option (#2427) (
#2456) This is a backport of PR #2427 as merged into main (995bf94). SUMMARY Closes #2402 ISSUE TYPE Feature Pull Request COMPONENT NAME inventory/aws_ec2 Reviewed-by: Helen Bailey <[email protected]> Reviewed-by: Bikouo Aubin
- Loading branch information
1 parent
52ad905
commit a60cf3c
Showing
5 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
changelogs/fragments/20241217-inventory-aws_ec2-support-jinja2-in-hostnames-variables.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: | ||
- inventory/aws_ec2 - Support jinja2 expression in ``hostnames`` variable(https://github.com/ansible-collections/amazon.aws/issues/2402). |
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
65 changes: 65 additions & 0 deletions
65
...entory_aws_ec2/playbooks/test_populating_inventory_with_hostnames_with_jinja2_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,65 @@ | ||
--- | ||
- hosts: 127.0.0.1 | ||
connection: local | ||
gather_facts: false | ||
environment: "{{ ansible_test.environment }}" | ||
tasks: | ||
- module_defaults: | ||
group/aws: | ||
access_key: "{{ aws_access_key }}" | ||
secret_key: "{{ aws_secret_key }}" | ||
session_token: "{{ security_token | default(omit) }}" | ||
region: "{{ aws_region }}" | ||
block: | ||
# Create VPC, subnet, security group, and find image_id to create instance | ||
- name: Setup EC2 network | ||
ansible.builtin.include_tasks: tasks/setup.yml | ||
|
||
- name: Create a new host | ||
amazon.aws.ec2_instance: | ||
image_id: "{{ image_id }}" | ||
name: "{{ resource_prefix }}" | ||
tags: | ||
Tag1: tag1.test-ansible | ||
Tag2: tag2.test-ansible | ||
purge_tags: true | ||
instance_type: t2.micro | ||
security_groups: "{{ sg_id }}" | ||
vpc_subnet_id: "{{ subnet_id }}" | ||
wait: false | ||
register: setup_instance | ||
|
||
# refresh inventory | ||
- ansible.builtin.meta: refresh_inventory | ||
|
||
- name: Display ansible hostvars variable | ||
ansible.builtin.debug: | ||
var: hostvars | ||
|
||
- name: Assert that hostvars contain multiple hostnames (hostnames with multiple tags and allow_duplicated_hosts=true) | ||
ansible.builtin.assert: | ||
that: | ||
- hostvars.keys() | length == 2 | ||
- '"tag1.prod-Ansible" in hostvars' | ||
- '"tag2.prod-Ansible" in hostvars' | ||
when: | ||
- search_multiple_tags | default(false) | bool | ||
- (allow_duplicated_hosts | default(false) | bool) | ||
|
||
- name: Assert that hostvars contain only 1 hostname (hostnames with multiple tags and allow_duplicated_hosts=false) | ||
ansible.builtin.assert: | ||
that: | ||
- hostvars.keys() | length == 1 | ||
- '"tag1.prod-Ansible" in hostvars' | ||
when: | ||
- search_multiple_tags | default(false) | bool | ||
- not (allow_duplicated_hosts | default(false) | bool) | ||
|
||
- name: Assert that hostvars contain only 1 hostname (hostnames with single tag) | ||
ansible.builtin.assert: | ||
that: | ||
- hostvars.keys() | length == 1 | ||
- '"TAG1.PROD-ANSIBLE" in hostvars' | ||
when: | ||
- not (search_multiple_tags | default(false) | bool) | ||
- not (allow_duplicated_hosts | default(false) | bool) |
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
17 changes: 17 additions & 0 deletions
17
...n/targets/inventory_aws_ec2/templates/inventory_with_hostnames_with_jinja2_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,17 @@ | ||
plugin: amazon.aws.aws_ec2 | ||
access_key: '{{ aws_access_key }}' | ||
secret_key: '{{ aws_secret_key }}' | ||
{% if security_token | default(false) %} | ||
session_token: '{{ security_token }}' | ||
{% endif %} | ||
{% if allow_duplicated_hosts | default(false) %} | ||
allow_duplicated_hosts: True | ||
{% endif %} | ||
regions: | ||
- '{{ aws_region }}' | ||
hostnames: | ||
{% if search_multiple_tags | default(false) %} | ||
- "tag:Tag1,Tag2 | replace('test', 'prod') | title()" | ||
{% else %} | ||
- "tag:Tag1 | replace('test', 'prod') | upper()" | ||
{% endif %} |