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

查看:223
本文介绍了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

推荐答案

您只需要在restore命令即可. rel ="nofollow"> 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 }}"

还有一些额外的技巧:

  • 我在播放开始时将connection设置为local,因此当与hosts: localhost结合使用时,所有任务都将是本地任务.
  • 我从Ansible主机自身的事实(从collect_facts出发,仅针对localhost)构建了一个类似于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天全站免登陆