What i want to achieve:
Get the list of running docker services on a specific node in ansible.
How I tried to achieve this:
---
- name: echo text (this will later be docker stack ps SERVICE instead of echo)
shell: echo 'SomePrefix_value1.1\nSomePrefix_value2.1'
register: result
- name: Print values
debug:
msg: '{{ item | regex_search(regexp) }}'
with_items: result.stdout_lines
vars:
regexp: 'SomePrefix_([^\.]+)'
In a first shot I just wanted to provide some example output and print out the desired values. But when I run those tasks I get just an empty String printed out.
TASK [test-list : Print values] *****************************************************************************
ok: [127.0.0.1] => (item=None) => {
"msg": ""
}
I tried to set regexp to '.*'
to verify that the problem is in the regexp end the task is successfully with this regexp and it prints the following:
TASK [test-list : Print values] *****************************************************************************
ok: [127.0.0.1] => (item=None) => {
"msg": "result.stdout_lines"
}
I thought it has something to do with escaping and therefore I printed out the value of regexp and got the following output:
TASK [test-list : Print regexp] *****************************************************************************
ok: [127.0.0.1] => (item=None) => {
"regexp": "SomePrefix_([^\\.]+)"
}
I was wondering about the second \
in the regexp, but after reading a bit it turned out that this is just in the debug output.
I don't know what I'm doing wrong. On https://regex101.com my regex works fine. Can anybody give me a hint?
Solved by the question that is marked as duplication
I had to adapt with_items to be with_items: '{{ result.stdout_lines }}'