可使用2.7.8
我的目标是:
从另一个数据库的最新现有快照创建一个新的RDS数据库。
我在(with_indifferent_access
)发现了类似的问题,但是该解决方案需要创建一个新的快照。我想从最新的已创建快照中还原。
所以这是我到目前为止所做的...该剧本用来收集当前的快照信息。我正在使用rds_snapshot_facts
模块(Ansible: Create new RDS DB from last snapshot of another DB)
- 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操作构建该逻辑,因此欢迎任何帮助或指出正确的方向-谢谢!
答案 0 :(得分:1)
Jinja2是您所需要的。
使用selectattr
过滤器仅选择可用的快照。
然后用sort
过滤器按snapshot_create_time
最后,last
过滤器将选择最后一个结果,并将其用于rds
模块中。
- 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 }}"