How to get ec2 instance id tag when it's in an autoscaling group - a Jinja2 escaping problem?

449 views
Skip to first unread message

Stephen Tan

unread,
Mar 17, 2015, 5:16:48 AM3/17/15
to ansible...@googlegroups.com
Sounds simple, but it isn't.

I want to create a variable called "instance_role" which is set to the aws:autoscaling:groupName on the instance. 
However, there is a formatting problem as the colons are used by Jinja and I can't figure out how to escape them. 

The role: 

---
- name: Get our facts
  action: ec2_facts
- name: Using the instance ID, we can get our tags
  ec2_tag:
    resource: "{{ ansible_ec2_instance_id }}"
    state: list
    region: "{{ ansible_ec2_placement_region }}"
  register: instance_tags
- debug: var=instance_tags
# example output for reference
#ok: [localhost] => {
#    "instance_tags": {
#        "changed": false,
#        "invocation": {
#            "module_args": "",
#            "module_name": "ec2_tag"
#        },
#        "tags": {
#            "Name": "trainsmart-openvpnas-prod",
#            "aws:autoscaling:groupName": "openvpnas",
#            "environment": "access",
#            "region": "eu-west-1",
#            "stage": "prod"
#        }
#    }
#}

- name: Extract our instance role tag - ie aws:autoscaling:groupName value
  set_fact:
    instance_role: "{{ instance_tags.tags.aws:autoscaling:groupName }}"
- debug: var=instance_role

Outputs the following result:

root@ip-172-16-0-227:~/ansible# ansible-playbook local.yml  -t get_role_tags

PLAY [Base config and return register for instance roles for final config] ****

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [get_role_tags | Get our facts] *****************************************
ok: [localhost]

TASK: [get_role_tags | Using the instance ID, we can get our tags] ************
ok: [localhost]

TASK: [get_role_tags | debug var=instance_tags] *******************************
ok: [localhost] => {
    "instance_tags": {
        "changed": false,
        "invocation": {
            "module_args": "",
            "module_name": "ec2_tag"
        },
        "tags": {
            "Name": "trainsmart-openvpnas-prod",
            "aws:autoscaling:groupName": "openvpnas",
            "environment": "access",
            "org": "trainsmart",
            "region": "eu-west-1",
            "stage": "prod"
        }
    }
}

TASK: [get_role_tags | Extract our instance role tag - ie aws:autoscaling:groupName value] ***
fatal: [localhost] => template error while templating string: expected token 'end of print statement', got ':'


Stephen Tan

unread,
Mar 17, 2015, 7:56:31 AM3/17/15
to ansible...@googlegroups.com
OK - someone called halberom on freenode #ansible gave me the answer:

    # A better way of referencing dict elements to avoid escaping issues
    instance_role: "{{ instance_tags['tags']['aws:autoscaling:groupName'] }}"

this is a cleaner way of getting to the dictionary. 
Reply all
Reply to author
Forward
0 new messages