diff --git a/Ansible/README.md b/Ansible/README.md index 060589c..f287c03 100644 --- a/Ansible/README.md +++ b/Ansible/README.md @@ -13,3 +13,29 @@ ansible -l -m -a "" ``` ansible all -l localhost -m systemd -a "name=nginx state=restarted" -u test --become --ask-become-pass ``` + +## Usecases + +### Filter numbers from a list of strings + +`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. + +```yaml +--- + +- hosts: localhost + + vars: + sample: + result_list: + - stdout_lines: + - 'line0' + - 'line12 (2319)' + - stdout_lines: + - 'line2' + - 'line12 (2320)' + + tasks: + - debug: + msg: '{{ sample.results | json_query("[*].stdout_lines[1]") | map("regex_search" ,"(?<=\()\d*") | list }}' +```