我有一个简单的任务但却无法完成。
我有一个返回 AWS EC2 实例配置的剧本。我只需要打印(显示)私有 IP 地址。
这是我的剧本
---
- hosts: local
connection: local
gather_facts: false
become: yes
become_method: enable
tasks:
- name: gather-info-ec2
community.aws.ec2_instance_info:
instance_ids:
- i-XXXXXAAAAAA
register: ec2
- debug: msg="{{ ec2.instances.network_interfaces.private_ip_address }}"
当我这样运行它时,出现以下错误。
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'network_interfaces'\n\nThe error appears to be in '/etc/ansible/playbooks/AWSLinuxMigration/gather_ec2_info.yaml': line 16, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug: msg=\"{{ ec2.instances.network_interfaces.private_ip_address }}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
当我在没有 DEBUG 部分的情况下执行它并且使用-vvv它显示以下结果。如何提取并打印此地址?我将其缩短了一点,但你应该明白了
ok: [localhost] => {
"msg": {
"changed": false,
"failed": false,
"instances": [
{
"ami_launch_index": 0,
"architecture": "x86_64",
"block_device_mappings": [
{
"device_name": "/dev/xvda",
"ebs": {
"attach_time": "2020-04-15T16:11:19+00:00",
"delete_on_termination": true,
"status": "attached",
"volume_id": "xxxxxx"
}
}
],
"capacity_reservation_specification": {
"capacity_reservation_preference": "open"
},
"client_token": "",
"cpu_options": {
"core_count": 1,
"threads_per_core": 2
},
"ebs_optimized": true,
"ena_support": true,
"enclave_options": {
"enabled": false
},
"hibernation_options": {
"configured": false
},
"hypervisor": "xen",
"iam_instance_profile": {
"arn": "xxxxxx",
"id": "xxxxxx"
},
"image_id": "xxxxx",
"instance_id": "xxxxx",
"instance_type": "t3.medium",
"key_name": "xxxxx",
"launch_time": "2021-04-21T00:01:25+00:00",
"metadata_options": {
"http_endpoint": "enabled",
"http_put_response_hop_limit": 1,
"http_tokens": "optional",
"state": "applied"
},
"monitoring": {
"state": "disabled"
},
"network_interfaces": [
{
"association": {
"ip_owner_id": "xxxx",
"public_dns_name": "xxxxx",
"public_ip": "xxxx"
},
"attachment": {
"attach_time": "2020-04-15T16:11:18+00:00",
"attachment_id": "xxxxx",
"delete_on_termination": true,
"device_index": 0,
"network_card_index": 0,
"status": "attached"
},
"description": "Primary network interface",
"groups": [
{
"group_id": "xxxxx",
"group_name": "xxxxx"
}
],
"interface_type": "interface",
"ipv6_addresses": [],
"mac_address": "xxxxx",
"network_interface_id": "xxxx",
"owner_id": "xxxxx",
"private_dns_name": "ip-10-0-1-161.ec2.internal",
"private_ip_address": "10.0.1.161",
"private_ip_addresses": [
{
"association": {
"ip_owner_id": "xxxxx",
"public_dns_name": "xxxx.compute-1.amazonaws.com",
"public_ip": "2.2.2.2"
},
"primary": true,
"private_dns_name": "ip-333333.ec2.internal",
"private_ip_address": "1.1.1.1."
}
],
"source_dest_check": true,
"status": "in-use"
}
]
]
}
}
答案1
两种属性实例和网络接口是列表。使用json_查询例如
- debug:
msg: "{{ ec2.instances|
json_query('[].network_interfaces[].private_ip_address') }}"
结果也将是一个列表,因为可能有更多的网络接口和地址。对于你的情况,你可能想选择第一个,例如
- debug:
msg: "{{ ec2.instances|
json_query('[].network_interfaces[].private_ip_address')|
first }}"