From ce52f347423c7e7d45ae7f6c462fecac839bc090 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Tue, 14 Jan 2020 11:14:36 +1100 Subject: [PATCH] DCD-890: Simplify tagging logic. --- roles/aws_common/tasks/write-tags.yml | 65 +++++++++++---------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/roles/aws_common/tasks/write-tags.yml b/roles/aws_common/tasks/write-tags.yml index 243d338..2c0fc47 100644 --- a/roles/aws_common/tasks/write-tags.yml +++ b/roles/aws_common/tasks/write-tags.yml @@ -9,50 +9,37 @@ - name: Retrieve autoscaling group set_fact: - ec2_autoscaling_group: "{{ ec2_tags.tags['aws:autoscaling:groupName']|default('') }}" + ec2_autoscaling_group: "{{ ec2_tags.tags['aws:autoscaling:groupName'] | default('') }}" - block: - # We're in an ASG, lookup the tags... - - name: Get AutoscalingGroup tags - command: "aws autoscaling - describe-tags - --region {{ ansible_ec2_placement_region }} - --filters Name=auto-scaling-group,Values='{{ ec2_autoscaling_group }}'" - register: asg_tags_out + # No existing timestamp, so this is a first run. Persist some metadata into the ASG. + - name: Fetch the git revision for this repo + command: + cmd: git rev-parse HEAD + register: git_out - - name: Parse and transform the AWS tags into a lookup table + - name: Setup the new ASG tags set_fact: - asg_tags: "{{ (asg_tags_out.stdout | from_json).Tags | items2dict(key_name='Key', value_name='Value') }}" + deployment_firstrun_meta: + - ResourceType: "auto-scaling-group" + ResourceId: "{{ ec2_autoscaling_group }}" + PropagateAtLaunch: true + Key: "atl:deployment:commit" + Value: "{{ git_out.stdout }}" - - block: - # No existing timestamp, so this is a first run. Persist some metadata into the ASG. - - name: Fetch the git revision for this repo - command: - cmd: git rev-parse HEAD - register: git_out + - ResourceType: "auto-scaling-group" + ResourceId: "{{ ec2_autoscaling_group }}" + PropagateAtLaunch: true + Key: "atl:deployment:first-run" + Value: "{{ ansible_date_time.iso8601 }}" - - name: Setup the new ASG tags - set_fact: - deployment_firstrun_meta: - - ResourceType: "auto-scaling-group" - ResourceId: "{{ ec2_autoscaling_group }}" - PropagateAtLaunch: true - Key: "atl:deployment:commit" - Value: "{{ git_out.stdout }}" + - name: Set the first-run tags on the ASG + command: "aws autoscaling + create-or-update-tags + --region {{ ansible_ec2_placement_region }} + --tags '{{ deployment_firstrun_meta | to_json }}'" - - ResourceType: "auto-scaling-group" - ResourceId: "{{ ec2_autoscaling_group }}" - PropagateAtLaunch: true - Key: "atl:deployment:first-run" - Value: "{{ ansible_date_time.iso8601 }}" - - - name: Set the first-run tags on the ASG - command: "aws autoscaling - create-or-update-tags - --region {{ ansible_ec2_placement_region }} - --tags '{{ deployment_firstrun_meta | to_json }}'" - - when: asg_tags['atl:deployment:first-run'] is not defined - - when: ec2_autoscaling_group != '' + when: + - ec2_autoscaling_group != '' + - ec2_instance_tags.tags['atl:deployment:first-run'] is not defined ignore_errors: true