Ansible:从另一个数据库的最后一个快照创建新的 RDS 数据库 [英] Ansible: Create new RDS DB from last snapshot of another DB

查看:32
本文介绍了Ansible:从另一个数据库的最后一个快照创建新的 RDS 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Promote 命令似乎不适用于我使用的 Ansible 版本.因此,我尝试创建一个新数据库作为现有数据库的副本,并在将其设为主数据库后,删除源数据库.

Promote command does not seem to work on the version of Ansible that I am using. So I am trying to create a new database as a replica of an existing one and after making it master, delete the source database.

我试图这样做:

  1. 制作副本
  2. 提升副本
  3. 删除源数据库

但现在我在想:

  1. 从源数据库上次快照创建新数据库[从一开始就作为主数据库]
  2. 删除源数据库

那个剧本会怎样?

我的剧本:

 - 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

推荐答案

您只需要使用 RDS 模块.

You just need to use the restore command in the RDS module.

您的剧本将如下所示:

 - 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 }}"

这里有一些额外的技巧:

There's a couple of extra tricks in there:

  • 我在播放开始时将 connection 设置为 local,因此,当与 hosts: localhost 结合使用时,所有任务都将是本地的任务.
  • 我根据 Ansible 主机自己的事实(来自gather_facts,它仅针对本地主机)构建了一个看起来像 YYYY-mm-dd-hh-mm 的日期时间戳.然后将其用于快照名称以确保我们创建它(如果存在同名,则 Ansible 不会创建另一个快照 - 在这种情况下可能会很糟糕,因为它会在删除您的快照之前使用旧快照源数据库).
  • 我在任务中获取有关 RDS 实例的事实,并使用它来将实例类型设置为与源数据库相同.如果您不希望那样,那么您可以直接定义 instance_type 并删除整个 get Facts 任务
  • I set connection to local at the start of the play so, when combined with hosts: localhost all of the tasks will be local tasks.
  • I build a date time stamp that looks like YYYY-mm-dd-hh-mm from the Ansible host's own facts (from gather_facts and it only targeting localhost). This is then used for the snapshot name to make sure that we create it (if one exists with the same name then Ansible won't create another snapshot - something that could be bad in this case as it would use an older snapshot before deleting your source database).
  • I fetch the facts about the RDS instance in a task and use that to set the instance type to be the same as the source database. If you don't want that then you can define the instance_type directly and remove the whole get facts task

这篇关于Ansible:从另一个数据库的最后一个快照创建新的 RDS 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆