Home > OS >  How to return a message form shell script to Ansible playbook to Display as output
How to return a message form shell script to Ansible playbook to Display as output

Time:12-01

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?

CodePudding user response:

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
  • Related