Skip to content

Commit ba44dd2

Browse files
Dba 791 backups (#473)
* Simply variable reference * Refactor to use template script * Template shell script * Use shell script for checking catalog connection * Extra condition to copy script * Commit changes made by code formatters --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 4c20f62 commit ba44dd2

7 files changed

+96
-63
lines changed

playbooks/oracle_backup/backup.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@
162162
async: "{{ allowable_duration|default(28800) }}"
163163
poll: 0
164164
environment:
165-
ASSUME_ROLE_NAME: "{{ secretsmanager_passwords['catalog'].assume_role_name | default() }}"
166-
SECRET_ACCOUNT_ID: "{{ account_ids[secretsmanager_passwords['catalog'].account_name] | default() }}"
165+
ASSUME_ROLE_NAME: "{{ assume_role_name | default() }}"
166+
SECRET_ACCOUNT_ID: "{{ account_ids[account_name] | default() }}"
167167
SECRET: "{{ secretsmanager_passwords['catalog'].secret | default() }}"
168168
register: backup_cmd_output
169169

playbooks/oracle_backup/check_defunct_backups.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
- name: Set Environment Variables Required For Shell And Script Modules
3939
set_fact:
40-
assume_role_name: "{{ secretsmanager_passwords['catalog'].assume_role_name }}"
41-
secret_account_id: "{{ account_ids[secretsmanager_passwords['catalog'].account_name] }}"
40+
assume_role_name: "{{ assume_role_name }}"
41+
secret_account_id: "{{ account_ids[account_name] }}"
4242
secret: "{{ secretsmanager_passwords['catalog'].secret }}"
4343

4444
# As we do not have a tnsnames.ora file on the standby we pick up the tnsnames definition for the catalog from the primary

playbooks/oracle_backup/get_facts.yml

+52-46
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
- name: Set Secrets Dictionary
22
set_fact:
3+
account_name: "hmpps-oem-{{ aws_environment }}"
4+
assume_role_name: "EC2OracleEnterpriseManagementSecretsRole"
35
secretsmanager_passwords:
46
emrep:
5-
account_name: "hmpps-oem-{{ aws_environment }}"
6-
assume_role_name: "EC2OracleEnterpriseManagementSecretsRole"
77
secret: "/oracle/database/EMREP/shared-passwords"
88
users:
99
- sysman:
1010
catalog:
11-
account_name: "hmpps-oem-{{ aws_environment }}"
12-
assume_role_name: "EC2OracleEnterpriseManagementSecretsRole"
1311
secret: "/oracle/database/{{ catalog }}/shared-passwords"
1412
users:
1513
- rcvcatowner:
@@ -18,50 +16,58 @@
1816
set_fact:
1917
account_ids: "{{ lookup('aws_ssm', 'account_ids', region='eu-west-2') }}"
2018

21-
- name: Get OEM secrets
22-
import_role:
23-
name: secretsmanager-passwords
24-
vars:
25-
secretsmanager_passwords: "{{ secretsmanager_passwords }}"
19+
- name: Copy Get Facts Script On Conditions
20+
when: (tnsnames_entry_exists is defined and not tnsnames_entry_exists) or (get_slack_channel is defined) or (copy_script | default(false) | bool)
21+
block:
22+
- name: Copy Get Facts Script
23+
template:
24+
src: get_facts_script.sh.j2
25+
dest: /u02/stage/get_facts_script.sh
26+
mode: "0700"
2627

27-
- name: Set password facts
28-
set_fact:
29-
sysman_password: "{{ secretsmanager_passwords_dict['emrep'].passwords['sysman'] }}"
30-
rcvcatowner_password: "{{ secretsmanager_passwords_dict['catalog'].passwords['rcvcatowner'] }}"
31-
getslacktoken: "{{ secretsmanager_passwords_dict['emrep'].passwords['slack_token'] }}"
28+
- name: Catalog Host Name
29+
when:
30+
- tnsnames_entry_exists is defined
31+
- not tnsnames_entry_exists
32+
block:
33+
- name: Set Emcli Catalog Host Name Command
34+
set_fact:
35+
emcli_commands: |
36+
{{ emcli }} get_targets -noheader -targets="%:oracle_oms" | awk '{print $NF}' | cut -d: -f1
3237
33-
- name: Catalog Host Name
34-
when:
35-
- tnsnames_entry_exists is defined
36-
- not tnsnames_entry_exists
37-
block:
38-
- name: Get Catalog Host Name
39-
shell: |
40-
. ~/.bash_profile
41-
export PATH=$PATH:/u01/app/oracle/product/oem-agent/agent_{{ OEM_AGENT_VERSION }}/oracle_common/jdk/jre/bin
42-
{{ emcli }} sync 1>/dev/null 2>&1 || ( {{ emcli }} login -username=sysman -password=${SYSMAN_PASSWORD} -force && {{ emcli }} sync ) 1>/dev/null 2>&1
43-
{{ emcli }} get_targets -noheader -targets="%:oracle_oms" | awk '{print $NF}' | cut -d: -f1
44-
environment:
45-
SYSMAN_PASSWORD: "{{ sysman_password }}"
46-
register: getcataloghostname
38+
- name: Get Catalog Host Name
39+
ansible.builtin.command: >
40+
/u02/stage/get_facts_script.sh
41+
environment:
42+
ACCOUNT_NAME: "{{ account_name }}"
43+
ASSUME_ROLE_NAME: "{{ assume_role_name }}"
44+
EMCLI_COMMANDS: "{{ emcli_commands }}"
45+
register: getcataloghostname
46+
changed_when: false
4747

48-
- name: Set Catalog Host Name
49-
set_fact:
50-
catalog_hostname: "{{ getcataloghostname.stdout }}"
48+
- name: Set Catalog Host Name
49+
set_fact:
50+
catalog_hostname: "{{ getcataloghostname.stdout }}"
5151

52-
- name: Slack Channel
53-
when: get_slack_channel is defined
54-
block:
55-
- name: Get Slack Channel
56-
shell: |
57-
. ~/.bash_profile
58-
export PATH=$PATH:/u01/app/oracle/product/oem-agent/agent_{{ OEM_AGENT_VERSION }}/oracle_common/jdk/jre/bin
59-
{{ emcli }} sync 1>/dev/null 2>&1 || ( {{ emcli }} login -username=sysman -password=${SYSMAN_PASSWORD} -force && {{ emcli }} sync ) 1>/dev/null 2>&1
60-
{{ emcli }} list -resource=TargetProperties -search="TARGET_NAME='{{ database_sid }}'" -search="PROPERTY_NAME='orcl_gtp_contact'" -columns="PROPERTY_VALUE" -colsize="PROPERTY_VALUE:30" -noheader -format="name:script"
61-
environment:
62-
SYSMAN_PASSWORD: "{{ sysman_password }}"
63-
register: getslackchannel
52+
- name: Slack Channel
53+
when: get_slack_channel is defined
54+
block:
55+
- name: Set Emcli Slack Channel Command
56+
set_fact:
57+
emcli_commands: |
58+
{{ emcli }} list -resource=TargetProperties -search="TARGET_NAME='{{ database_sid }}'" -search="PROPERTY_NAME='orcl_gtp_contact'" -columns="PROPERTY_VALUE" -colsize="PROPERTY_VALUE:30" -noheader -format="name:script"
59+
60+
- name: Get Slack Channel
61+
ansible.builtin.command: >
62+
/u02/stage/get_facts_script.sh
63+
environment:
64+
ACCOUNT_NAME: "{{ account_name }}"
65+
ASSUME_ROLE_NAME: "{{ assume_role_name }}"
66+
CATALOG_SECRET: "{{ secretsmanager_passwords['catalog'].secret }}"
67+
EMCLI_COMMANDS: "{{ emcli_commands }}"
68+
register: getslackchannel
69+
changed_when: false
6470

65-
- name: Setup Slack Configuration
66-
set_fact:
67-
slack_channel: "{{ getslackchannel.stdout }}"
71+
- name: Setup Slack Configuration
72+
set_fact:
73+
slack_channel: "{{ getslackchannel.stdout }}"

playbooks/oracle_backup/setup-catalog-tnsnames.yml

+10-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
- name: Get Passwords
2828
include_tasks: get_facts.yml
29+
vars:
30+
copy_script: true
2931

3032
- name: Add catalog connect identifier to {{ oracle_home.stdout }}/network/admin/tnsnames.ora
3133
blockinfile:
@@ -51,16 +53,15 @@
5153
when: not tnsnames_entry_exists
5254

5355
- name: Attempt to connect to the catalog as rman
54-
shell:
55-
cmd: |
56-
. ~/.bash_profile
57-
sqlplus -s /nolog<< EOF
58-
whenever sqlerror exit failure
59-
connect rcvcatowner/{{ rcvcatowner_password }}@{{ catalog }}
60-
EOF
61-
changed_when: false
56+
ansible.builtin.command: >
57+
/u02/stage/get_facts_script.sh
58+
environment:
59+
STEP: CONNECT_CATALOG
60+
ACCOUNT_NAME: "{{ account_name }}"
61+
ASSUME_ROLE_NAME: "{{ assume_role_name }}"
62+
CATALOG_SECRET: "{{ secretsmanager_passwords['catalog'].secret }}"
6263
register: rman_connect
63-
no_log: true
64+
changed_when: false
6465

6566
- name: Display connection result
6667
debug:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
. ~/.bash_profile
4+
5+
OEM_ACCOUNT_ID=$(aws ssm get-parameters --with-decryption --name account_ids | jq --arg ACCOUNT_NAME ${ACCOUNT_NAME} -r 'with_entries(if (.key|test($ACCOUNT_NAME)) then ( {key: .key, value: .value}) else empty end)' | jq -r 'to_entries|.[0].value')
6+
OEM_SECRET_ARN="arn:aws:secretsmanager:eu-west-2:${OEM_ACCOUNT_ID}:secret:${OEM_SECRET}"
7+
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
8+
OEM_ROLE_ARN="arn:aws:iam::${ACCOUNT_ID}:role/${ASSUME_ROLE_NAME}"
9+
CREDS=$(aws sts assume-role --role-arn "${OEM_ROLE_ARN}" --role-session-name "from-ansible" --output text --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]")
10+
export AWS_ACCESS_KEY_ID=$(echo "${CREDS}" | tail -1 | cut -f1)
11+
export AWS_SECRET_ACCESS_KEY=$(echo "${CREDS}" | tail -1 | cut -f2)
12+
export AWS_SESSION_TOKEN=$(echo "${CREDS}" | tail -1 | cut -f3)
13+
14+
SYSMAN_PASSWORD=$(aws secretsmanager get-secret-value --secret-id "${OEM_SECRET_ARN}" --query SecretString --output text | jq -r .sysman)
15+
export PATH=$PATH:/u01/app/oracle/product/oem-agent/agent_{{ OEM_AGENT_VERSION }}/oracle_common/jdk/jre/bin
16+
{{ emcli }} sync 1>/dev/null 2>&1 || ( {{ emcli }} login -username=sysman -password=${SYSMAN_PASSWORD} -force && {{ emcli }} sync ) 1>/dev/null 2>&1
17+
18+
[[ ! -z "${EMCLI_COMMANDS}" ]] && eval ${EMCLI_COMMANDS}
19+
if [[ "${STEP}" == "CONNECT_CATALOG" ]]
20+
then
21+
RCVCATOWNER_PASSWORD=$(aws secretsmanager get-secret-value --secret-id "arn:aws:secretsmanager:eu-west-2:${OEM_ACCOUNT_ID}:secret:${CATALOG_SECRET}" --query SecretString --output text | jq -r .rcvcatowner)
22+
sqlplus -s /nolog << EOF
23+
whenever sqlerror exit failure
24+
connect rcvcatowner/${RCVCATOWNER_PASSWORD}@{{ catalog }}
25+
EOF
26+
fi

playbooks/oracle_backup/validate_backup.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
- name: Set Environment Variables Required For Shell And Script Modules
2222
set_fact:
23-
assume_role_name: "{{ secretsmanager_passwords['catalog'].assume_role_name }}"
24-
secret_account_id: "{{ account_ids[secretsmanager_passwords['catalog'].account_name] }}"
23+
assume_role_name: "{{ assume_role_name }}"
24+
secret_account_id: "{{ account_ids[account_name] }}"
2525
secret: "{{ secretsmanager_passwords['catalog'].secret }}"
2626

2727
# As we do not have a tnsnames.ora file on the standby we pick up the tnsnames definition for the catalog from the primary

playbooks/oracle_backup/validate_chunks.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848

4949
- name: Set Environment Variables Required For Shell And Script Modules
5050
set_fact:
51-
assume_role_name: "{{ secretsmanager_passwords['catalog'].assume_role_name }}"
52-
secret_account_id: "{{ account_ids[secretsmanager_passwords['catalog'].account_name] }}"
51+
assume_role_name: "{{ assume_role_name }}"
52+
secret_account_id: "{{ account_ids[account_name] }}"
5353
secret: "{{ secretsmanager_passwords['catalog'].secret }}"
5454

5555
- name: Get DBID

0 commit comments

Comments
 (0)