我正在通過劇本運行 shell 腳本
- name: Get status of Filebeat
shell: sh /path/get_status.sh "status"
when: action == "status"
我的 shell 腳本是 get_status.sh
SERVICE="filebeat"
if pgrep -x "$SERVICE" >/dev/null
then
echo "$SERVICE is running"
else
echo "$SERVICE is stopped"
我希望 shell 腳本的這個 echo 陳述句出現在 ansible 輸出上,我該怎么做?
uj5u.com熱心網友回復:
關于你最初的問題,你可能只是注冊的回傳值。
---
- 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 }}"
導致輸出
TASK [Show result] *******
ok: [test1.example.com] =>
msg: RUNNING
- 使用 Ansible 顯示遠程命令的輸出
如果您的服務以與其他服務相同的方式安裝在您的系統中,則更好的方法可能是使用 Ansible 內置程式。
- 如何在 Ansible 中檢查服務是否存在
- 使用 Ansible 檢查服務是否存在
- 如何通過 Ansible 獲取服務狀態
如果您使用 systemd運行filebeat,則可以使用systemd_module。
- name: Make sure service is started
systemd:
name: filebeat
state: started
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/372614.html
上一篇:嘗試使用jq搜索陣列時,陣列和字串無法進行包含檢查錯誤
下一篇:sed:具有反向前瞻匹配的嵌套組
