Skip to content

Commit 29175fd

Browse files
authored
Do not delegate snapshot action (#496)
The role on the ansible controller does not have privileges to run a backup, so need to use the role on the instance instead
1 parent 2a424d5 commit 29175fd

File tree

1 file changed

+61
-65
lines changed

1 file changed

+61
-65
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,69 @@
1-
- name: Create AWS Snapshot of EC2 Instance
2-
delegate_to: localhost
3-
become: no
4-
block:
5-
- name: Get the current caller identity information
6-
amazon.aws.aws_caller_info:
7-
register: caller_info
8-
changed_when: false
1+
- name: Get the current caller identity information
2+
amazon.aws.aws_caller_info:
3+
register: caller_info
4+
changed_when: false
95

10-
- name: Get Database Type
11-
set_fact:
12-
database_type: "{{ group_names | select('match','.*_dbs') | list | first | regex_replace('^.*_(.*)_dbs', '\\1') }}"
6+
- name: Get Database Type
7+
set_fact:
8+
database_type: "{{ group_names | select('match','.*_dbs') | list | first | regex_replace('^.*_(.*)_dbs', '\\1') }}"
139

14-
- name: Setup Backup Vault Name
15-
set_fact:
16-
backup_vault_name: "{{ simple_environment_name }}{% if database_type != 'delius' %}-{{ database_type }}{% endif %}-db-oracle-backup-vault"
10+
- name: Setup Backup Vault Name
11+
set_fact:
12+
backup_vault_name: "{{ simple_environment_name }}{% if database_type != 'delius' %}-{{ database_type }}{% endif %}-db-oracle-backup-vault"
1713

18-
- name: Get Name of Backup Vault
19-
shell: |
20-
set -o pipefail
21-
aws backup list-backup-vaults --region {{ region }} | jq -r 'first( .BackupVaultList[].BackupVaultName | select (contains("{{ backup_vault_name }}")) ) '
22-
register: get_backup_vault
23-
changed_when: false
24-
args:
25-
executable: /bin/bash
14+
- name: Get Name of Backup Vault
15+
shell: |
16+
set -o pipefail
17+
aws backup list-backup-vaults --region {{ region }} | jq -r 'first( .BackupVaultList[].BackupVaultName | select (contains("{{ backup_vault_name }}")) ) '
18+
register: get_backup_vault
19+
changed_when: false
20+
args:
21+
executable: /bin/bash
2622

27-
- fail:
28-
msg: "No Oracle Backup Vault Available"
29-
when: ( get_backup_vault.stdout_lines | length ) < 1
23+
- fail:
24+
msg: "No Oracle Backup Vault Available"
25+
when: ( get_backup_vault.stdout_lines | length ) < 1
3026

31-
- name: Run Backup Job
32-
shell: |
33-
set -o pipefail
34-
aws backup start-backup-job --backup-vault-name {{ get_backup_vault.stdout }} --resource-arn arn:aws:ec2:{{ region }}:{{ caller_info.account }}:instance/{{ backup_hostname }} --iam-role-arn arn:aws:iam::{{ caller_info.account }}:role/service-role/AWSBackupDefaultServiceRole --lifecycle DeleteAfterDays={{ delete_after_days | default(7) }} --region {{ region }} | jq -r '.BackupJobId'
35-
register: start_backup_job
36-
args:
37-
executable: /bin/bash
27+
- name: Run Backup Job
28+
shell: |
29+
set -o pipefail
30+
aws backup start-backup-job --backup-vault-name {{ get_backup_vault.stdout }} --resource-arn arn:aws:ec2:{{ region }}:{{ caller_info.account }}:instance/{{ backup_hostname }} --iam-role-arn arn:aws:iam::{{ caller_info.account }}:role/service-role/AWSBackupDefaultServiceRole --lifecycle DeleteAfterDays={{ delete_after_days | default(7) }} --region {{ region }} | jq -r '.BackupJobId'
31+
register: start_backup_job
32+
args:
33+
executable: /bin/bash
3834

39-
# If the Backup Job is at Status CREATED then wait until this changes. This simply means it is in a queue to run.
40-
- name: Get Backup Job Status
41-
shell: |
42-
set -o pipefail
43-
aws backup describe-backup-job --backup-job-id "{{ start_backup_job.stdout }}" --region "{{ region }}" | jq -r '.State'
44-
register: get_backup_job_status
45-
until: not get_backup_job_status.stdout is search("CREATED")
46-
retries: 60
47-
delay: 30
48-
changed_when: false
49-
args:
50-
executable: /bin/bash
35+
# If the Backup Job is at Status CREATED then wait until this changes. This simply means it is in a queue to run.
36+
- name: Get Backup Job Status
37+
shell: |
38+
set -o pipefail
39+
aws backup describe-backup-job --backup-job-id "{{ start_backup_job.stdout }}" --region "{{ region }}" | jq -r '.State'
40+
register: get_backup_job_status
41+
until: not get_backup_job_status.stdout is search("CREATED")
42+
retries: 60
43+
delay: 30
44+
changed_when: false
45+
args:
46+
executable: /bin/bash
5147

52-
- name: Get Backup AMI Name
53-
shell: |
54-
set -o pipefail
55-
aws backup describe-backup-job --backup-job-id "{{ start_backup_job.stdout }}" --region "{{ region }}" | jq -r '.RecoveryPointArn | split("/")[1]'
56-
register: get_ami_name
57-
changed_when: false
58-
args:
59-
executable: /bin/bash
48+
- name: Get Backup AMI Name
49+
shell: |
50+
set -o pipefail
51+
aws backup describe-backup-job --backup-job-id "{{ start_backup_job.stdout }}" --region "{{ region }}" | jq -r '.RecoveryPointArn | split("/")[1]'
52+
register: get_ami_name
53+
changed_when: false
54+
args:
55+
executable: /bin/bash
6056

61-
# We do not need to wait for completion of the backup as it is run asynchronously but it should be more than 0% complete on all EBS volumes before we move on.
62-
# Note that tghe "//" below sets the Progress to 0% if no snapshots are found. If Progress of any snapshot is 0% then report the Snapshot as Pending.
63-
- name: Wait for All Snapshots for the Backup to Be Above 0% Complete
64-
shell: |
65-
set -o pipefail
66-
aws ec2 describe-snapshots --filters Name=description,Values="*for {{ get_ami_name.stdout }}" --region={{ region }} | jq '.Snapshots[].Progress // "0%" | match("^0%") | "SNAPSHOT_PENDING"'
67-
register: get_snapshot_status
68-
until: not get_snapshot_status.stdout is search("SNAPSHOT_PENDING")
69-
retries: 60
70-
delay: 30
71-
changed_when: false
72-
args:
73-
executable: /bin/bash
57+
# We do not need to wait for completion of the backup as it is run asynchronously but it should be more than 0% complete on all EBS volumes before we move on.
58+
# Note that tghe "//" below sets the Progress to 0% if no snapshots are found. If Progress of any snapshot is 0% then report the Snapshot as Pending.
59+
- name: Wait for All Snapshots for the Backup to Be Above 0% Complete
60+
shell: |
61+
set -o pipefail
62+
aws ec2 describe-snapshots --filters Name=description,Values="*for {{ get_ami_name.stdout }}" --region={{ region }} | jq '.Snapshots[].Progress // "0%" | match("^0%") | "SNAPSHOT_PENDING"'
63+
register: get_snapshot_status
64+
until: not get_snapshot_status.stdout is search("SNAPSHOT_PENDING")
65+
retries: 60
66+
delay: 30
67+
changed_when: false
68+
args:
69+
executable: /bin/bash

0 commit comments

Comments
 (0)