So far I came up with the following, but it does feel a bit kludgy:
- name: check if running inside AWS
uri:
timeout: 2
register: aws_uri_check
failed_when: False
- name: store result
set_fact:
inside_aws: "{{ aws_uri_check.status == 200 }}"
- name: install aws cli
command: pip install awscli
when: inside_aws
- name: get the list of tags
register: tag_list
when: inside_aws
- name: create facts out of the tags
set_fact:
"{{'ec2_tag_' + tag.Key.replace(':','_').replace('-','_') }}": "{{ tag.Value }}"
with_items: "{{ (tag_list.stdout | from_json)['Tags'] }}"
when: inside_aws
loop_control:
loop_var: tag
label: "{{ tag.Key }}"
- name: remove awscli tools
command: pip uninstall -y awscli
when: inside_aws
kind regards
Pshem