Skip to content

Instantly share code, notes, and snippets.

@gkspranger
Created October 5, 2017 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkspranger/250a28d3587bc9f1c493d7064650e16a to your computer and use it in GitHub Desktop.
Save gkspranger/250a28d3587bc9f1c493d7064650e16a to your computer and use it in GitHub Desktop.
# AWS EC2 hosts
# using ANSIBLE to assign tags
{% macro eze(tag) -%}
{# this is an easy, consistent way to enable a tag #}
{{ tag }} = enabled
{%- endmacro %}
{# loop thru all your EC2 hosts in alpha order #}
{% for i in ansible_play_batch | sort %}
{# set the node name AND node vars #}
{% set node_name=i %}
{% set node_vars=hostvars[ i ] %}
# tags for {{ node_name }}
[host={{ node_name }}]
{# set custom enviro tag -- dev, stage, prod, etc #}
{{ eze(node_vars.enviro) }}
{# set std AWS metadata tag -- region, AZ, etc #}
{{ eze(node_vars.ansible_ec2_placement_region) }}
{{ eze(node_vars.ansible_ec2_instance_id) }}
{{ eze(node_vars.ansible_ec2_placement_availability_zone) }}
{# set custom tag if condition is met -- app type, etc #}
{% if node_vars.app_type is defined %}
{{ eze(node_vars.app_type) }}
{% endif %}
...
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment