Score:1

Ansible print debug message result variable

hk flag

I have a simple task that I cannot overcome.

I have a playbook that returns AWS EC2 instance configuration. I need to only print (display) private_ip_address.

Here my playbook

---
- 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 }}"

When I run it like this, I get the following error.

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"}

When I execute it without DEBUG section and with -vvv it displays the below result. How to I extract and print this address? I shortened it a bit, but you get an idea

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"

                    }
                ]
        ]
    }
}
Score:0
br flag

Both attributes instances and network_interfaces are lists. Use json_query, e.g.

    - debug:
        msg: "{{ ec2.instances|
                 json_query('[].network_interfaces[].private_ip_address') }}"

The result will be a list too because there might be more network interfaces and more addresses. In your case, you might want to select the first one, e.g.

    - debug:
        msg: "{{ ec2.instances|
                 json_query('[].network_interfaces[].private_ip_address')|
                 first }}"
housemd avatar
hk flag
great, this worked!!! how do i register this to a variable so i can reuse inside the playbook?
br flag
See [set_fact](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html#ansible-builtin-set-fact-set-host-variable-s-and-fact-s). Then read [How do comments work?](https://meta.stackexchange.com/questions/19756/how-do-comments-work) and "What are comments for, and when shouldn't I comment?" in particular.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.