我正在尝试使用aws ec2
命令在AWS中查找设备和主机的最新快照。我从aws ec2 describe-snapshots
得到以下输出。
如您所见,我可以为同一主机(请参见带有密钥主机名和设备名的标签)和设备提供多个快照。开始时间不同。
{
"Snapshots": [
{
"Description": "My desc.",
"Encrypted": false,
"VolumeId": "vol-aaa",
"State": "completed",
"VolumeSize": 8,
"StartTime": "2018-02-02T19:27:56.000Z",
"Progress": "100%",
"OwnerId": "5674567",
"SnapshotId": "snap-xxx"
},
{
"Description": "host1.domain.com - sdc",
"Tags": [
{
"Value": "SNAP1",
"Key": "Name"
},
{
"Value": "sdc",
"Key": "devicename"
},
{
"Value": "host1.domain.com",
"Key": "hostname"
}
],
"Encrypted": false,
"VolumeId": "vol-xxx",
"State": "completed",
"VolumeSize": 140,
"StartTime": "2018-09-21T08:39:58.000Z",
"Progress": "100%",
"OwnerId": "345634563456",
"SnapshotId": "snap-xxx"
},
{
"Description": "host1.domain.com - sdc",
"Tags": [
{
"Value": "SNAP2",
"Key": "Name"
},
{
"Value": "sdc",
"Key": "devicename"
},
{
"Value": "host1.domain.com",
"Key": "hostname"
}
],
"Encrypted": false,
"VolumeId": "vol-xxx",
"State": "completed",
"VolumeSize": 140,
"StartTime": "2018-09-22T08:39:58.000Z",
"Progress": "100%",
"OwnerId": "345634563456",
"SnapshotId": "snap-xxx"
}
}
我如何在Ansible中查询此JSON以获取主机名和设备的最新快照?我通常不会这样做,所以在查询语法上很费劲。
直到现在我有以下内容。
- shell: "aws ec2 describe-snapshots"
register: snap
delegate_to: localhost
- debug:
msg: "{{ snap.stdout | from_json | json_query(query) }}"
vars:
query: "Snapshots[].Tags[?Key=='hostname'].Value"
但是,如何选择Tag.Value等于Key为“主机名”的某个值的所有快照元素?然后如何从列表中选择最新的?
答案 0 :(得分:2)
根据the fine manual,JMESPath支持嵌套的括号说明符表达式:
vars:
snap: |
{
"Snapshots": [
{"Id": "aaa", "Tags": [{"Key": "hostname", "Value": "alpha"}]},
{"Id": "bbb", "Tags": [{"Key": "hostname", "Value": "beta"}]}
]
}
jq: "Snapshots[? Tags[? Key=='hostname' && Value=='alpha']].Id"
tasks:
- debug:
msg: "{{ snap | from_json | json_query(jq) }}"
对于“用于特定主机”部分,vars: query:
就像其他所有可输入的字符串一样,也要接受jinja2插值,因此:
vars:
query: ... [? Value=='{{ the_hostname }}' ] ...
请小心确保正确地转义了该值-主机名可能不会出现问题,但我的意思通常是 。
然后,对于“列表中的最新”部分,ISO8601具有按字母顺序排序的非常令人愉悦的附带好处,因此:
vars:
jq: "Snapshots[? Tags[? Key=='hostname' && Value=='alpha']]"
tasks:
- debug:
msg: "{{ snap | from_json | json_query(jq) | sort(attribute='StartTime', reverse=True) }}"