5

I have the following Ansible task:

tasks:
- name: ensure instances are running
    ec2:
      aws_access_key: "{{aws_access_key}}"
      aws_secret_key: "{{aws_secret_key}}"
      ...
      user_data: "{{ lookup('template', 'userdata.txt.j2') }}"
    register: ec2_result

- debug:
    msg: "{{ ec2_result }}"

- set_fact:
    win_instance_id: "{{ ec2_result | json_query('tagged_instances[*].id') }}"

The output:

TASK [debug] ***************
ok: [localhost] => {
    "msg": {
        "changed": false, 
        "failed": false, 
        "instance_ids": null, 
        "instances": [], 
        "tagged_instances": [
            {
                "ami_launch_index": "0", 
                "architecture": "x86_64", 
                "block_device_mapping": {
                    "/dev/sda1": {
                        "delete_on_termination": true, 
                        "status": "attached", 
                        "volume_id": "vol-01f217e489c681211"
                    }
                }, 
                "dns_name": "", 
                "ebs_optimized": false, 
                "groups": {
                    "sg-c63822ac": "WinRM RDP"
                }, 
                "hypervisor": "xen", 
                "id": "i-019c03c3e3929f76e", 
                "image_id": "ami-3204995d", 
                ... 
                "tags": {
                    "Name": "Student01 _ Jumphost"
                }, 
                "tenancy": "default", 
                "virtualization_type": "hvm"
            }
        ]
    }
}

TASK [set_fact] ****************
ok: [localhost]

TASK [debug] ******************
ok: [localhost] => {
    "msg": "The Windows Instance ID is: [u'i-019c03c3e3929f76e']"
}

As you can see, the instance ID is correct, but not well formated. Is there a way to convert this output into "human readable" output? Or is there any better way to parse the instance id from the ec2 task output?

Thanks!

1 Answer 1

8

It's not non-human readable format, but a list object in Python notation, because you query a list.

If you want a string, you should pass it through a first filter.

win_instance_id: "{{ ec2_result | json_query('tagged_instances[*].id') | first }}"

You can also access the value directly without json_query ([0] refers to the first element of a list):

win_instance_id: "{{ ec2_result.tagged_instances[0].id }}"
Sign up to request clarification or add additional context in comments.

4 Comments

This really helped me. Is there a way to bypass set_fact and simply access the json directly from the variable, such as ec2_result.tagged_instances[*].id?
@PeteCornell Yes. Just do it.
I get "template error while templating string: unexpected '*' I am using ec2_instance_facts module however, so maybe there's a difference in output type.
@PeteCornell Sorry, I don't know what code you are running. If you have a new question, just ask it on StackOverflow. Don't forget to add the MCVE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.