0

I am running shell script via playbook

 - name: Get status of Filebeat
   shell: sh /path/get_status.sh "status"
   when: action == "status"

my shell script is get_status.sh

    SERVICE="filebeat"
    if pgrep -x "$SERVICE" >/dev/null
    then
      echo "$SERVICE is running"
    else
      echo "$SERVICE is stopped"

I want this echo statement of shell script to be on ansible output, how can I do?

Priya
  • 173
  • 6
  • 18

1 Answers1

1

Regarding your initial question you may just register the return value.

---
- hosts: test
  become: no
  gather_facts: no

  tasks:

  - name: Verify service status
    shell:
      cmd: "/home/{{ ansible_user }}/test.sh"
      warn: false
    changed_when: false
    check_mode: false
    register: result

  - name: Show result
    debug:
      msg: "{{ result.stdout }}"

resulting into an output of

TASK [Show result] *******
ok: [test1.example.com] =>
  msg: RUNNING

If your service is installed in your system in the same way like other services, a better approach might be to use Ansible build-ins.

If you are running filebeat with systemd, you may use systemd_module.

- name: Make sure service is started
  systemd:
    name: filebeat
    state: started
U880D
  • 8,601
  • 6
  • 24
  • 40
  • This is not installed as a service, I am just running a filebeat binary to start service and kill it to stop. In the above solution how am I running my shell file? I tried cmd: sh /path/get_status.sh "status", it gives output as "msg": "result.stdout" – Priya Nov 30 '21 at 12:01
  • @Priya, regarding your objection "_In the above solution how am I running my shell file?_", you may just call it within the command line. I am update the example accordingly. For debugging puprose you may also change the message to `msg: "{{ result }}"`. – U880D Nov 30 '21 at 12:28