任务问题确保 Nginx 已停止

problem with task Ensure that Nginx is stopped

我为 运行 reactos 应用程序写了一个角色,问题发生在单个任务中以确保 Nginx 已停止。我收到一个奇怪的错误

如果我从角色中删除这一点,应用程序将成功关闭,但我希望它也能与它一起工作 上一个任务效果很好

- name: Ensure nginx is not installed
  apt:
    name: nginx
    state: absent

但这不起作用

- name: Ensure that Nginx is stopped
  ansible.builtin.systemd:
    name: nginx
    state: stopped

如何解决问题

使用服务。它在大多数用例中都是兼容的。

Service - 控制远程主机上的服务。支持的 init 系统包括 BSD init、OpenRC、SysV、Solaris SMF、systemd、upstart。

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html

---

- hosts: localhost
  gather_facts: false
  become: true
  tasks:
    - name: Ensure nginx is installed
      apt:
        name: nginx
        state: latest

    - name: service started
      service:
        name: nginx
        state: started

    - name: service stopped
      service:
        name: nginx
        state: stopped

sysvinit - 控制使用 SysV init 系统的目标主机上的服务。 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/sysvinit_module.html

---

- hosts: localhost
  gather_facts: false
  become: true
  tasks:
    - name: Ensure nginx is installed
      apt:
        name: nginx
        state: latest

    - name: sysvinit started
      sysvinit:
        name: nginx
        state: started

    - name: sysvinit stopped
      sysvinit:
        name: nginx
        state: stopped

Systemd 要求:由 systemd 管理的系统。 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html

---

- hosts: localhost
  gather_facts: false
  become: true
  tasks:
    - name: Ensure nginx is installed
      apt:
        name: nginx
        state: latest
    
    # this one fails
    - name: systemd started - fail
      systemd:
        name: nginx
        state: started

    # this one fails
    - name: systemd stopped - fail
      systemd:
        name: nginx
        state: stopped