Ansible
Ansible 列印調試消息結果變數
我有一個簡單的任務,我無法克服。
我有一個返回 AWS EC2 實例配置的劇本。我只需要列印(顯示)private_ip_address。
這是我的劇本
--- - 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" } ] ] } }
屬性實例和網路介面都是列表。使用json_query,例如
- debug: msg: "{{ ec2.instances| json_query('[].network_interfaces[].private_ip_address') }}"
結果也將是一個列表,因為可能有更多的網路介面和更多的地址。在您的情況下,您可能想要選擇第一個,例如
- debug: msg: "{{ ec2.instances| json_query('[].network_interfaces[].private_ip_address')| first }}"