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

1.2k views Asked by At

I'm trying to enable carbon-aggregator with ansible in CentOS7 enabling systemctl service with ansible variable "ansible_processor_cores". And it doesn't works. Here is role example:

- 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 %}

Error is:

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"}

As I see here is trying to enable some services that I didnt asked:

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

I don't know where they came from. If I'll do that manually it works perfectly like that:

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

Any suggestions?

1

There are 1 answers

0
larsks On BEST ANSWER

To start up a number of services you could use a with_sequence loop. See the Loops documentation for details, although I believe that the documentation for with_sequence may be incorrect.

An example might look something like:

- 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}}

On a system with four cores, the above loop would call the service module four times, with name: set to carbon-aggregator@0, carbon-aggregator@1, carbon-aggregator@2, and carbon-aggregator@3.