mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-09 23:00:02 -07:00
Create urlsplit filter (#28537)
* Improve tests for uri filter * Create URL Split docs * Add urlsplit filter * Py3 compatibility * Use helper method and eliminate query options * Add options, cleanup output, fix tests * Update docs * Add parenthesis to boilerplate import * Add debug task to tests * Use exclude option to filter returned values * Filter out additional option for Python 3
This commit is contained in:
parent
15bfdd634a
commit
80c00d3238
3 changed files with 184 additions and 62 deletions
42
lib/ansible/plugins/filter/urlsplit.py
Normal file
42
lib/ansible/plugins/filter/urlsplit.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
# Copyright (c) 2017 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {
|
||||
'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'
|
||||
}
|
||||
|
||||
|
||||
from ansible.errors import AnsibleFilterError
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlsplit
|
||||
from ansible.utils import helpers
|
||||
|
||||
|
||||
def split_url(value, query='', alias='urlsplit'):
|
||||
|
||||
results = helpers.object_to_dict(urlsplit(value), exclude=['count', 'index', 'geturl', 'encode'])
|
||||
|
||||
# If a query is supplied, make sure it's valid then return the results.
|
||||
# If no option is supplied, return the entire dictionary.
|
||||
if query:
|
||||
if query not in results:
|
||||
raise AnsibleFilterError(alias + ': unknown URL component: %s' % query)
|
||||
return results[query]
|
||||
else:
|
||||
return results
|
||||
|
||||
|
||||
# ---- Ansible filters ----
|
||||
class FilterModule(object):
|
||||
''' URI filter '''
|
||||
|
||||
def filters(self):
|
||||
return {
|
||||
'urlsplit': split_url
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue