format url module output

798 views
Skip to first unread message

David Neudorfer

unread,
Nov 19, 2013, 4:28:42 PM11/19/13
to ansible...@googlegroups.com
I'm using Ansible to purchase a domain name using the namecheap.com api. Here is a example of the playbook.

- name: Purchase Domain Name
  local_action: >
        method=GET body="{{ namecheap_purchase_domain_name }}"
        status_code=200 HEADER_Content-Type="application/x-www-form-urlencoded"
  register: domain_name_purchase
  
- debug: msg="{{ domain_name_purchase }}"

The output looks like this:

ok: [162.243.95.241] => {"cache_control": "private", "changed": false, "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<ApiResponse Status=\"OK\" xmlns=\"http://api.namecheap.com/xml.response\">\r\n  <Errors />\r\n  <Warnings />\r\n  <RequestedCommand>namecheap.domains.getList</RequestedCommand>\r\n  <CommandResponse Type=\"namecheap.domains.getList\">\r\n    <DomainGetListResult>\r\n      <Domain ID=\"33320\" Name=\"asdfasdfasdfasdfasdfasdfasdfjk123.com\" User=\"username\" Created=\"11/19/2013\" Expires=\"11/19/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"33321\" Name=\"avd.com\" User=\"username\" Created=\"11/19/2013\" Expires=\"11/19/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"33319\" Name=\"domain.com\" User=\"username\" Created=\"11/19/2013\" Expires=\"11/19/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"33318\" Name=\"domain.com\" User=\"username\" Created=\"11/19/2013\" Expires=\"11/19/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"33322\" Name=\"domain11.com\" User=\"username\" Created=\"11/19/2013\" Expires=\"11/19/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"33323\" Name=\"domain 13.com\" User=\"username\" Created=\"11/19/2013\" Expires=\"11/19/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"32804\" Name=\"domain.com\" User=\"username\" Created=\"11/01/2013\" Expires=\"11/02/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"32805\" Name=\"domain3.com\" User=\"username\" Created=\"11/01/2013\" Expires=\"11/02/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"32806\" Name=\"domain 4.com\" User=\"username\" Created=\"11/01/2013\" Expires=\"11/02/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n      <Domain ID=\"33316\" Name=\"domain5.com\" User=\"username\" Created=\"11/19/2013\" Expires=\"11/19/2014\" IsExpired=\"false\" IsLocked=\"false\" AutoRenew=\"false\" WhoisGuard=\"NOTPRESENT\" />\r\n    </DomainGetListResult>\r\n    <Paging>\r\n      <TotalItems>10</TotalItems>\r\n      <CurrentPage>1</CurrentPage>\r\n      <PageSize>20</PageSize>\r\n    </Paging>\r\n  </CommandResponse>\r\n  <Server>WEB1-SANDBOX1</Server>\r\n  <GMTTimeDifference>--5:00</GMTTimeDifference>\r\n  <ExecutionTime>0.096</ExecutionTime>\r\n</ApiResponse>", "content_length": "2478", "content_location": "https://api.sandbox.namecheap.com/xml.response", "content_type": "text/xml; charset=utf-8", "date": "Wed, 20 Nov 2013 02:06:37 GMT", "item": "", "redirected": false, "server": "Microsoft-IIS/7.0", "status": 200, "x_aspnet_version": "4.0.30319", "x_powered_by": "ASP.NET"}

I'm interested in the XML inside the 'content' container. Is there any way to query this output so I know if the purchase was successful or not

Kahlil Hodgson

unread,
Nov 19, 2013, 4:51:23 PM11/19/13
to ansible...@googlegroups.com
something like

- command: /bin/echo FOO
  when: '"FOO" in domain_name_purchase.stdout'

where FOO is a string indicating successful purchase.

Hope this helps,

K


Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



--
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.
For more options, visit https://groups.google.com/groups/opt_out.

David Neudorfer

unread,
Nov 19, 2013, 5:23:07 PM11/19/13
to ansible...@googlegroups.com
Thanks Kahlil,

Thats a great idea. Thank you. I've updated my code now:

- name: Purchase Domain Name
  local_action: >
        uri
        method=GET
        body="{{ namecheap_purchase_domain_name }}"
        status_code=200
        HEADER_Content-Type="application/x-www-form-urlencoded"
        return_content=yes
  register: domain_name_purchase

- command: /bin/echo SUCCESS
  when: '"ApiResponse Status=OK" in domain_name_purchase.stdout'

Now I'm getting this error:

TypeError: argument of type 'StrictUndefined' is not iterable

Michael DeHaan

unread,
Nov 19, 2013, 5:26:38 PM11/19/13
to ansible...@googlegroups.com
yes, you can't call things on undefined variables.

Perhaps there's a copy paste error and that variable isn't named what you expect.

Also, it should *NOT* be a thing, but might be in some versions of ansible, make sure there's not a trailing space after the register line.  If there is, let me know, I'm just being paranoid that that could be a thing.




--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Kahlil Hodgson

unread,
Nov 19, 2013, 5:36:15 PM11/19/13
to ansible...@googlegroups.com
Hmm ... just searched by ansible setup because I've seen this recently.  Here's an example where selinux.stdout is undefined when it perhaps should be an empty string.

# this is mainly to fix an iptables rpm bug
- name: ensure selinux contexts under /sbin are correct
  command: /sbin/restorecon -rv /sbin/
  register: selinux
  changed_when: >
    selinux.stdout is defined and 'reset' in selinux.stdout



Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



Michael DeHaan

unread,
Nov 19, 2013, 5:40:16 PM11/19/13
to ansible...@googlegroups.com
I would agree if stdout is blank it should be returned as an empty string.

If you can reproduce this on 1.3.X/1.4 please file a ticket and we'll repro on our end and make sure 1.4 or 1.5 gets a fix for it.


Kahlil Hodgson

unread,
Nov 19, 2013, 5:41:24 PM11/19/13
to ansible...@googlegroups.com
<rizo:ansible> ansible --version
ansible 1.4 (devel b7971a426c) last updated 2013/11/18 08:39:30 (GMT +1100)

Will file a ticket.


Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



David Neudorfer

unread,
Nov 19, 2013, 6:24:05 PM11/19/13
to ansible...@googlegroups.com
Woo Hoo! Always good to help discover a bug. I'm still struggling with parsing the output. my variable {{ domain_name_purchase }} is returned what I called in the url while "return_content=yes" is returned the error code I want to parse. How do I manipulate the returned content?


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/spNDlR4XTCg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

Michael DeHaan

unread,
Nov 19, 2013, 6:27:55 PM11/19/13
to ansible...@googlegroups.com
Define manipulate?

Kahlil Hodgson

unread,
Nov 19, 2013, 6:28:34 PM11/19/13
to ansible...@googlegroups.com
Hi David,

I'm having trouble reproducing this issue.  Have been taking a lot of pain killers over the last week, so this may just be because my brain is mush.  What version of ansible are using?

K


Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



Kahlil Hodgson

unread,
Nov 19, 2013, 6:35:17 PM11/19/13
to ansible...@googlegroups.com
On 20 November 2013 10:24, David Neudorfer <da...@davidneudorfer.com> wrote:
How do I manipulate the returned content?

for some jinja type things that you can do

Also there are some ansible docs on filters somewhere on the website.

The variable is python-ish so I believe some python constructs might work.

K

Michael DeHaan

unread,
Nov 19, 2013, 6:38:38 PM11/19/13
to ansible...@googlegroups.com
For purposes of Google history, let us know what you want to do, and we can suggest something.




--
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.
For more options, visit https://groups.google.com/groups/opt_out.

David Neudorfer

unread,
Nov 19, 2013, 6:39:38 PM11/19/13
to ansible...@googlegroups.com
I'm running version 1.4 and I'm looking to detect what error returns 

<ApiResponse Status=\"ERROR\"

and what that error is:

<Error Number=\"3028166\">540:Object exists</Error>

or a success

ApiResponse Status=\"OK\"


Thanks for the help guys.

Cheers,

David

David Neudorfer

unread,
Nov 19, 2013, 7:19:03 PM11/19/13
to ansible...@googlegroups.com
Maybe someone can help me understand my python better. domain_name_purchase is dict object that as far as I can tell has no attributes or elements. I can echo it but beyond that I'm stuck.

Kahlil Hodgson

unread,
Nov 19, 2013, 7:26:18 PM11/19/13
to ansible...@googlegroups.com
try

- debug: var=domain_name_purchase

should show you all the elements of the dict

K


Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



David Neudorfer

unread,
Nov 19, 2013, 7:31:57 PM11/19/13
to ansible...@googlegroups.com
Kahlil, Thank you! That got me one step closer. I'm using:

- debug: var=domain_name_purchase.content

To return the data I need. How do I drill down even further? 

I'd like to return "ApiResponse Status" and check if it was ERROR or SUCCESS and what "Error Number" is was

ok: [162.243.95.241] => {

    "domain_name_purchase.content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<ApiResponse Status=\"ERROR\" xmlns=\"http://api.namecheap.com/xml.response\">\r\n  <Errors>\r\n    <Error Number=\"4014104\">Possible duplicate create command for unavailable domain. Try again after 11/20/2013 7:29:46 AM UTC</Error>\r\n  </Errors>\r\n  <Warnings />\r\n  <RequestedCommand>namecheap.domains.create</RequestedCommand>\r\n  <CommandResponse Type=\"namecheap.domains.create\">\r\n    <DomainCreateResult Domain=\"elitereceipt202321412341234.com\" ChargedAmount=\"0\" DomainID=\"0\" OrderID=\"0\" TransactionID=\"0\" WhoisguardEnable=\"false\" FreePositiveSSL=\"false\" NonRealTimeDomain=\"false\" />\r\n  </CommandResponse>\r\n  <Server>WEB1-SANDBOX1</Server>\r\n  <GMTTimeDifference>--5:00</GMTTimeDifference>\r\n  <ExecutionTime>0.02</ExecutionTime>\r\n</ApiResponse>", 

    "item": ""

}

Kahlil Hodgson

unread,
Nov 19, 2013, 7:42:08 PM11/19/13
to ansible...@googlegroups.com
You've drilled down as far as you can go in the data structure. What you have got is a string. You will need to do some kind of string munging to get any further. I think you might be able to use the python 'find' method on the string, e.g., {{ domain_name_purchase.content.find("Error") }}, but I'm not sure that's what you want.  Someone on this list who actually knows python might be able to help here and I'd be keen to see how the experts approach it. It would be great if there was a filter (say 'xml2dict') that would magically turn an xml string into python/jinja2 data structure.

K


Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



Michael DeHaan

unread,
Nov 19, 2013, 10:46:07 PM11/19/13
to ansible...@googlegroups.com
when: "'foo' in variable"

when: "'foo' not in variable"

is usually cleaner than find.

find in Python returns -1 if a string is not found, so what Kahlil suggested above needs to add that the comparison to -1.




Kahlil Hodgson

unread,
Nov 19, 2013, 10:59:36 PM11/19/13
to ansible...@googlegroups.com
I think what David is looking for, and what I was alluding to, was something like a regexp capture. I know how I'd do this in Perl, but have no idea what the magic Python invocation would be. That is, something like: {{ foo_string.capture('Error="(\d+)"', 1) }} to give me back the error number, perhaps for later use in a template.  Is there such a thing in Python?

K


 


Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



David Neudorfer

unread,
Nov 19, 2013, 11:01:37 PM11/19/13
to ansible...@googlegroups.com
Kahlil is correct. Thats exactly what I'm looking for. Thank for the help guys.

Kahlil Hodgson

unread,
Nov 20, 2013, 6:31:58 PM11/20/13
to ansible...@googlegroups.com
I've just made a pull request to add a 'capture' filter:

https://github.com/ansible/ansible/pull/4981

The idea is that you would be able to do something like

{{ foo_string | capture('Error="(\d+)"') }}

to get the error number buried in the string.

If this is accepted, I might even add some documentation :-)

K


Kahlil (Kal) Hodgson GPG: C9A02289
Head of Technology (m) +61 (0) 4 2573 0382
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925



PePe Amengual

unread,
Nov 20, 2013, 7:24:11 PM11/20/13
to ansible...@googlegroups.com
I'm basically having the same issue I need to get  from this :

{
   "httpStatus" : 201,
   "detail" : "Request accepted.",
   "estimatedSeconds" : 420,
   "purgeId" : "95b5a092-043f-4af0-843f-aaf0043faaf0",
   "progressUri" : "/ccu/v2/purges/95b5a092-043f-4af0-843f-aaf0043faaf0",
   "pingAfterSeconds" : 420,
   "supportId" : "17PY1321286429616716-211907680",
}

the pingAfterSeconds to use it in a wait_for task and the purgeid but this is a JSON so dunno if the process is similar to a string parsing?

Thanks.

Romeo Theriault

unread,
Nov 20, 2013, 9:44:35 PM11/20/13
to ansible...@googlegroups.com
On Wed, Nov 20, 2013 at 2:24 PM, PePe Amengual <jose.a...@gmail.com> wrote:
I'm basically having the same issue I need to get  from this :

{
   "httpStatus" : 201,
   "detail" : "Request accepted.",
   "estimatedSeconds" : 420,
   "purgeId" : "95b5a092-043f-4af0-843f-aaf0043faaf0",
   "progressUri" : "/ccu/v2/purges/95b5a092-043f-4af0-843f-aaf0043faaf0",
   "pingAfterSeconds" : 420,
   "supportId" : "17PY1321286429616716-211907680",
}

the pingAfterSeconds to use it in a wait_for task and the purgeid but this is a JSON so dunno if the process is similar to a string parsing?


If the web service returns a Content-type header set as "application/json" and the response you get is valid json then the uri module will parse the JSON and make it available via the key. 


So you should be able to do something like {{ register_var.content.pingAfterSeconds }} to get the value. (Not tested, but you get the idea.)

Once you get it, it would be good to send a pull request better documenting this :)


--
Romeo

PePe Amengual

unread,
Nov 21, 2013, 1:31:03 PM11/21/13
to ansible...@googlegroups.com
This is what I get from the API :


HTTP/1.1 201 Created
Date: Thu, 21 Nov 2013 18:01:08 GMT
Server: Apache
Content-Length: 268
Content-Location: /ccu/v2/purges/e771b867-52d6-11e3-bcaf-2a98bcaf2a98
Allow: GET, HEAD, POST
Vary: Authorization
Content-Type: application/json

{"estimatedSeconds": 420, "progressUri": "/ccu/v2/purges/e771b867-52d6-11e3-bcaf-2a98bcaf2a98", "purgeId": "e771b867-52d6-11e3-bcaf-2a98bcaf2a98", "supportId": "17PY1385056868737216-189879392", "httpStatus": 201, "detail": "Request accepted.", "pingAfterSeconds": 420}⏎   

my uri request from the playbook :

tasks:
   - name: Flushing Akamai cache
             method=POST user=aaaa password=aaaa
             HEADER_Content-Type="application/json" 
             body='{"objects":["{{urllist}}"]}'
             status_code=201
             return_content=yes
     register: akamairesponse
   - debug: msg="value of {{akamairesponse.content.purgeId}}"
   - name: Notifing
     action: hipchat token=aaa room=Akamai msg="Purged {{urllist}}"
     action: hipchat token=aaa room=Akamai msg="{{akamairesponse.content.purgerId}}"

I get in hipchat this {{akamairesponse.content.purgerId}} like a string is not actually parsing the data.

the debug :

PLAY [localhost] ************************************************************** 

TASK: [Flushing Akamai cache] ************************************************* 
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-1385033255.71-37626758703564 && chmod a+rx $HOME/.ansible/tmp/ansible-1385033255.71-37626758703564 && echo $HOME/.ansible/tmp/ansible-1385033255.71-37626758703564']
<localhost> REMOTE_MODULE uri url=https://api.ccu.akamai.com/ccu/v2/queues/default method=POST user=xxxxxx password=xxxxx HEADER_Content-Type="application/json" body='{"objects":["http://xxxx/images/flags/uk.png"]}' status_code=201 return_content=yes
<localhost> PUT /tmp/tmpR6pSOQ TO /usr/home/jamengual/.ansible/tmp/ansible-1385033255.71-37626758703564/uri
<localhost> EXEC ['/bin/sh', '-c', '/usr/local/bin/python /usr/home/jamengual/.ansible/tmp/ansible-1385033255.71-37626758703564/uri; rm -rf /usr/home/jamengual/.ansible/tmp/ansible-1385033255.71-37626758703564/ >/dev/null 2>&1']
ok: [localhost] => {"allow": "GET, HEAD, POST", "changed": false, "content": "{\"estimatedSeconds\": 420, \"progressUri\": \"/ccu/v2/purges/bcb3d980-52da-11e3-b16b-21bd716b21bd\", \"purgeId\": \"bcb3d980-52da-11e3-b16b-21bd716b21bd\", \"supportId\": \"17PY1385058515823533-206664800\", \"httpStatus\": 201, \"detail\": \"Request accepted.\", \"pingAfterSeconds\": 420}", "content_length": "268", "content_location": "/ccu/v2/purges/bcb3d980-52da-11e3-b16b-21bd716b21bd", "content_type": "application/json", "date": "Thu, 21 Nov 2013 18:28:35 GMT", "json": {"detail": "Request accepted.", "estimatedSeconds": 420, "httpStatus": 201, "pingAfterSeconds": 420, "progressUri": "/ccu/v2/purges/bcb3d980-52da-11e3-b16b-21bd716b21bd", "purgeId": "bcb3d980-52da-11e3-b16b-21bd716b21bd", "supportId": "17PY1385058515823533-206664800"}, "redirected": false, "server": "Apache", "status": "201", "vary": "Authorization"}

TASK: [debug msg="value of {{akamairesponse.content.purgeId}}"] *************** 
fatal: [localhost] => One or more undefined variables: 'unicode object' has no attribute 'purgeId'

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/usr/home/jamengual/akamai.retry

localhost                  : ok=1    changed=0    unreachable=1    failed=0   

I tried with : {{akamairesponse.content.purgeId}} and {{akamairesponse.content.[purgeId]}}, {{akamairesponse.content.["purgeId"]}} and {{akamairesponse.content."purgeId"}} and it doesn't work.

am I doing something wrong ?

Thanks.

Romeo Theriault

unread,
Nov 21, 2013, 2:33:23 PM11/21/13
to ansible...@googlegroups.com
What is the debug output of just {{akamairesponse}} and {{akamairesponse.content}} ? Just trying to narrow down...


--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
Romeo

PePe Amengual

unread,
Nov 21, 2013, 7:19:13 PM11/21/13
to ansible...@googlegroups.com
this :

{u'status': u'201', u'content_length': u'268', u'content_type': u'application/json', u'changed': False, u'vary': u'Authorization', u'server': u'Apache', u'content': u'{estimatedSeconds: 420, progressUri: /ccu/v2/purges/41224eec-52df-11e3-8be1-212a0be1212a, purgeId: 41224eec-52df-11e3-8be1-212a0be1212a, supportId: 17PY1385060455982392-223450208, httpStatus: 201, detail: Request"}

Romeo Theriault

unread,
Nov 21, 2013, 7:54:58 PM11/21/13
to ansible...@googlegroups.com
I'm assuming that's the output from just {{akamairesponse}} ?  If so what do you get when you take it to the next level of {{akamairesponse.content}}?

Not sure if it's a copy/paste issue but it looks like the response is missing a trailing '}' as well, which could be an issue.

Romeo

PePe Amengual

unread,
Nov 21, 2013, 8:02:08 PM11/21/13
to ansible...@googlegroups.com
that is the output from debug :

TASK: [debug msg="value of {{akamairesponse}}"] ******************************* 
ok: [localhost] => {"msg": "value of {u'status': u'201', u'content_length': u'268', u'content_type': u'application/json', u'changed': False, u'vary': u'Authorization', u'server': u'Apache', u'content': u'{estimatedSeconds: 420, progressUri: /ccu/v2/purges/26f55aaf-5311-11e3-a7ec-8286a7ec8286, purgeId: 26f55aaf-5311-11e3-a7ec-8286a7ec8286, supportId: 17PY1385081886910598-252707936, httpStatus: 201, detail: Request"}


akamairesponse.content is :

ok: [localhost] => {"msg": "value of {estimatedSeconds: 420, progressUri: /ccu/v2/purges/7ce8c4c2-5311-11e3-8bb2-22460bb22246, purgeId: 7ce8c4c2-5311-11e3-8bb2-22460bb22246, supportId: 17PY1385082031067768-252707936, httpStatus: 201, detail: Request"}

it looks correct to me.

Romeo Theriault

unread,
Nov 21, 2013, 9:00:22 PM11/21/13
to ansible...@googlegroups.com
Ok, so looked at the code again and the way it should work if the JSON was getting parsed correctly would be:

{{akamairesponse.json.purgeId}} 

but since we're not seeing the json subkey I'm guessing the code that loads the json is having an exception on loading the data (i.e. invalid json)

    if 'content_type' in uresp:
        if uresp['content_type'].startswith('application/json'):
            try:
                js = json.loads(content)
                uresp['json'] = js
            except:
                pass


I'm not quite sure why it doesn't think it's valid json though. You'll have to debug that a bit more. It looks like it should work though....

Romeo


Romeo Theriault

unread,
Nov 21, 2013, 9:04:25 PM11/21/13
to ansible...@googlegroups.com
One more thing, make sure you have the python json module installed and not just the simplejson module. It seems that the json module is needed to do the json.loads.
--
Romeo

Michael DeHaan

unread,
Nov 21, 2013, 9:19:14 PM11/21/13
to ansible...@googlegroups.com
This got sorted on IRC.

The value of registered variable foo was in fact available in "foo.json" and things like "foo.json.subValue" got accessible.

For some reason it was being debugged weird or didn't come back valid in content type and later started working?




Romeo Theriault

unread,
Nov 21, 2013, 9:30:45 PM11/21/13
to ansible...@googlegroups.com
Great. I'll see about submitting a pull request to the docs about the json subkey if the returned content is JSON. That way I don't have to look at the code next time to remember :)
Reply all
Reply to author
Forward
0 new messages