Score:1

Ansible working with ec2 tags

sj flag

I have a playbook with dynamic ec2 inventory, below is the graph.

$ ansible-inventory -i inventory/dynamic_inventory/uat_aws_ec2.yaml --graph
@all:
  |--@aws_ec2:
  |  |--xx.xxx.xx.xx
  |  |--xx.xxx.xx.xx
  |  |--xx.xxx.xx.xx
  |--@nonprod_uat:
  |  |--xx.xxx.xx.3
  |  |--xx.xxx.xx.1
  |  |--xx.xxx.xx.2
  |--@uat_auth:
  |  |--xx.xxx.xx.xx
  |--@uat_web:
  |  |--xx.xxx.xx.xx
  |--@ungrouped:

Now if I need to use set_facts for a specific tag? Example, I need to use something like this.

As I'm already using the plugin for dynamic inventory, it should be simple enough I guess

- set_fact:
    deploy: uat
  when:  "ec2_tags/hostgroups('nonprod_uat')"
  • debug: var=hostvars also gives me the tag information, but need to know how to use it to set_facts on the playbook
Score:1
sj flag

Got the answer myself after doing some trial and error

  • set_fact: deploy: uat when: hostvars[inventory_hostname].tags.Environment == 'uat'
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.