From 13d94ed6812c5c11f2f2d17bc4fa3f1c888ed83b Mon Sep 17 00:00:00 2001 From: Satyajit Bulage Date: Fri, 11 Jan 2019 15:09:42 +0530 Subject: [PATCH] Update filter documentation (#50255) Added documentation for following filters - * Product * human_readable * human_to_bytes Signed-off-by: Satyajit Bulage --- .../rst/user_guide/playbooks_filters.rst | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/docs/docsite/rst/user_guide/playbooks_filters.rst b/docs/docsite/rst/user_guide/playbooks_filters.rst index d489f11fcba..e35c4b827f8 100644 --- a/docs/docsite/rst/user_guide/playbooks_filters.rst +++ b/docs/docsite/rst/user_guide/playbooks_filters.rst @@ -1261,6 +1261,24 @@ Combinations always require a set size:: Also see the :ref:`zip_filter` +Product Filters +``````````````` + +The product filter returns the `cartesian product `_ of the input iterables. + +This is roughly equivalent to nested for-loops in a generator expression. + +For example:: + + - name: generate multiple hostnames + debug: + msg: "{{ ['foo', 'bar'] | product(['com']) | map('join', '.') | join(',') }}" + +This would result in:: + + { "msg": "foo.com,bar.com" } + + Debugging Filters ````````````````` @@ -1273,6 +1291,58 @@ type of a variable:: {{ myvar | type_debug }} +Computer Theory Assertions +``````````````````````````` + +The ``human_readable`` and ``human_to_bytes`` functions let you test your +playbooks to make sure you are using the right size format in your tasks - that +you're providing Byte format to computers and human-readable format to people. + +Human Readable +`````````````` + +Asserts whether the given string is human readable or not. + +For example:: + + - name: "Human Readable" + assert: + that: + - '"1.00 Bytes" == 1|human_readable' + - '"1.00 bits" == 1|human_readable(isbits=True)' + - '"10.00 KB" == 10240|human_readable' + - '"97.66 MB" == 102400000|human_readable' + - '"0.10 GB" == 102400000|human_readable(unit="G")' + - '"0.10 Gb" == 102400000|human_readable(isbits=True, unit="G")' + +This would result in:: + + { "changed": false, "msg": "All assertions passed" } + +Human to Bytes +`````````````` + +Returns the given string in the Bytes format. + +For example:: + + - name: "Human to Bytes" + assert: + that: + - "{{'0'|human_to_bytes}} == 0" + - "{{'0.1'|human_to_bytes}} == 0" + - "{{'0.9'|human_to_bytes}} == 1" + - "{{'1'|human_to_bytes}} == 1" + - "{{'10.00 KB'|human_to_bytes}} == 10240" + - "{{ '11 MB'|human_to_bytes}} == 11534336" + - "{{ '1.1 GB'|human_to_bytes}} == 1181116006" + - "{{'10.00 Kb'|human_to_bytes(isbits=True)}} == 10240" + +This would result in:: + + { "changed": false, "msg": "All assertions passed" } + + A few useful filters are typically added with each new Ansible release. The development documentation shows how to extend Ansible filters by writing your own as plugins, though in general, we encourage new ones to be added to core so everyone can make use of them.