简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM