Vishal Patil
Vishal Patil

Reputation: 87

Ansible inventory.aws_ec2.yml tags

Can someone please explain how to defined value in the inventory.aws_ec2.yml file?

I want to execute ansible-playbook on the below mentiond tags instances. Where I need to defined Environment:stage in inventory.aws_ec2.yml. i.e. in key or in prefix?

      tags:
        Environment: stage

Below is inventory.aws_ec2.yml file.

plugin: aws_ec2
regions:
  - us-east-2
  - us-west-2

keyed_groups:
  # Add hosts to tag name groups
  #  - key: tags.Role
  #    prefix: role_
  #    separator: ""
  - key: tags.Environment
    prefix: stage
    separator: ""

Upvotes: 0

Views: 567

Answers (1)

gary lopez
gary lopez

Reputation: 1954

When you use key: tags.Environment by each different value of this tag will be created a group in ansible, prefix is to add a prefix to the name of each of groups created in ansible (I recommend you in this case use "Environment") and separator is to add characters between the prefix and the value of the tags (by default is "_"). A way to write your aws_ec2.yml is

plugin: aws_ec2
regions:
  - us-east-2
  - us-west-2

keyed_groups:
  - key: tags.Environment
    prefix: Environment
    separator: "_"

For example, if you have 4 ec2 instances with tag: Environment=stage and 2 ec2 instances with tag: Environment=prod, you'll have two groups in ansible, Environment_stage and Environment_prod then you could create your playbook in this way

- name: Testing my groups
  hosts: Environment_stage
  gather_facts: yes

  tasks:
  - name: Debug my ec2 instances
    debug:
      msg: "hostname: {{ ansible_hostname }}" 

Upvotes: 1

Related Questions