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

问题描述 投票:0回答:2

我有

systemd
服务列表

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

现在我只想停止上面列表中的

a.service
。这如何使用
systemd
_module来实现?

ansible systemd
2个回答
1
投票

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

- 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

0
投票

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

- 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
你可以看看

进一步阅读

© www.soinside.com 2019 - 2024. All rights reserved.