Document some more filters (#78548)

pull/78792/head
Felix Fontein 2 years ago committed by GitHub
parent e276770ee9
commit cbffb77f57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,47 @@
DOCUMENTATION:
name: dict2items
author: Ansible core team
version_added: "2.6"
short_description: takes a dictionary and transforms it into a list of dictionaries
description:
- Takes a dictionary and transforms it into a list of dictionaries, with each having a
C(key) and C(value) keys that correspond to the keys and values of the original.
options:
_input:
description:
- The dictionary to transform
type: dict
required: true
key_name:
description: Configure the key to use instead of C(key).
type: str
default: key
version_added: "2.8"
value_name:
description: Configure the key to use instead of C(value).
type: str
default: value
version_added: "2.8"
seealso:
- plugin_type: filter
plugin: ansible.builtin.items2dict
EXAMPLES: |
- name: Convert a dictionary into a list of dictionaries
debug:
msg: "{{ files | dict2items(key_name='file', value_name='path') }}"
vars:
files:
users: /etc/passwd
groups: /etc/group
# The output is a list of dictionaries:
# - file: users
# path: /etc/passwd
# - file: groups
# path: /etc/group
RETURN:
_value:
description: A list of dictionaries.
type: list
elements: dict

@ -0,0 +1,47 @@
DOCUMENTATION:
name: items2dict
author: Ansible core team
version_added: "2.7"
short_description: convert a list of one-element dictionaries into a dictionary
description:
- Takes a list of dicts with each having a C(key) and C(value) keys, and transforms the list into a dictionary,
effectively as the reverse of R(dict2items,ansible_collections.ansible.builtin.dict2items_filter).
options:
_input:
description:
- A list of dictionaries.
- Every dictionary must have keys C(key) and C(value).
type: list
elements: dict
required: true
key_name:
description: Configure the key to use instead of C(key).
type: str
default: key
value_name:
description: Configure the key to use instead of C(value).
type: str
default: value
seealso:
- plugin_type: filter
plugin: ansible.builtin.dict2items
EXAMPLES: |
- name: Convert list of key-value pairs to dictionary
debug:
msg: "{{ tags | items2dict }}"
vars:
tags:
- key: Application
value: payment
- key: Environment
value: dev
# The output is a dictionary with two key/value pairs:
#
# Application: payment
# Environment: dev
RETURN:
_value:
description: The resulting dictionary.
type: dict

@ -0,0 +1,21 @@
DOCUMENTATION:
name: path_join
author: Anthony Bourguignon (@Toniob)
version_added: "2.10"
short_description: join one or more path components
description:
- Returns a path obtained by joining one or more path components.
options:
_input:
description: A path, or a list of paths.
type: any
required: true
EXAMPLES: |
# If path == 'foo/bar' and file == 'baz.txt', the result is '/etc/foo/bar/subdir/baz.txt'
{{ ('/etc', path, 'subdir', file) | path_join }}
RETURN:
_value:
description: The concatenated path.
type: path

@ -0,0 +1,22 @@
DOCUMENTATION:
name: realpath
author: darkone23 (@darkone23)
version_added: "1.8"
short_description: get canonical path for the given file
description:
- Returns the canonical path for the given file. This is done by eliminating symbolic
links encountered in the path.
options:
_input:
description: A path to a file.
type: path
required: true
EXAMPLES: |
# To get the real path of a link
{{ mypath | realpath }}
RETURN:
_value:
description: The canonical path.
type: path

@ -0,0 +1,29 @@
DOCUMENTATION:
name: relpath
author: Jakub Jirutka (@jirutka)
version_added: "1.7"
short_description: get relative path of the given file
description:
- Returns the relative path of the given file to the current directory if I(start) is not specified,
or relative to the directory given in I(start).
positional: start
options:
_input:
description: A path.
type: path
required: true
start:
description: The directory the path should be relative to.
type: path
EXAMPLES: |
# Get the relative path to 'mypath' from 'mydir'
{{ mypath | relpath(mydir) }}
# Returns '../foo/bar.txt'
{{ '/tmp/foo/bar.txt' | relpath('/tmp/baz/') }}
RETURN:
_value:
description: The relative path.
type: path

@ -0,0 +1,30 @@
DOCUMENTATION:
name: splitext
author: Matt Martz (@sivel)
version_added: "2.0"
short_description: split a filename into root and file extension
description:
- Returns a tuple consisting of C(root) and C(extension), where C(root ~ extension) equals the filename.
options:
_input:
description:
- A filename.
- Path components contained in the filename will be returned as part of the root.
type: str
required: true
EXAMPLES: |
# with path == 'nginx.conf' the return would be ('nginx', '.conf')
{{ path | splitext }}
# with path == 'nginx.conf' the return would be 'nginx'
{{ path | splitext | first }}
# with path == 'nginx.conf' the return would be '.conf'
{{ path | splitext | last }}
RETURN:
_value:
description: A tuple consisting of root and the extension.
type: tuple
elements: str
Loading…
Cancel
Save