# urlparse filters

**URL:** <https://forum.ansible.com/t/urlparse-filters/16303>\
**Category:** Ansible Developer\
**Created:** [September 23, 2014, 12:12am UTC](https://forum.ansible.com/t/urlparse-filters/16303 "2014-09-23T00:12:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jameel\_Al-Aziz](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Jameel\_Al-Aziz](https://forum.ansible.com/u/Jameel_Al-Aziz)\
**Post date:** [September 23, 2014, 12:12am UTC](https://forum.ansible.com/t/urlparse-filters/16303/1 "2014-09-23T00:12:17Z")

</div>

I added a urljoin filter ([https://github.com/jalaziz/ansible/commit/2eb5d1b94732197c651f6962444df88bb90a1575](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?

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [September 23, 2014, 1:25pm UTC](https://forum.ansible.com/t/urlparse-filters/16303/2 "2014-09-23T13:25:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jameel\_Al-Aziz](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Jameel\_Al-Aziz](https://forum.ansible.com/u/Jameel_Al-Aziz)\
**Post date:** [September 23, 2014, 7:42pm UTC](https://forum.ansible.com/t/urlparse-filters/16303/3 "2014-09-23T19:42:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [September 24, 2014, 7:58pm UTC](https://forum.ansible.com/t/urlparse-filters/16303/4 "2014-09-24T19:58:04Z")

</div>

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:

&nbsp;&nbsp;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):

&nbsp;&nbsp;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:

&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Jameel\_Al-Aziz](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Jameel\_Al-Aziz](https://forum.ansible.com/u/Jameel_Al-Aziz)\
**Post date:** [September 24, 2014, 8:25pm UTC](https://forum.ansible.com/t/urlparse-filters/16303/5 "2014-09-24T20:25:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [September 24, 2014, 8:38pm UTC](https://forum.ansible.com/t/urlparse-filters/16303/6 "2014-09-24T20:38:05Z")

</div>

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.
