【问题标题】:Get NGINX status with an Ansible playbook使用 Ansible playbook 获取 NGINX 状态
【发布时间】:2022-02-15 05:44:07
【问题描述】:

我尝试从 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   

或类似的东西。
如何实现?

【问题讨论】:

    标签: nginx ansible


    【解决方案1】:

    关于您的要求

    我想在我的剧本结束时查看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 }}"
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 当有专门的模块或事实时,在 Ansible 中挤压命令不是这种方式,请参阅@U880D 的回答。
      • 这就是你的反馈,我需要找一本“zen of ansible”风格的书
      【解决方案3】:

      甚至更好

      - name: Check status of the nginx server
        shell: 'systemctl nginx status'
        register: command_output
      
      - debug:
               var: command_output.stdout_lines
      

      【讨论】:

      • 如果您有多种方法可以获得相同的结果,请将其发布在您之前答案的edit 中,而不是复制答案。请注意,SO 是问答,而不是论坛。
      • 谢谢,我没有看到那个编辑按钮。我以后会做的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2017-09-18
      • 2021-01-17
      相关资源
      最近更新 更多