Ask Your Question
2

How can you use ansible to identify AWS rds instances that have specific tags and exclude them from the result?

asked 2022-11-29 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-01-14 10:00:00 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-11-29 11:00:00 +0000

Seen: 10 times

Last updated: Jan 14 '23