unicode, json...

I have an AWS CLI command that returns this (I’ve truncated it):

changed: [localhost] => {“changed”: true, “cmd”: [“aws”, “elbv2”, “create-load-balancer”, “–name”, “ADFS-ext-nlb”, “–type”, “network”, “–subnet-mappings”, "SubnetId=subnet-c1580ba5,AllocationId=eipalloc-09644c10a5aac6a39 SubnetId=subnet-3cd85265,AllocationId=eipalloc-09c95f8e4f8e111b2 "], “delta”: “0:00:02.343846”, “end”: “2018-09-01 14:13:21.199441”, “rc”: 0, “start”: “2018-09-01 14:13:18.855595”, “stderr”: “”, “stderr_lines”: , “stdout”: "{\n "LoadBalancers": [\n {\n "Scheme": "internet-facing",\n "State": {\n "Code": "active"\n },\n "DNSName": "ADFS-ext-nlb-722a4422c412e03e.elb.ap-southeast-2.amazonaws.com",\n

I have converted it into json like this:

  • set_fact:
    nlb: " {{ nlb.stdout |from_json }}"

And the output from that is this:

TASK [nlb : set_fact] ************************************************************************************
ok: [localhost] => {“ansible_facts”: {“nlb”: " {u’LoadBalancers’: [{u’IpAddressType’: u’ipv4’, u’Scheme’: u’internet-facing’, u’LoadBalancerArn’: u’arn:aws:elasticloadbalancing:ap-southeast-2:375939078089:loadbalancer/net/ADFS-ext-nlb/722a4422c412e03e’, u’VpcId’: u’vpc-8bc156ef’, u’State’: {u’Code’: u’active’}, u’DNSName’: u’ADFS-ext-nlb-722a4422c412e03e.elb.ap-southeast-2.amazonaws.com’, u’LoadBalancerName’: u’ADFS-ext-nlb’, u’CreatedTime’: u’2018-09-01T03:10:47.036Z’, u’AvailabilityZones’: [{u’SubnetId’: u’subnet-c1580ba5’, u’ZoneName’: u’ap-southeast-2b’, u’LoadBalancerAddresses’: [{u’IpAddress’: u’52.64.194.159’, u’AllocationId’: u’eipalloc-09c95f8e4f8e111b2’}]}], u’Type’: u’network’, u’CanonicalHostedZoneId’: u’ZCT6FZBF4DROD’}]} "}, “changed”: false}

I want to extract a LoadBalancerArn from that, but I can’t seem to refer to the internal structure. I won’t list here all the various syntactical variations I’ve tried! What I want is something like this:

  • set_fact:

lb_arn: “{{ nlb.LoadBalancers[0].LoadBalancerArn }}”

No matter what I try I always get something like this:

fatal: [localhost]: FAILED! => {“msg”: “‘ansible.utils.unsafe_proxy.AnsibleUnsafeText object’ has no attribute ‘LoadBalancers’”}

Is this because of the unicode? Or am I missing something else?

Regards, K.

It appears that nlb contains a string. Perhaps because of the initial space in

nlb: " {{ nlb.stdout |from_json }}"

?

Thank you!

It all suddenly worked and I had no idea why, then you pointed out the space and the light dawned. I’d removed the space, but without realising it was significant. Also, I worked out that the I needed to treat the response from AWS as yaml, not json - that got rid of the unicode stuff.

Thanks again.

I wonder if I will ever NOT be a noob at this stuff… sigh.

Regards, K.