0

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.

  • 2
    Why do you need that line? Is there a reason you are not using the [purposed Ansible fact](https://stackoverflow.com/questions/38847824/ansible-how-to-get-service-status-by-ansible)? – β.εηοιτ.βε Apr 04 '23 at 18:36
  • The reason is I am still new with Ansible and haven't even begun to understand everything yet. – user21566539 Apr 04 '23 at 19:32
  • 2
    Then, a sound advice: don't try to do something the wrong way because you do not understand it yet. Rather take the time to make the thing the right way. The most upvoted answer in the question _[Ansible: How to get service status by Ansible?](https://stackoverflow.com/questions/38847824/ansible-how-to-get-service-status-by-ansible)_ is the proper way of doing what you want. – β.εηοιτ.βε Apr 04 '23 at 19:34
  • Other examples of managing services without using the `shell` module are [How to check service ...](https://stackoverflow.com/a/69773111/6771046) or [Ansible: How to get disabled but running services?](https://stackoverflow.com/a/70376280/6771046). – U880D Apr 05 '23 at 06:55

1 Answers1

-1

You could try using a conditional loop to show the specific value from the array twc_result.stdout_lines.

Sample yml:

- name: Status TWC
  shell: "systemctl status twcloud"
  register: twc_result

- name: show specific value
  debug:
    msg: "{{ item }}"
  when: item.find("Active:") != -1
  loop: twc_result.stdout_lines

If you want to store it in a variable and console the output. you could try the below:

- name: Status TWC
  shell: "systemctl status twcloud"
  register: twc_result

- name: show specific value
  set_fact:
    ansible_var: "{{ item }}"
  when: item.find("Active:") != -1
  loop: twc_result.stdout_lines

- name: debug
  debug:
      var: active_var

One negative scenario could be when the condition is failed for every value in the twc_result.stdout_lines array, we will get VARIABLE IS NOT DEFINED for the debug task.

You could learn more about ansible loops and conditionals from here loops and conditionals

Hope this is what you're looking for. Thanks.

vidyasagar
  • 31
  • 2
  • This would not work, as services are not always active. If the service is in a failed state, the poster would not get any output back. – Mercifies Apr 04 '23 at 21:07