Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to prefix and/or suffix hostvar coming from AWS #621

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- add new parameters hostvars_prefix and hostvars_suffix for inventory plugins aws_ec2 and aws_rds (https://github.com/ansible-collections/amazon.aws/issues/535).
25 changes: 25 additions & 0 deletions plugins/inventory/aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
- The use of this feature is discouraged and we advise to migrate to the new ``tags`` structure.
type: bool
default: False
hostvars_prefix:
description:
- The prefix for host variables names coming from AWS.
type: str
version_added: 3.1.0
hostvars_suffix:
description:
- The suffix for host variables names coming from AWS.
type: str
version_added: 3.1.0
'''

EXAMPLES = '''
Expand Down Expand Up @@ -205,6 +215,12 @@
ansible_host: public_dns_name
groups:
libvpc: vpc_id == 'vpc-####'
# Define prefix and suffix for host variables coming from AWS.
plugin: aws_ec2
regions:
- us-east-1
hostvars_prefix: 'aws_'
hostvars_suffix: '_ec2'
'''

import re
Expand Down Expand Up @@ -647,8 +663,17 @@ def _add_hosts(self, hosts, group, hostnames):
if not hostname:
continue
self.inventory.add_host(hostname, group=group)
hostvars_prefix = self.get_option("hostvars_prefix")
hostvars_suffix = self.get_option("hostvars_suffix")
new_vars = dict()
for hostvar, hostval in host.items():
if hostvars_prefix:
hostvar = hostvars_prefix + hostvar
if hostvars_suffix:
hostvar = hostvar + hostvars_suffix
new_vars[hostvar] = hostval
self.inventory.set_variable(hostname, hostvar, hostval)
host.update(new_vars)

# Use constructed if applicable

Expand Down
21 changes: 21 additions & 0 deletions plugins/inventory/aws_rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
iam_role_arn:
description: The ARN of the IAM role to assume to perform the inventory lookup. You should still provide
AWS credentials with enough privilege to perform the AssumeRole action.
hostvars_prefix:
description:
- The prefix for host variables names coming from AWS.
type: str
version_added: 3.1.0
hostvars_suffix:
description:
- The suffix for host variables names coming from AWS.
type: str
version_added: 3.1.0
note:
Ansible versions prior to 2.10 should use the fully qualified plugin name 'amazon.aws.aws_rds'.
extends_documentation_fragment:
Expand All @@ -64,6 +74,8 @@
prefix: rds
- key: tags
- key: region
hostvars_prefix: aws_
hostvars_suffix: _rds
'''

try:
Expand Down Expand Up @@ -280,8 +292,17 @@ def _add_hosts(self, hosts, group):
host['region'] = host['availability_zones'][0][:-1]

self.inventory.add_host(hostname, group=group)
hostvars_prefix = self.get_option("hostvars_prefix")
hostvars_suffix = self.get_option("hostvars_suffix")
new_vars = dict()
for hostvar, hostval in host.items():
if hostvars_prefix:
hostvar = hostvars_prefix + hostvar
if hostvars_suffix:
hostvar = hostvar + hostvars_suffix
new_vars[hostvar] = hostval
self.inventory.set_variable(hostname, hostvar, hostval)
host.update(new_vars)

# Use constructed if applicable
strict = self.get_option('strict')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
- 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
ec2_instance:
image_id: '{{ image_id }}'
name: '{{ resource_prefix }}_1'
tags:
tag_instance1: foo
instance_type: t2.micro
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
wait: no
register: setup_instance_1

- meta: refresh_inventory

- name: assert the hostvars are defined with prefix and/or suffix
assert:
that:
- "hostvars['{{ resource_prefix }}_1'].{{ vars_prefix }}instance_type{{ vars_suffix }} == 't2.micro'"
- "'{{ vars_prefix }}instance_type{{ vars_suffix }}' in hostvars['{{ resource_prefix }}_1']"
- "'{{ vars_prefix }}image_id{{ vars_suffix }}' in hostvars['{{ resource_prefix }}_1']"
- "'{{ vars_prefix }}instance_id{{ vars_suffix }}' in hostvars['{{ resource_prefix }}_1']"
- "'instance_type' not in hostvars['{{ resource_prefix }}_1']"
- "'image_id' not in hostvars['{{ resource_prefix }}_1']"
- "'instance_id' not in hostvars['{{ resource_prefix }}_1']"
- "'ansible_diff_mode' in hostvars['{{ resource_prefix }}_1']"
- "'ansible_forks' in hostvars['{{ resource_prefix }}_1']"
- "'ansible_version' in hostvars['{{ resource_prefix }}_1']"
vars:
vars_prefix: "{{ hostvars_prefix | default('') }}"
vars_suffix: "{{ hostvars_suffix | default('') }}"

always:

- name: remove setup ec2 instance
ec2_instance:
instance_type: t2.micro
instance_ids: '{{ setup_instance_1.instance_ids }}'
state: absent
name: '{{ resource_prefix }}_1'
security_groups: "{{ sg_id }}"
vpc_subnet_id: "{{ subnet_id }}"
ignore_errors: yes
when: setup_instance_1 is defined

- include_tasks: tear_down.yml
10 changes: 10 additions & 0 deletions tests/integration/targets/inventory_aws_ec2/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ ansible-playbook playbooks/test_populating_inventory_with_concatenation.yml "$@"
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_include_or_exclude_filters.yml.j2'" "$@"
ansible-playbook playbooks/test_populating_inventory_with_include_or_exclude_filters.yml "$@"

# generate inventory config with hostvars_prefix
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_hostvars_prefix_suffix.yml.j2'" -e "hostvars_prefix='aws_ec2_'" "$@"
ansible-playbook playbooks/test_populating_inventory_with_hostvars_prefix_suffix.yml -e "hostvars_prefix='aws_ec2_'" "$@"
# generate inventory config with hostvars_suffix
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_hostvars_prefix_suffix.yml.j2'" -e "hostvars_suffix='_aws_ec2'" "$@"
ansible-playbook playbooks/test_populating_inventory_with_hostvars_prefix_suffix.yml -e "hostvars_suffix='_aws_ec2'" "$@"
# generate inventory config with hostvars_prefix and hostvars_suffix
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_hostvars_prefix_suffix.yml.j2'" -e "hostvars_prefix='aws_'" -e "hostvars_suffix='_ec2'" "$@"
ansible-playbook playbooks/test_populating_inventory_with_hostvars_prefix_suffix.yml -e "hostvars_prefix='aws_'" -e "hostvars_suffix='_ec2'" "$@"

# generate inventory config with caching and test using it
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_use_contrib_script_keys.yml.j2'" "$@"
ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS=never ansible-playbook playbooks/test_populating_inventory_with_use_contrib_script_keys.yml "$@"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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 }}_*'
{% if hostvars_prefix | default(false) %}
hostvars_prefix: '{{ hostvars_prefix }}'
{% endif %}
{% if hostvars_suffix | default(false) %}
hostvars_suffix: '{{ hostvars_suffix }}'
{% endif %}
hostnames:
- tag:Name
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
- hosts: 127.0.0.1
connection: local
gather_facts: no
environment: "{{ ansible_test.environment }}"
collections:
- community.aws
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:

- set_fact:
instance_id: "{{ resource_prefix }}-mariadb"

- name: create minimal mariadb instance in default VPC and default subnet group
rds_instance:
state: present
engine: mariadb
db_instance_class: db.t2.micro
allocated_storage: 20
instance_id: '{{ resource_prefix }}-mariadb'
master_username: 'ansibletestuser'
master_user_password: 'password-{{ resource_prefix | regex_findall(".{8}$") | first }}'
tags:
workload_type: other
register: setup_instance

- meta: refresh_inventory

- name: assert the hostvars are defined with prefix and/or suffix
assert:
that:
- "hostvars[host_instance_name].{{ vars_prefix }}db_instance_class{{ vars_suffix }} == 'db.t2.micro'"
- "hostvars[host_instance_name].{{ vars_prefix }}engine{{ vars_suffix }} == 'mariadb'"
- "hostvars[host_instance_name].{{ vars_prefix }}db_instance_status{{ vars_suffix }} == 'available'"
- "'db_instance_class' not in hostvars[host_instance_name]"
- "'engine' not in hostvars[host_instance_name]"
- "'db_instance_status' not in hostvars[host_instance_name]"
- "'ansible_diff_mode' in hostvars[host_instance_name]"
- "'ansible_forks' in hostvars[host_instance_name]"
- "'ansible_version' in hostvars[host_instance_name]"
vars:
host_instance_name: "{{ resource_prefix }}-mariadb"
vars_prefix: "{{ inventory_prefix | default('') }}"
vars_suffix: "{{ inventory_suffix | default('') }}"

always:

- name: remove mariadb instance
rds_instance:
state: absent
engine: mariadb
skip_final_snapshot: yes
instance_id: '{{ instance_id }}'
ignore_errors: yes
when: setup_instance is defined
12 changes: 12 additions & 0 deletions tests/integration/targets/inventory_aws_rds/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,17 @@ rm -r aws_rds_cache_dir/
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_constructed.j2'" "$@"
ansible-playbook playbooks/test_populating_inventory_with_constructed.yml "$@"

# generate inventory config with hostvars_prefix features and test using it
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_hostvars_prefix_suffix.j2'" -e "inventory_prefix='aws_rds_'" "$@"
ansible-playbook playbooks/test_inventory_with_hostvars_prefix_suffix.yml -e "inventory_prefix='aws_rds_'" "$@"

# generate inventory config with hostvars_suffix features and test using it
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_hostvars_prefix_suffix.j2'" -e "inventory_suffix='_aws_rds'" "$@"
ansible-playbook playbooks/test_inventory_with_hostvars_prefix_suffix.yml -e "inventory_suffix='_aws_rds'" "$@"

# generate inventory config with hostvars_prefix and hostvars_suffix features and test using it
ansible-playbook playbooks/create_inventory_config.yml -e "template='inventory_with_hostvars_prefix_suffix.j2'" -e "inventory_prefix='aws_'" -e "inventory_suffix='_rds'" "$@"
ansible-playbook playbooks/test_inventory_with_hostvars_prefix_suffix.yml -e "inventory_prefix='aws_'" -e "inventory_suffix='_rds'" "$@"

# cleanup inventory config
ansible-playbook playbooks/empty_inventory_config.yml "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugin: amazon.aws.aws_rds
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 }}'
{% if inventory_prefix | default(false) %}
hostvars_prefix: '{{ inventory_prefix }}'
{% endif %}
{% if inventory_suffix | default(false) %}
hostvars_suffix: '{{ inventory_suffix }}'
{% endif %}
filters:
db-instance-id: "{{ resource_prefix }}-mariadb"