2

How to list all the services of a system with state=='running' without providing a list like the first code?

- name: Populate service facts
  service_facts:
  no_log: true
  register: inspect_services2
  when: "ansible_facts.services[] is defined and ansible_facts.services[].state == 'running' "

I have manage to list them only if I use a list:

- name: Running Services 
  debug:
    msg: "{{ ansible_facts.services[item].state == 'running' }}"
  when: "ansible_facts.services[item] is defined and ansible_facts.services[item].state == 'running' "
  loop: "{{ inspect_services2 }}"
  ignore_errors: yes
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
John Kon
  • 47
  • 1
  • 6

2 Answers2

3

In a nutshell:

---
- name: work with service facts
  hosts: localhost

  tasks:
    - name: gather service facts
      service_facts:

    - name: show running services
      debug:
        msg: "{{ ansible_facts.services | dict2items 
          | selectattr('value.state', '==', 'running') | items2dict }}"

This gives you a dict with all info for all running services. If e.g. you only want to display the names of those services, your could change the message in debug task to:

        msg: "{{ ansible_facts.services | dict2items 
          | selectattr('value.state', '==', 'running') | map(attribute='key') }}"

You are of course absolutely free to use that result and put it in a variable somewhere as an alias to reuse it. Below a useless yet functional example creating a file with the service name on the target server just to illustrate:

---
- name: Work with service facts II
  hosts: localhost

  vars:
    # Note1: this will be undefined until service facts are gathered
    # Note2: this time this var will be a list of all dicts
    # dropping the initial key wich is identical to `name`
    running_services: "{{ ansible_facts.services | dict2items 
      | selectattr('value.state', '==', 'running') | map(attribute='value') }}"

  tasks:
    - name: gather service facts
      service_facts:

    - name: useless task creating a file with service name in tmp
      copy:
        content: "ho yeah. {{ item.name }} is running"
        dest: "/tmp/{{ item.name }}.txt"
      loop: "{{ running_services }}"
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • This is an other question. New questions do not go in comment but in... a new question, with a clear explanation of your problem, the description of what you have already tried to fix it and a [minimal example](/help/mcve) to reproduce it. Meanwhile, I don't see any `files` attribute in any of the service definitions returned by `service_facts` (at least on my linux machine) nor any other attribute related to permissions. So I don't really get what you are trying to do here. Hence why you should take the time to ground this to a real life example inside a new post. – Zeitounator Jan 21 '22 at 17:57
2

To list the running services only, just

- name: Loop over all services and print name
  debug:
    msg: "{{ item }}"
  when:
    - ansible_facts.services[item].state == 'running'
  with_items: "{{ ansible_facts.services }}"

Thanks to

U880D
  • 8,601
  • 6
  • 24
  • 40
  • @β.εηοιτ.βε, good hint since it might be more future proof according [Migrating from `with_x` to loop](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#migrating-from-with-x-to-loop) – U880D Jan 21 '22 at 15:56