# Convert curl to get\_url or uri

**URL:** <https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311>\
**Category:** Ansible Developer\
**Created:** [May 26, 2015, 2:56pm UTC](https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311 "2015-05-26T14:56:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [May 26, 2015, 2:56pm UTC](https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311/1 "2015-05-26T14:56:38Z")

</div>

Hello,

I have a couple of curl commands that I’m trying to convert to either the get\_url module or uri module. I was hoping someone here might be able to help.

Here are the commands I’m trying to convert:

1. Gets the mojolicious cookie:  
curl -vLks -X POST -d ‘u=" . $user . "’ -d ‘p=" . $pass . "’ " . $url . "/login -o /dev/null 2\>&1 | grep Set-Cookie | awk ‘{print $3}’

2. Downloads a file:

curl -k -H “Cookie: mojolicious=eyJhdXRoX2RhdGEiOiJoYmVhdHR5IiwiZXhwaXJlcyI6MTQzMjMyMjkwOH0—2231daa1152b807376209174bb4836e2ed8c623b” “https://\<some\_server\>/iso\_download?osversion=centos66&hostname=&rootpass=\<some\_pass\>&dhcp=no&ipaddr=\<some\_ip\>&netmask=\<some\_netmask\>&gateway=\<some\_gateway\>&ip6\_address=\<some\_url\_encoded\_ipv6\_address\>&ip6\_gateway=\<some\_url\_encoded\_ipv6\_gw\>&dev=p2p1&mtu=1500&ondisk=sdy” -o .iso

Here is the playbook I’ve tried so far:

- name: Login  
uri: url={{ traffic\_ops\_url }}/login follow\_redirects=all method=POST user={{ traffic\_ops\_user }} password={{ traffic\_ops\_pass }}  
register: login

- name: Get ISO  
uri: url:{{ traffic\_ops\_url }}/iso\_download?osversion=centos66&hostname={{inventory\_hostname}}&rootpass={{temp\_root\_pass}}&dhcp=no&ipaddr={{ipv4}}&netmask={{ipv4\_netmask}}&gateway={{ipv4\_gw}}&ip6\_address={{ipv6}}&ip6\_gateway={{ipv6\_gw}}&dev=p2p1&mtu=1500&ondisk=sdy return\_content=yes method=GET HEADER\_Cookie=“{{login.set\_cookie}}” dest={{inventory\_hostname}}.iso

The second command doesn’t work because there is no variable {{login.set\_cookie}}

Thanks,  
Hank

---

<div class="post-metadata">

**Author:** ![Corwin\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@Corwin\_Brown](https://forum.ansible.com/u/Corwin_Brown)\
**Post date:** [May 26, 2015, 3:16pm UTC](https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311/2 "2015-05-26T15:16:47Z")

</div>

I’m sure you have, but have you tried printing out everything that gets registered to login? It seems like the data you want is probably in there, just not accessible in the way you expect.

Try something like

- name: Debugging Login  
debug:  
msg: “{{ login }}”

---

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [May 26, 2015, 3:30pm UTC](https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311/3 "2015-05-26T15:30:39Z")

</div>

Hello,

Here is the output of the debugging suggested:

ok: [\<some\_host\>] =\> {  
“msg”: {  
“changed”: false,  
“connection”: “keep-alive”,  
“content\_length”: “3755”,  
“content\_location”: “https://\<some\_server\>/loginpage”,  
“content\_type”: “text/html;charset=UTF-8”,  
“date”: “Tue, 26 May 2015 15:26:08 GMT”,  
“invocation”: {  
“module\_args”: “url=https://\<some\_server\>/login follow\_redirects=all method=POST user=\<some\_user\> password=\<some\_pass\>”,  
“module\_name”: “uri”  
},  
“redirected”: false,  
“server”: “Mojolicious (Perl)”,  
“status”: 200  
}  
}

I don’t see the cookie in there.

Thanks,  
Hank

---

<div class="post-metadata">

**Author:** ![Corwin\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@Corwin\_Brown](https://forum.ansible.com/u/Corwin_Brown)\
**Post date:** [May 26, 2015, 4:47pm UTC](https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311/4 "2015-05-26T16:47:49Z")

</div>

Have you tried using “return\_content” in your uri call? In reading [http://docs.ansible.com/uri\_module.html](http://docs.ansible.com/uri_module.html) it looks like without it you basically only get header information. It might be worth adding that to your URI bit and seeing if you can find it in the debugging output then.

---

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [May 26, 2015, 5:58pm UTC](https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311/5 "2015-05-26T17:58:12Z")

</div>

Hello,

I was able to get the first one working by removing the “follow\_redirects=all” and adding “ignore\_errors: yes” but, the cookie is longer than when using the curl command.

Now when I run the second one I’m getting a 401 error.

Thanks,  
Hank

---

<div class="post-metadata">

**Author:** ![Corwin\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@Corwin\_Brown](https://forum.ansible.com/u/Corwin_Brown)\
**Post date:** [May 26, 2015, 6:07pm UTC](https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311/6 "2015-05-26T18:07:48Z")

</div>

Well, unfortunately that all seems to point to you not getting the data you think you’re getting. I don’t think you’ve parsed out the correct token, since you’re now getting an unauthorized

---

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [May 27, 2015, 10:52am UTC](https://forum.ansible.com/t/convert-curl-to-get-url-or-uri/18311/7 "2015-05-27T10:52:51Z")

</div>

A co-worker and I were able to get this working. Thought you might like the solutions.

- name: Login  
uri: url={{server\_url}}/login method=POST status\_code=302 body=“u={{user}}&p={{pass}}” HEADER\_Content-Type=“application/x-www-form-urlencoded”  
register: login

- uri: url={{server\_url}}/iso\_download?osversion=centos66&hostname={{inventory\_hostname}}&rootpass=&dhcp=no&ipaddr={{ipv4}}&netmask={{ipv4\_netmask}}&gateway={{ipv4\_gw}}&ip6\_address={{ipv6}}&ip6\_gateway={{ipv6\_gw}}&dev={{interface}}&mtu={{mtu}}&ondisk={{boot\_disk}} method=GET HEADER\_Cookie=“{{ login.set\_cookie }}” dest=/tmp/{{inventory\_hostname}}.iso

Thanks,  
Hank
