urlparse filters

I added a urljoin filter (https://github.com/jalaziz/ansible/commit/2eb5d1b94732197c651f6962444df88bb90a1575), but am considering adding some other urlparse filters before submitting the PR. Any suggestions on other url filters people would like to see?

I've had the need for a URL query parse (turn a query string into a
dictionary) and compose (turn a dictionary into a query string) filter
before. I managed to get around it with some looping in jinja, but
this would have been much cleaner.

Do you think exposing urlparse.parse_qs() and urllib.urlencode() is enough for this? If support is added for parse_qs, then we’d probably need urlsplit or urlparse to fully take advantage of it.

Also, do you think you can give me examples of how you’d use those filters for the docs? I’m sure I can come up with something, but a real use-case would be ideal.

I’ll update my branch later today/tonight and submit the PR unless anyone else has suggestions.

Here's a couple of use cases that I've had:

1) I'm using the "uri" module to retrieve information from a
webservice. The returned JSON has a field which is a url with a query
string. I need to take one of the values in that query string as a
fact for another task.

Right now I'm using an ugly regex_replace and I'm lucky that this
particular value is not url encoded since decoding it would be tricky.
So maybe this is arguing for a urldecode filter as well, but something
like this would be really nice:

  set_fact: foo={{ service_results | parse_url_query | attr('foo') }}

2) Using the "uri" module to interact with a web service I need to
create a query string based on parameters in a dictionary. I can use a
combination loops and urlencode but it's ugly (and seems like I'm
using ansible/jinja as a programming language):

  set_fact: foo_query="{% for k in foo_dict %}{{ k }}={{ foo_dict[k] |
urlencode }}{% if not loop.last %}&{% endif %}{% endfor %}"

Could be cleaner with something like:

  set_fact: foo_query="{{ foo_dict | url_query }}"

Or something like that. This is more about reducing clutter, not
because it's not currently possible. So the 2nd idea would be ok to
drop if others think it clutters us the filter space too much.

But more generically it would be nice to be able to parse URLs into
structured data and then be able to interact with that data. And the
flip side of being able to turn structured data into URLs would be
nice. Hopefully this gives you some ideas along those lines.

So as it turns out, the urlencode filter that ships with Jinja2 2.7 already supports converting dictionaries into query string params.

Your second example can actually be written as:
set_fact: foo_query=“{{ foo_dict | urlencode }}”

That being said, I don’t see a way to do the reverse. So, it looks like adding parse_qs and urlsplit/urlparse filters may be useful.

Nice, thanks for pointing that out. I did see the docs mention it
supported a dictionary, but since I didn't try it I just assumed it
would urlencode each key/value string inside the dictionary rather
than converting into a query string.