4

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!

dom_weissb03
  • 415
  • 2
  • 5
  • 8

1 Answers1

7

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 }}"
techraf
  • 64,883
  • 27
  • 193
  • 198
  • 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`? – Pete Cornell Jun 21 '18 at 23:52
  • @PeteCornell Yes. Just do it. – techraf Jun 22 '18 at 04:15
  • 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. – Pete Cornell Jun 22 '18 at 17:27
  • @PeteCornell Sorry, I don't know what code you are running. If you have a new question, just [ask it on StackOverflow](https://stackoverflow.com/questions/ask). Don't forget to add the [MCVE](https://stackoverflow.com/help/mcve). – techraf Jun 22 '18 at 17:29