通过Ansible攻略手册获得NGINX地位

u5rb5r59  于 2022-12-11  发布在  Nginx
关注(0)|答案(3)|浏览(183)

我尝试从Ansible剧本安装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

或者类似的东西。
如何才能做到这一点?

wooyq4lh

wooyq4lh1#

甚至更好

- name: Check status of the nginx server
  shell: 'systemctl nginx status'
  register: command_output

- debug:
         var: command_output.stdout_lines
xdnvmnnf

xdnvmnnf2#

关于你的要求

  • 我希望在行动手册结尾处看到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 }}"
b4wnujal

b4wnujal3#

您可以从此处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

相关问题