|
79 | 79 | {{ aws_cmd }} ecr put-lifecycle-policy |
80 | 80 | --repository-name {{ service_id }}_{{ item }} |
81 | 81 | --lifecycle-policy-text file://{{ playbook_dir }}/../ecr-lifecycle/ecr_lifecycle.json |
| 82 | + when: |
| 83 | + - existing_policy_json != desired_policy_json- name: Get existing lifecycle policy JSON for {{ service_id }}_{{ item }} |
| 84 | + ansible.builtin.command: > |
| 85 | + {{ aws_cmd }} ecr get-lifecycle-policy |
| 86 | + --repository-name {{ service_id }}_{{ item }} |
| 87 | + --query 'lifecyclePolicyText' |
| 88 | + --output text |
| 89 | + register: existing_policy_raw |
| 90 | + failed_when: false |
| 91 | + changed_when: false |
| 92 | + |
| 93 | +- name: Parse existing lifecycle policy JSON if present |
| 94 | + set_fact: |
| 95 | + existing_policy_json: "{{ existing_policy_raw.stdout | from_json }}" |
| 96 | + when: |
| 97 | + - existing_policy_raw.stdout is defined |
| 98 | + - existing_policy_raw.stdout != "" |
| 99 | + - existing_policy_raw.stdout != "None" |
| 100 | + - existing_policy_raw.stdout != "null" |
| 101 | + |
| 102 | +- name: Ensure existing_policy_json always exists |
| 103 | + set_fact: |
| 104 | + existing_policy_json: {} |
| 105 | + when: existing_policy_json is not defined |
| 106 | + |
| 107 | +- name: Read lifecycle policy from the shared file |
| 108 | + ansible.builtin.slurp: |
| 109 | + src: "{{ playbook_dir }}/ecr-lifecycle/ecr_lifecycle.json" |
| 110 | + register: desired_policy_raw |
| 111 | + |
| 112 | +- name: Debug raw slurp output |
| 113 | + debug: |
| 114 | + var: desired_policy_raw |
| 115 | + |
| 116 | +- name: Show decoded lifecycle policy content |
| 117 | + debug: |
| 118 | + msg: "{{ desired_policy_raw.content | b64decode }}" |
| 119 | + |
| 120 | +- name: Decode lifecycle policy file |
| 121 | + set_fact: |
| 122 | + desired_policy_json: "{{ desired_policy_raw.content | b64decode | from_json }}" |
| 123 | + |
| 124 | +- name: Apply lifecycle policy to ecr {{ service_id }}_{{ item }} if different |
| 125 | + ansible.builtin.command: > |
| 126 | + {{ aws_cmd }} ecr put-lifecycle-policy |
| 127 | + --repository-name {{ service_id }}_{{ item }} |
| 128 | + --lifecycle-policy-text file://{{ playbook_dir }}/ecr-lifecycle/ecr_lifecycle.json |
82 | 129 | when: |
83 | 130 | - existing_policy_json != desired_policy_json |
| 131 | + |
0 commit comments