Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to use ansible to identify AWS rds instances that have specific tags and exclude them from the result is to use the tag query options for the ec2_vpc_net_facts module.

Here's an example playbook that uses ec2_vpc_net_facts to query for RDS instances that have a specific Environment tag and then uses the selectattr filter to exclude any instances that have a Status tag matching production:

- name: Find non-production RDS instances
  hosts: localhost
  gather_facts: False
  vars:
    region: us-west-2
    environment_tag: Environment:dev
  tasks:
    - name: Get RDS instances with environment tag
      ec2_vpc_net_facts:
        region: "{{ region }}"
        filters:
          tag: "{{ environment_tag }}"
        refresh_cache: true
      register: results

    - name: Filter out production instances
      set_fact:
        rds_nonprod: "{{ results.vpcs[0].rds_instances | selectattr('tags.Status', 'ne', 'production') | list }}"

    - name: Print non-production instances
      debug:
        var: rds_nonprod

In this example, we are using the ec2_vpc_net_facts module to query for RDS instances in the us-west-2 region that have the Environment:dev tag. We then register the results in the results variable.

Next, we use the set_fact module to create a new rds_nonprod variable. Here we use the selectattr filter to exclude any instances that have a Status tag matching production, and then convert the result to a list.

Finally, we use the debug module to print out the rds_nonprod variable, which will contain a list of all RDS instances with the Environment:dev tag, excluding any instances with a Status tag matching production.