使用 {0..{{ansible_processor_cores}}} 启用 systemctl 服务的 CentOS7 中的 Ansible 不起作用

Ansible in CentOS7 enabling systemctl service with {0..{{ansible_processor_cores}}} doesn't works

我正在尝试在 CentOS7 中启用带有 ansible 的碳聚合器,从而启用带有 ansible 变量的 systemctl 服务 "ansible_processor_cores"。它不起作用。 这是角色示例:

- name: enable carbon-aggregator
  service: 
    name: 'carbon-aggregator@{0..{{ansible_processor_cores -3}}}'
    enabled: yes
    state: started
    daemon_reload: yes

Carbon.conf.j2:

{% for aggr in range(ansible_processor_cores -2) %} 
[aggregator:{{aggr}}]
{% endfor %}

错误是:

FAILED! => {"changed": false, "failed": true, "msg": "Unable to start service carbon-aggregator@{0..1}: Job for carbon-aggregator@\x7b0..1\x7d.service failed because the control process exited with error code. See \"systemctl status \"carbon-aggregator@\x7b0..1\x7d.service\"\" and \"journalctl -xe\" for details.\n"}

我在这里看到的是尝试启用一些我没有要求的服务:

carbon-aggregator@\x7b0..1\x7d.service

我不知道他们来自哪里。 如果我手动执行此操作,它会像这样完美地工作:

sudo systemctl enable carbon-aggregator@{0..1}

有什么建议吗?

要启动许多服务,您可以使用 with_sequence 循环。有关详细信息,请参阅 Loops 文档,但我认为 with_sequence 的文档可能不正确。

示例可能类似于:

- name: enable carbon-aggregator
  service: 
    name: 'carbon-aggregator@{{ item }}'
    enabled: yes
    state: started
    daemon_reload: yes
  with_sequence: start=0 end={{ansible_processor_cores-1}}

在有四个内核的系统上,上面的循环会调用 service 模块四次,其中 name: 设置为 carbon-aggregator@0carbon-aggregator@1carbon-aggregator@2, 和 carbon-aggregator@3.