Ansible - 从最新的现有快照创建 RDS 实例

Ansible - Create RDS Instance from most recent existing snapshot

ansible 2.7.8

我的目标是:

Create a new RDS database, from the most recent existing snapshot of another database.

我在 () 发现了一个类似的问题,但是该解决方案需要创建一个新的快照。我想从最近的已创建 快照恢复。

这就是我到目前为止所做的...用于收集当前快照信息的游戏。我正在使用 rds_snapshot_facts 模块 (https://docs.ansible.com/ansible/latest/modules/rds_snapshot_facts_module.html)

  - name : get snap facts
    rds_snapshot_facts  :
      db_instance_identifier: "{{ source_db_name }}"
      region        : "{{ region }}"
      aws_access_key: "{{ access_key }}"
      aws_secret_key: "{{ secret_key }}"

运行成功,这里是上面播放返回的示例数据结构:

"snapshots": [
    {
        "allocated_storage": 500,
        "availability_zone": "us-east-1a",
        "db_instance_identifier": "pg-test-01",
        "db_snapshot_arn": "arn:aws:rds:us-east-1:111111111111111:snapshot:rds:pg-test-01-2019-03-01-06-22",
        "db_snapshot_identifier": "rds:pg-test-01-2019-03-01-06-22",
        "dbi_resource_id": "db-hidden",
        "encrypted": false,
        "engine": "postgres",
        "engine_version": "10.6",
        "iam_database_authentication_enabled": false,
        "instance_create_time": "2019-02-27T05:08:47.480000+00:00",
        "license_model": "postgresql-license",
        "master_username": "postgres",
        "option_group_name": "default:postgres-10",
        "percent_progress": 100,
        "port": 5432,
        "processor_features": [],
        "snapshot_create_time": "2019-03-01T06:22:58.771000+00:00",
        "snapshot_type": "automated",
        "status": "available",
        "storage_type": "gp2",
        "tags": {},
        "vpc_id": "vpc-hidden"
    },
    {
        "allocated_storage": 500,
        "availability_zone": "us-east-1a",
        "db_instance_identifier": "pg-test-01",
        "db_snapshot_arn": "arn:aws:rds:us-east-1:111111111111111:snapshot:rds:pg-test-01-2019-03-02-06-32",
        "db_snapshot_identifier": "rds:pg-test-01-2019-03-02-06-32",
        "dbi_resource_id": "db-hidden",
        "encrypted": false,
        "engine": "postgres",
        "engine_version": "10.6",
        "iam_database_authentication_enabled": false,
        "instance_create_time": "2019-02-27T05:08:47.480000+00:00",
        "license_model": "postgresql-license",
        "master_username": "postgres",
        "option_group_name": "default:postgres-10",
        "percent_progress": 100,
        "port": 5432,
        "processor_features": [],
        "snapshot_create_time": "2019-03-02T06:32:42.325000+00:00",
        "snapshot_type": "automated",
        "status": "available",
        "storage_type": "gp2",
        "tags": {},
        "vpc_id": "vpc-hidden"
    },
    {
        "allocated_storage": 500,
        "availability_zone": "us-east-1a",
        "db_instance_identifier": "pg-test-01",
        "db_snapshot_arn": "arn:aws:rds:us-east-1:111111111111111:snapshot:rds:pg-test-01-2019-03-03-06-33",
        "db_snapshot_identifier": "rds:pg-test-01-2019-03-03-06-33",
        "dbi_resource_id": "db-hidden",
        "encrypted": false,
        "engine": "postgres",
        "engine_version": "10.6",
        "iam_database_authentication_enabled": false,
        "instance_create_time": "2019-02-27T05:08:47.480000+00:00",
        "license_model": "postgresql-license",
        "master_username": "postgres",
        "option_group_name": "default:postgres-10",
        "percent_progress": 100,
        "port": 5432,
        "processor_features": [],
        "snapshot_create_time": "2019-03-03T06:33:06.463000+00:00",
        "snapshot_type": "automated",
        "status": "available",
        "storage_type": "gp2",
        "tags": {},
        "vpc_id": "vpc-hidden"
    },
    {
        "allocated_storage": 500,
        "availability_zone": "us-east-1a",
        "db_instance_identifier": "pg-test-01",
        "db_snapshot_arn": "arn:aws:rds:us-east-1:111111111111111:snapshot:rds:pg-test-01-2019-03-04-06-32",
        "db_snapshot_identifier": "rds:pg-test-01-2019-03-04-06-32",
        "dbi_resource_id": "db-hidden",
        "encrypted": false,
        "engine": "postgres",
        "engine_version": "10.6",
        "iam_database_authentication_enabled": false,
        "instance_create_time": "2019-02-27T05:08:47.480000+00:00",
        "license_model": "postgresql-license",
        "master_username": "postgres",
        "option_group_name": "default:postgres-10",
        "percent_progress": 100,
        "port": 5432,
        "processor_features": [],
        "snapshot_create_time": "2019-03-04T06:32:30.227000+00:00",
        "snapshot_type": "automated",
        "status": "available",
        "storage_type": "gp2",
        "tags": {},
        "vpc_id": "vpc-hidden"
    },
    {
        "allocated_storage": 500,
        "availability_zone": "us-east-1a",
        "db_instance_identifier": "pg-test-01",
        "db_snapshot_arn": "arn:aws:rds:us-east-1:111111111111111:snapshot:rds:pg-test-01-2019-03-05-06-32",
        "db_snapshot_identifier": "rds:pg-test-01-2019-03-05-06-32",
        "dbi_resource_id": "db-hidden",
        "encrypted": false,
        "engine": "postgres",
        "engine_version": "10.6",
        "iam_database_authentication_enabled": false,
        "instance_create_time": "2019-02-27T05:08:47.480000+00:00",
        "license_model": "postgresql-license",
        "master_username": "postgres",
        "option_group_name": "default:postgres-10",
        "percent_progress": 100,
        "port": 5432,
        "processor_features": [],
        "snapshot_create_time": "2019-03-05T06:32:57.880000+00:00",
        "snapshot_type": "automated",
        "status": "available",
        "storage_type": "gp2",
        "tags": {},
        "vpc_id": "vpc-hidden"
    },
    {
        "allocated_storage": 500,
        "availability_zone": "us-east-1a",
        "db_instance_identifier": "pg-test-01",
        "db_snapshot_arn": "arn:aws:rds:us-east-1:111111111111111:snapshot:snapshot-pg-test-01-2019-03-06-02-22",
        "db_snapshot_identifier": "snapshot-pg-test-01-2019-03-06-02-22",
        "dbi_resource_id": "db-hidden",
        "encrypted": false,
        "engine": "postgres",
        "engine_version": "10.6",
        "iam_database_authentication_enabled": false,
        "instance_create_time": "2019-02-27T05:08:47.480000+00:00",
        "license_model": "postgresql-license",
        "master_username": "postgres",
        "option_group_name": "default:postgres-10",
        "percent_progress": 100,
        "port": 5432,
        "processor_features": [],
        "snapshot_create_time": "2019-03-06T02:23:35.039000+00:00",
        "snapshot_type": "manual",
        "status": "available",
        "storage_type": "gp2",
        "tags": {},
        "vpc_id": "vpc-hidden"
    }
]

所以根据以上信息,我想对它做一些逻辑并创建一个新的数据库。

我已经知道如何从快照创建数据库:

  - name : Restore RDS from snapshot
    rds  :
      command           : restore
      instance_name     : "{{ new_db_name }}"
      snapshot          : "{{ snapshot_name }}"
      instance_type     : "db.t2.medium"
      subnet            : my_subnet_grp 
      wait              : yes
      wait_timeout      : 1600
      region             : "{{ region }}"
      aws_access_key: "{{ access_key }}"
      aws_secret_key: "{{ secret_key }}"

但是,在我的例子中,我希望根据收集的事实动态填充变量 "{{ snapshot_name }}"

伪代码逻辑基本上是这样的:

db_snapshot_identifier where status=available and max(snapshot_create_time)

我真的不确定从哪里开始构建 Ansible 操作的逻辑,所以欢迎任何帮助或指出正确的方向 - 谢谢!

Jinja2 正是您所需要的。

使用 selectattr 过滤器仅 select 个可用快照。

然后 sort 筛选结果按 snapshot_create_time

排序

最后 last 过滤器选择最后的结果并在 rds 模块中使用它。

Jinja2 documentation

中有很多有用的信息
- name : get snap facts
  rds_snapshot_facts  :
    db_instance_identifier: "{{ source_db_name }}"
    region        : "{{ region }}"
    aws_access_key: "{{ access_key }}"
    aws_secret_key: "{{ secret_key }}"
  register: snapshot_facts

- name: get latest snapshot facts
  set_fact:
    latest_snapshot: '{{ snapshot_facts.snapshots | 
                        selectattr("status", "equalto", "available") |
                        sort(attribute="snapshot_create_time") |
                        last }}'

- name : Restore RDS from snapshot
  rds  :
    command           : restore
    instance_name     : "{{ new_db_name }}"
    snapshot          : "{{ latest_snapshot.db_snapshot_identifier }}"
    instance_type     : "db.t2.medium"
    subnet            : my_subnet_grp 
    wait              : yes
    wait_timeout      : 1600
    region            : "{{ region }}"
    aws_access_key: "{{ access_key }}"
    aws_secret_key: "{{ secret_key }}"