I need to print only a portion of the stdout
of a command.
Here is the play
- name: Status TWC
shell: "systemctl status twcloud"
register: twc_result
- debug:
var: twc_result.stdout_lines
The results look like this
twc_result.stdout_lines": [
"● twcloud.service - No Magic Teamwork Cloud",
" Loaded: loaded (/etc/systemd/system/twcloud.service; enabled; vendor preset: disabled)",
" Active: active (running) since Tue 2023-04-04 16:33:54 UTC; 2s ago",
" Main PID: 2713958 (java)",
" Tasks: 59 (limit: 823113)",
" Memory: 250.5M",
" CGroup: /system.slice/twcloud.service",
" └─2713958 /etc/alternatives/jre_11/bin/java -Xmx8192m -Desi.system.config=configuration/application.conf -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl -Dlogback.configurationFile=configuration/logback.xml -Desi.shiro.config=configuration/shiro.ini -Dfile.encoding=utf-8 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=2468 -Dcom.sun.management.jmxremote.rmi.port=2468 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED -Djava.io.tmpdir=/home/twcloud/.twcloud/2022x/tmp -classpath
And it goes on and on.
All I want is the output from this one line:
" Active: active (running) since Tue 2023-04-04 16:33:54 UTC; 2s ago",
How do I print only this? I don't need any of that other jibberish printed.
I've tried many combinations of regex that I have found online, but I must not be doing them right. They either undefine the variable or break the play entirely.
Update: Before I read any of these I figured it out myself. To get the line I needed I just had to modify the debug module.
- debug:
msg: "{{ twc_result.stdout_lines[2] }}"
I will look into your solutions as well though. Thank you.