-2

I have list of systemd services defined as

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

Now I want to stop only a.service from above list. How this can be achieved using systemd_module?

U880D
  • 8,601
  • 6
  • 24
  • 40
Sam
  • 97
  • 1
  • 10
  • 3
    When you say `only a.service` do you mean `the first service in the list` ? Moreover, what have you tried so far and what is the exact problem you have ? – Zeitounator Jan 04 '22 at 17:45

2 Answers2

1

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

Further readings

U880D
  • 8,601
  • 6
  • 24
  • 40
1

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:

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

OR

- name: Stop first service
  systemd:
    name: "{{ systemd_scripts[0] }}"
    state: stopped
Jiří Baum
  • 6,697
  • 2
  • 17
  • 17