Promote命令似乎不适用于我正在使用的Ansible版本。 所以我试图创建一个新数据库作为现有数据库的副本,并在使其成为主数据库后删除源数据库。
我试图这样做:
但现在我在想这个:
那个剧本怎么样?
我的剧本:
- hosts: localhost
vars:
source_db_name: "{{ SOURCE_DB }}" # stagingdb
new_db_name: "{{ NEW_DB }}" # stagingdb2
tasks:
- name: Make RDS replica
local_action:
module: rds
region: us-east-1
command: replicate
instance_name : "{{ new_db_name }}"
source_instance: "{{ source_db_name }}"
wait: yes
wait_timeout: 900 # wait 15 minutes
# Notice - not working [Ansible bug]
- name: Promote RDS replica
local_action:
module: rds
region: us-east-1
command: promote
instance_name: "{{ new_db_name }}" # stagingdb2
backup_retention: 0
wait: yes
wait_timeout: 300
- name: Delete source db
local_action:
command: delete
instance_name: "{{ source_db_name }}"
region: us-east-1
答案 0 :(得分:4)
您只需使用RDS module中的restore
命令。
你的剧本看起来像是:
- hosts: localhost
connection: local
gather_facts: yes
vars:
date: "{{ ansible_date_time.year }}-{{ ansible_date_time.month }}-{{ ansible_date_time.day }}-{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }}"
source_db_name: "{{ SOURCE_DB }}" # stagingdb
new_db_name: "{{ NEW_DB }}" # stagingdb2
snapshot_name: "snapshot-{{ source_db_name }}--{{ date }}"
tasks:
- name : Take RDS snapshot
rds :
command : snapshot
instance_name : "{{ source_db_name }}"
snapshot : "{{ snapshot_name }}"
wait : yes
register: snapshot_out
- name : get facts
rds :
command : facts
instance_name : "{{ instance_name }}"
register: db_facts
- name : Restore RDS from snapshot
rds :
command : restore
instance_name : "{{ new_db_name }}"
snapshot : "{{ snapshot_name }}"
instance_type : "{{ db_facts.instance.instance_type }}"
subnet : primary # Unfortunately this isn't returned by db_facts
wait : yes
wait_timeout : 1200
- name : Delete source db
rds :
command : delete
instance_name : "{{ source_db_name }}"
那里有一些额外的技巧:
connection
设置为local
,因此,当与hosts: localhost
结合使用时,所有任务都将是本地任务。YYYY-mm-dd-hh-mm
的日期时间戳(来自gather_facts,它只针对localhost)。然后将其用于快照名称,以确保我们创建它(如果存在一个具有相同名称,则Ansible不会创建另一个快照 - 在这种情况下可能会很糟糕,因为它之前会使用较旧的快照删除源数据库)。instance_type
并删除整个get facts
任务