cannot access attributes of registered variable returned by AWS CLI command run

125 views
Skip to first unread message

Co S

unread,
Nov 15, 2015, 6:53:59 AM11/15/15
to Ansible Project
Hi All,

I cannot figure out how to reference the zone id attribute from the registered variable of the below AWS CLI.
For some reason it doesn't work.

Playbook:
___________________________
- name: List hosted zone
  command: aws route53 list-hosted-zones --max-items 1 --query 'HostedZones[?Name==`"{{ zone_name }}"`]' --output json
  register: hosted_zone_list_res

- name: Get hosted zone details
  set_fact: hosted_zone="{{ hosted_zone_list_res.stdout | from_json }}"

- debug: var=hosted_zone

- name: Get hosted zone ID
  set_fact: zone_id="{{ hosted_zone[0].Id }}"
___________________________


Here is the output:
___________________________
TASK: [route53_zone | Get hosted zone details] ********************************
ok: [127.0.0.1] => {"ansible_facts": {"hosted_zone": "[{u'Config': {u'Comment': u'Test public zone', u'PrivateZone': False}, u'CallerReference': u'20151115-095525', u'ResourceRecordSetCount': 2, u'Id': u'/hostedzone/Z2VTI3W5U16LCK', u'Name': u'aws.company.com.'}]"}}

TASK: [route53_zone | debug var=hosted_zone] **********************************
ok: [127.0.0.1] => {
    "var": {
        "hosted_zone": [
            {
                "CallerReference": "20151115-095525",
                "Config": {
                    "Comment": "Test public zone",
                    "PrivateZone": false
                },
                "Id": "/hostedzone/Z2VTI3W5U16LCK",
                "Name": "aws.company.com.",
                "ResourceRecordSetCount": 2
            }
        ]
    }
}

TASK: [route53_zone | Get hosted zone ID] *************************************
fatal: [127.0.0.1] => One or more undefined variables: 'unicode object' has no attribute 'Id'

FATAL: all hosts have already failed -- aborting
___________________________

Could you please advise what I'm doing wrong?

Regards,
Costea

Matt Martz

unread,
Nov 15, 2015, 10:42:00 AM11/15/15
to ansible...@googlegroups.com
You will have issues trying to use '|from_json' in a set_fact as it is likely to cast the value back to a string before Ansible 2.0.

You should typically always use filters that change the data type in the "last mile".

So instead of using set_fact, just use the registered var where you need it.  If you want, you could always use set_fact to give you easier direct access to stdout, but you will still need to use '|from_json' in the last mile.

Such as:

(hosted_zone_list_res.stdout | from_json)[0].Id

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d9122654-1552-412d-9988-92753a502699%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Reply all
Reply to author
Forward
0 new messages