安塞尔2.7.8
我的目标是: 从另一个数据库的最新现有快照创建新的RDS数据库。我发现了一个类似的问题(Ansible:从上一个快照创建新的RDS DB …
Jinja2就是你所需要的。
用一个 selectattr 过滤以仅选择可用的快照。
selectattr
然后 sort 过滤以按结果排序 snapshot_create_time
sort
snapshot_create_time
最后 last 过滤以选择最后一个结果并在中使用 rds 模块。
last
rds
这里有很多很好的信息 Jinja2文档
- 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 }}"