Search code examples
ansiblesystemd

Start only specific systemd service from list of services using Ansible


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?


Solution

  • 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