New URI module from Romeo Theriault

Romeo Theriault wrote an EXCELLENT module for interacting with web
services, and I've pushed it, and the docs on it are now up on the
docsite.

It's called "uri".

Unfortunately the examples kind of ate it when using our docsite
module formatter (produced some weird markup), so there aren't a lot
of examples online yet.

You should try it out anyway.

It can be used in combination with "fail" and "register" and "when"
and the like to do really basic tests for web services, and it can
even log into forms, use register to save a cookie, and then submit
other requests.

You can even use it submit JIRA tickets.

(Actually what I am thinking is eventually we start including more
examples in the examples directory, and the docsite can just link to
them).

Check it out!

-Michael

Thanks Michael!

Until we get the examples formatting straightened out I figured I’d post the examples they will show here so people can get a taste of how it works:

# Check that you can connect to a page and it returns a status 200"
action: uri url=[http://www.awesome.com](http://www.awesome.com) method=GET"

# Check that a page returns a status 200 and fail if the word AWESOME is not in the page contents.

action: uri url=[http://www.awesome.com](http://www.awesome.com) method=GET return_content=yes
register: webpage

action: fail
when_string: '"AWESOME" not in "${webpage.content}"'

     
# "Create a JIRA issue."
action: uri url=[https://your.jira.server.com/rest/api/2/issue/](https://your.jira.server.com/rest/api/2/issue/) method=POST user=your_username password=your_pass body='$FILE(issue.json)' force_basic_auth=yes status_code=201 HEADER_Content-Type="application/json"

     
# Login to a form based webpage, then use the cookie that got returned to access the app in later tasks.

action: uri url=[https://your.form.based.auth.app.com/index.php](https://your.form.based.auth.app.com/index.php) method=POST body="name=your_username&password=your_password&enter=Sign%20in" status_code=302 HEADER_Content-Type="application/x-www-form-urlencoded"

register: login

action: uri url=[https://your.form.based.auth.app.com/dashboard.php](https://your.form.based.auth.app.com/dashboard.php) method=GET return_content=yes HEADER_Cookie="${login.set_cookie}"

Romeo:

Which machine are the requests made from, the remote host or the local machine that runs Ansible?

Lorin

The remote machine unless you specify the task as a 'delegate_to' or
'local_action' type task.

Which machine are the requests made from, the remote host or the local
machine that runs Ansible?

There is no connection plugin involved.

If you use local_action, they are local, if not, they are not.

Thanks Michael!

Until we get the examples formatting straightened out I figured I'd post the
examples they will show here so people can get a taste of how it works:

Thanks, I think "make docs" were fine but it was evident issues came
up with "make webdocs" when they produced weirdness in the rst files
that Sphinx did not like.

If you want to try to fix those, that would be great, and I'll take them.

I am herding too many other random things :slight_smile:

Thanks for the pointer and absolutely, I'll work on getting it
straightened out this weekend and sending a PR.

Thanks,

Hi

Is it possible to define two status_codes? For instance I have webpage which can return status code 200 or status code 301 and both are ok. How can i tell uri module to accept both?

thanks
Edgars

svētdiena, 2013. gada 17. februāris 00:50:27 UTC+1, Michael DeHaan rakstīja:

PR#4383

Edgars

svētdiena, 2013. gada 17. februāris 00:50:27 UTC+1, Michael DeHaan rakstīja:

Hi,

Can someone point me to a way of using this module with, say Openstack Keystone?

I sending my login/password to IP:35357 of Keystone, get token.

Question is - how can I cut token from the responce JSON, and assign it to a variable?

Cheers,
NM

If you run ansible-playbook with -v you will see what modules return.

You would then use the “register” statement to assign to a variable.

Hi
That`s exactly what I do now, thanks Michael.

I have a small issue, however.

- name: Test...
  shell: uname -a
  register: testvar
  tags:
    - testvar

- name: Test 1...
  shell: touch /tmp/testout
  tags:
    - testvar

- name: Test 2...
  shell: echo {{ testvar.stdout_lines }} > /tmp/testout
  tags:
    - testvar

# cat testout
[uLinux ctrl01-001 3.8.0-31-generic #46-Ubuntu SMP Tue Sep 10 20:03:44 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux]

How can I get rid of those [u ... ] braces?

Thanks,
NM

stdout_lines is an array, so it’s printing them as an array.

I would be more inclined to save the result to a file locally if you really wanted to do that, but I’d like to ask what the root use case actually is, as there are better ways to model this sort of thing in almost all cases than going through intermediate files.

Help me understand a bit what the real world scenario is?

Hi,

Well, usecase was rather simple - I needed to install Openstack`s Cinder with Ceph backend, and that required issuing commands that respond with hasg keys on STDOUT. Idea was to capture this output as variable and use it in consequent libvirt commands.

But I managed to route it to file and read from it.

Thanks,
NM

I have this same issue when using it to create riak-cs admin users.

riak-cs will return either 201 or 409 when sending a PUT request, never a 200.

Unfortunately 201 triggers module failure, as 200 is what it expects.

riak_cs | FAILED

“msg”: “Status code was not 200”,

I can work around this, but it’s going to be ugly.

I have this same issue when using it to create riak-cs admin users.

riak-cs will return either 201 or 409 when sending a PUT request, never a 200.

Unfortunately 201 triggers module failure, as 200 is what it expects.

riak_cs | FAILED

“msg”: “Status code was not 200”,

There’s a status_code option you can send.

http://docs.ansible.com/uri_module.html

Yes, but there are two valid results, either 409 or 201.

You may want to check out this pull request:

https://github.com/ansible/ansible/pull/4383/files

and possibly try resubmitting it the suggested fixes. This would allow
you to specify more than one status_code. If you can't get to it I may
be able to this weekend.

Romeo

Thanks Romeo, I have recreated a pull request.

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