Convert curl to get_url or uri

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

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 }}”

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

Have you tried using “return_content” in your uri call? In reading 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.

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

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

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