From bf81c101e7efc337bc76e2c3f01842547f4adc2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=8D=9A=E4=BB=81=20Buo-ren=20Lin?= Date: Tue, 1 Oct 2024 04:49:17 +0800 Subject: [PATCH] playbooks_filters: Mention the usage of the ansible.builtin.split filter (#1929) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * playbooks_filters: Mention the usage of the ansible.builtin.split filter This is a filter I frequently used in my project, it should mentioned in the documentation so that it can be easily recognized by users. Fixes #1928. * Update docs/docsite/rst/playbook_guide/playbooks_filters.rst Fix inconsistent heading markup style. Co-authored-by: Felix Fontein * playbooks_filters: Fix incorrect _dict_filter label location Signed-off-by: 林博仁(Buo-ren Lin) * playbooks_filters: Fix incorrect argument of the split builtin filter Signed-off-by: 林博仁(Buo-ren Lin) * playbooks_filters: Fix incorrect example input and output Signed-off-by: 林博仁(Buo-ren Lin) * Update docs/docsite/rst/playbook_guide/playbooks_filters.rst Increase verbosity. Co-authored-by: Felix Fontein --------- Signed-off-by: 林博仁(Buo-ren Lin) Co-authored-by: Felix Fontein --- .../rst/playbook_guide/playbooks_filters.rst | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/docsite/rst/playbook_guide/playbooks_filters.rst b/docs/docsite/rst/playbook_guide/playbooks_filters.rst index b9f475a4cd..bb15adb6f3 100644 --- a/docs/docsite/rst/playbook_guide/playbooks_filters.rst +++ b/docs/docsite/rst/playbook_guide/playbooks_filters.rst @@ -116,6 +116,29 @@ If you are unsure of the underlying Python type of a variable, you can use the : You should note that, while this may seem like a useful filter for checking that you have the right type of data in a variable, you should often prefer :ref:`type tests `, which will allow you to test for specific data types. +Transforming strings into lists +------------------------------- + +Use the :ansplugin:`ansible.builtin.split#filter` filter to transform a character/string delimited string into a list of items suitable for :ref:`looping `. For example, if you want to split a string variable `fruits` by commas, you can use: + +.. code-block:: yaml+jinja + + {{ fruits | split(',') }} + +String data (before applying the :ansplugin:`ansible.builtin.split#filter` filter): + +.. code-block:: yaml + + fruits: apple,banana,orange + +List data (after applying the :ansplugin:`ansible.builtin.split#filter` filter): + +.. code-block:: yaml + + - apple + - banana + - orange + .. _dict_filter: Transforming dictionaries into lists