tweeks200
tweeks200

Reputation: 1947

Assign ansible vars based on AWS tags

I'm trying to figure out a way to assign variables in Ansible based on tags I have in AWS. I was experimenting with ec2_remote_tags but it's returning alot more information than I need. It seems like there should be an easier way to do this and I'm just not thinking of it.

For example, if I have a tag called function that creates the tag_function_api group using dynamic inventory and I want to assign a variable function to the value api. Any ideas on an efficient way to do this?

Upvotes: 3

Views: 4001

Answers (3)

Earl Ruby
Earl Ruby

Reputation: 1590

If you are using Ansible's ec2.py dynamic inventory script it makes all tags available as host variables in the form ec2_tag_<tag name> = <tag value>. It also adds all EC2 hosts to the group ec2.

So if your EC2 instance has a tag AwesomeVariable = "Greatness" and you want that value assigned to the Ansible host variable stupendous you can do this:

- name: Register variables based on tags
  set_fact:
    stupendous: "{{ ec2_tag_AwesomeVariable }}"
  when: "'ec2' in group_names"

After this runs you can use the variable stupendous for your EC2 hosts and it has the value set for the AwesomeVariable tag.

Upvotes: 1

tweeks200
tweeks200

Reputation: 1947

Was able to get this to work based on some more information I found here: https://groups.google.com/forum/#!topic/ansible-project/ES2CjMPps3M

Here is the code that worked for us:

  - name: Retrieve all tags on an instance
    ec2_tag:
      region: '{{ ec2_region }}'
      resource: '{{ ec2_id }}'
      state: list
      aws_access_key: "{{ ANSIBLE_IAM_KEY }}"
      aws_secret_key: "{{ ANSIBLE_IAM_SECRET }}"
    register: ec2_facts

  - name: register variables based on tag
    set_fact:
      tt_function: "{{ ec2_facts.tags.Function }}"
      tt_release: "{{ ec2_facts.tags.Release }}"
      tt_client: "{{ ec2_facts.tags.Client }}"

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68289

I've managed to make a dict of tags with lists of values:

- hosts: localhost
  tasks:
    - ec2_remote_facts:
        region: eu-west-1
      register: ec2_facts

    # get all possible tag names
    - set_fact: tags="{{ item.keys() }}"
      with_items: "{{ ec2_facts.instances | map(attribute='tags') | list }}"
      register: tmp_tags

    # get flattened list of tags (for some reason lookup() returns string, so we use with_)
    - assert: that=true
      with_flattened: "{{ tmp_tags.results | map(attribute='ansible_facts.tags') | list }}"
      register: tmp_tags

    # get unique tag names
    - set_fact: tags="{{ tmp_tags.results | map(attribute='item') | list | unique }}"

    - set_fact: my_tags="{{ {} }}"

    # get all possible values for a given tag
    - set_fact:
        my_tags: "{{ my_tags | combine( {''+item: ec2_facts.instances | map(attribute='tags.'+item) | select('defined') | list | unique}) }}"
      with_items: "{{ tags }}"

    - debug: var=my_tags

Upvotes: 3

Related Questions