简体   繁体   English

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

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

I have list of systemd services defined as我有定义为的systemd服务列表

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

Now I want to stop only a.service from above list.现在我只想停止上面列表中的a.service How this can be achieved using systemd _module?如何使用systemd _module 实现这一点?

What are you trying to achieve?你想达到什么目的? As written, you could just do:如所写,您可以这样做:

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

If instead you mean "the first service", use the first filter or an index:如果您的意思是“第一个服务”,请使用第first过滤器或索引:

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

OR或者

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

Your question is very vague and unspecific.你的问题非常模糊和不具体。 However, parts of your question could be answered with the following approach但是,您的部分问题可以通过以下方法回答

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

You may need to extend and change it for your needs and required functionality.您可能需要根据需要和所需功能对其进行扩展和更改。

Regarding discovering, starting and stopping services via Ansible facts ( service_facts ) and systemd you may have a look into关于通过 Ansible 事实( service_facts )和systemd发现、启动和停止服务,您可以查看

Further readings进一步阅读

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

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