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 On Sunday, November 15, 2015, Co S <[email protected]> wrote: > 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 > > -- > 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 [email protected] > <javascript:_e(%7B%7D,'cvml','ansible-project%[email protected]');> > . > To post to this group, send email to [email protected] > <javascript:_e(%7B%7D,'cvml','[email protected]');>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/d9122654-1552-412d-9988-92753a502699%40googlegroups.com > <https://groups.google.com/d/msgid/ansible-project/d9122654-1552-412d-9988-92753a502699%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- Matt Martz @sivel sivel.net -- 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 [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAD8N0v_V5dHRAkg_75epoN-anpsPUS0g-X2xVan1pq8254q%2Byg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
