我嘗試從 Ansible playbook 安裝 NGINX。
目前我使用這個劇本來安裝它:
---
- hosts: all
gather_facts: true
become: true
become_user: root
tasks:
- apt:
name: nginx
when: ansible_os_family == "Debian"
它有效:
Identity added: /runner/artifacts/46/ssh_key_data (root@debian)
SSH password:
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.75]
TASK [apt] *********************************************************************
changed: [192.168.1.75]
PLAY RECAP *********************************************************************
192.168.1.75 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
但我想在我的劇本末尾看到 NGINX 服務的狀態,如下所示:
Identity added: /runner/artifacts/46/ssh_key_data (root@debian)
SSH password:
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.75]
TASK [apt] *********************************************************************
changed: [192.168.1.75]
TASK [Verify nginx state] *********************************************************************
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-02-14 17:59:13 CET; 20s ago
Docs: man:nginx(8)
Process: 1723 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 1724 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 1907 (nginx)
Tasks: 2 (limit: 445)
Memory: 5.6M
CPU: 99ms
CGroup: /system.slice/nginx.service
├─1907 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─1910 nginx: worker process
PLAY RECAP *********************************************************************
192.168.1.75 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
或類似的東西。
如何做到這一點?
uj5u.com熱心網友回復:
關于您的要求
我想在
nginx我的劇本結束時看到服務的狀態
建議只使用該systemd模塊。
- name: Make sure 'nginx' is started
systemd:
name: nginx
state: started
enabled: yes
register: result
- name: Show result
debug:
msg: "{{ result }}"
為了以后檢查您可以使用的特定服務service_facts。
- name: Gathering service facts
service_facts:
- name: Get facts
debug:
msg:
- "{{ ansible_facts.services['nginx'].status }}"
uj5u.com熱心網友回復:
您可以從此處的 sudo 命令獲取該詳細資訊
- name: check for service status
shell: sudo service nginx status
ignore_errors: true
register: nginxstatus
- name: Show service service status
debug:
msg: 'nginxstatus exists.'
when: nginxstatus.rc | int == 0
uj5u.com熱心網友回復:
甚至更好
- name: Check status of the nginx server
shell: 'systemctl nginx status'
register: command_output
- debug:
var: command_output.stdout_lines
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428586.html
上一篇:如何使用NGINX托管多個Create-React-App生產構建,每個構建在一個單獨的目錄中(沒有Docker)
