From beba4c5a1acab36370104baf535164a1e78ca159 Mon Sep 17 00:00:00 2001 From: Chris Conway Date: Mon, 14 Apr 2014 08:25:41 -0700 Subject: [PATCH 1/2] Adds search/match examples to "Playbooks > Variables > Jinja2 Filters" doc section. --- docsite/rst/playbooks_variables.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docsite/rst/playbooks_variables.rst b/docsite/rst/playbooks_variables.rst index ce70daf54ff..8ae0b92c305 100644 --- a/docsite/rst/playbooks_variables.rst +++ b/docsite/rst/playbooks_variables.rst @@ -291,6 +291,18 @@ doesn't know it is a boolean value:: - debug: msg=test when: some_string_value | bool +To match strings against a regex, use the "match" or "search" filter:: + + vars: + foo: abcdefg + + tasks: + - shell: echo "String '{{ foo }}' matches 'abc'" + when: foo | match("abc") + + - shell: echo "String '{{ foo }}' contains 'def'" + when: foo | search("def") + To replace text in a string with regex, use the "regex_replace" filter:: # convert "ansible" to "able" From 0ec5d78e208cebbe236a613d7394663c664e3839 Mon Sep 17 00:00:00 2001 From: Chris Conway Date: Mon, 14 Apr 2014 08:25:41 -0700 Subject: [PATCH 2/2] Modifies match/search filter examples to use non-trivial regexes. --- docsite/rst/playbooks_variables.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docsite/rst/playbooks_variables.rst b/docsite/rst/playbooks_variables.rst index 8ae0b92c305..4bad5a17ce1 100644 --- a/docsite/rst/playbooks_variables.rst +++ b/docsite/rst/playbooks_variables.rst @@ -294,14 +294,14 @@ doesn't know it is a boolean value:: To match strings against a regex, use the "match" or "search" filter:: vars: - foo: abcdefg + url: "http://example.com/users/foo/resources/bar" tasks: - - shell: echo "String '{{ foo }}' matches 'abc'" - when: foo | match("abc") + - debug: 'msg="Resource URI: {{ url }}"' + when: url | match("http://example.com/users/.*/resources/.*") - - shell: echo "String '{{ foo }}' contains 'def'" - when: foo | search("def") + - debug: 'msg="Resource path: {{ url }}"' + when: url | search("/users/.*/resources/.*") To replace text in a string with regex, use the "regex_replace" filter::