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

问题描述 投票:2回答:1

安塞尔2.7.8

我的目标是:

从另一个数据库的最新现有快照创建新的RDS数据库。

我在(Ansible: Create new RDS DB from last snapshot of another DB)发现了一个类似的问题,但该解决方案需要创建一个新的快照。我想从最近创建的快照中恢复。

所以这就是我到目前为止所做的...该剧用于收集当前的快照信息。我正在使用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操作的逻辑,所以欢迎任何帮助或指向正确的方向 - 谢谢!

amazon-web-services ansible ansible-2.x rds ansible-facts
1个回答
1
投票

Jinja2就是你所需要的。

使用selectattr过滤器仅选择可用的快照。

然后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 }}"
© www.soinside.com 2019 - 2024. All rights reserved.