From d64472cd62fd5035118815d4594cf051a009fb53 Mon Sep 17 00:00:00 2001 From: Michael Schlapa Date: Fri, 24 Sep 2021 12:12:43 +0200 Subject: [PATCH] Added on the filter usecase for Ansible --- Ansible/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Ansible/README.md b/Ansible/README.md index f287c03..4bc358e 100644 --- a/Ansible/README.md +++ b/Ansible/README.md @@ -20,6 +20,8 @@ ansible all -l localhost -m systemd -a "name=nginx state=restarted" -u test --be `sample.result_list` is a list of lists containing stdout output. We want to filter some numbers from this output. To do this we first filter the correct lines by using the `json_query` filter. If you need to match a specific substring, you could use `select("match", "")` in combination with the `json_query`. Next we `map` the `regex_search` filter on all lines to extract the data we need. As the `map` filter is a Python generator object, we need to convert it to a list. +The regex `(?<=\()\d+` uses a feature called *positive lookbehind*: `(?<=\()`. It matches a group before the main expression with including it in the result. In this case it matches the opening parantheses `(`. This feature may not be supported in your regex implementation, but in Ansible/Python it is supported. To make the regex even more specific you could add a *positive lookahead* (`(?=\))`) after the `\d+` to match the closing parantheses `)`. + ```yaml --- @@ -37,5 +39,5 @@ ansible all -l localhost -m systemd -a "name=nginx state=restarted" -u test --be tasks: - debug: - msg: '{{ sample.results | json_query("[*].stdout_lines[1]") | map("regex_search" ,"(?<=\()\d*") | list }}' + msg: '{{ sample.results | json_query("[*].stdout_lines[1]") | map("regex_search" ,"(?<=\()\d+") | list }}' ```