[英]Start many processes using systemd ansible
I want to start N processes with the systemd
module, let's suppose I have the following service:我想用
systemd
模块启动 N 个进程,假设我有以下服务:
# file: /etc/systemd/system/sleep@.service
[Service]
ExecStart=/bin/sleep 420
[Install]
WantedBy=multi-user.target
I want to start 3 processes with the systemd module:我想用 systemd 模块启动 3 个进程:
- name: Start Systemd Unit
systemd:
name: 'sleep@{1..3}.service'
state: 'started'
The task above creates only one encoded process sleep@\\x7b1..3\\x7d.service
, I can't figure out how to not encode the string.上面的任务只创建了一个编码进程
sleep@\\x7b1..3\\x7d.service
,我不知道如何不对字符串进行编码。 Does ansible support creating multiple processes? ansible是否支持创建多进程? if not, what is the easiest way to do so?
如果没有,最简单的方法是什么?
My current workaround defines a list of services that have an instances
key, using the subelements
I'm able to create those processes separately.我当前的解决方法定义了一个具有
instances
键的服务列表,使用我能够单独创建这些进程的subelements
。 I think there is a better way to it.我认为有更好的方法。
app_systemd_services:
- name: sleep
instances: "{{ range(3) | list }}"
- name: Ensure Systemd Units are started
systemd:
name: "{{ item.0.name }}@{{ item.1 }}.service"
state: started
loop: "{{ app_systemd_services | subelements('instances') }}"
Ansible does not support ranges (...). Ansible 不支持范围 (...)。 Use
with_items
.使用
with_items
。
- name: Start Systemd Unit
systemd:
name: 'sleep@{{ item }}.service'
state: started
with_items:
- 1
- 2
- 3
One more unsolicited advice.还有一个不请自来的建议。 If you use templates for systemd, use
.target
unit with dependency.如果您使用 systemd 的模板,请使用具有依赖关系的
.target
单元。
Template for foo@.service
should say Wanted-by: foo.target
, and than you can manage them by systemctl stop/start/restart foo.target
. foo@.service
的模板应该是Wanted-by: foo.target
,然后你可以通过systemctl stop/start/restart foo.target
来管理它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.