'Ansible service_facts filtering by partial name

I have some linux servers and want check state of services. I can easily check all processes, single process by name, but how to check for group of services with common part of name?

  1. Host1 have: company1.service, company2.service, company3.service
  2. Host2 have: company1.service, company3.service, company4.service
  3. Host3 have: company2.service, company3.service, company4.service

I dont want hardcode this services to each host due to often changes of services sets, and thats why i need filter by partial name.



Solution 1:[1]

Your question is somehow vague and unspecific. However, parts of your question could be answered with the following approach.

According your description

How to check for group of services with common part of name?

I understand that you like to check the service on a server only if a server belongs to certain group of hosts or domain in your inventory file (and if "services" are organized structured in inventory).

---
- hosts: test
  become: true
  gather_facts: true

  tasks:

  - name: Gathering Service Facts
    service_facts:
    when: "'test' in group_names and 'www.example.com' in ansible_domain"

  - name: Show result
    debug:
      var: services

resulting into an output of

TASK [Gathering Facts] *************
ok: [db.example.com]
ok: [www.example.com]

TASK [Gathering Service Facts] *****
ok: [www.example.com]

TASK [Show result] *****************
ok: [db.example.com] =>
  services: VARIABLE IS NOT DEFINED!
ok: [www.example.com] =>
  services:
    ...

Further Q&A

According your question

Ansible service_facts filtering by partial name

if you want to check for results ... from list of services using Ansible you may have a look into the linked answered or

Further Q&A

If all the information and links here do not answer your question you may need to provide much more information and description about your use case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1