繁体   English   中英

使用 Ansible 从服务列表中仅启动特定的 systemd 服务

[英]Start only specific systemd service from list of services using Ansible

我有定义为的systemd服务列表

vars:
  systemd_scripts: ['a.service', 'b.service', 'c.service']

现在我只想停止上面列表中的a.service 如何使用systemd _module 实现这一点?

你想达到什么目的? 如所写,您可以这样做:

- name: Stop service A
  systemd:
    name: a.service
    state: stopped

如果您的意思是“第一个服务”,请使用第first过滤器或索引:

- name: Stop first service
  systemd:
    name: "{{ systemd_scripts | first }}"
    state: stopped

或者

- name: Stop first service
  systemd:
    name: "{{ systemd_scripts[0] }}"
    state: stopped

你的问题非常模糊和不具体。 但是,您的部分问题可以通过以下方法回答

- name: Loop over service list and stop one service
  systemd:
    name: "{{ item }}"
    state: stopped
  when:
    - systemd_scripts[item] == 'a.service'
  with_items: "{{ systemd_scripts }}"

您可能需要根据需要和所需功能对其进行扩展和更改。

关于通过 Ansible 事实( service_facts )和systemd发现、启动和停止服务,您可以查看

进一步阅读

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM