I want to filter service statuses of specific services on my Linux nodes, displaying this to whoever is running the playbook. I'm able to bring back all services but this is far too much especially as it will be running on multiple servers. I only need to bring back two or three service statues per server.
I am using the variable in my inventory main_services
in other playbooks, and think I should be able to use it for this as well. Just not sure how to get it into json_query
?
Here is my inventory file:
[Site1]
AWS-APPA ansible_host=ip-172-33-53-87.eu-west-1.compute.internal main_services='["httpd.service", "postfix.service"]'
AWS-APPB ansible_host=ip-172-33-53-89.eu-west-1.compute.internal main_services='["httpd.service", "postfix.service"]'
AWS-DB01 ansible_host=ip-172-33-29-194.eu-west-1.compute.internal main_services='["mariadb.service", "sshd.service"]'
Here is my current playbook:
- hosts: all
tasks:
- name: System services status
service_facts:
register: services_running
- name: Get running services
set_fact:
services_runnig: "{{ ansible_facts | json_query('services.* | [?state == `running`].name') }}"
- name: Services
debug:
msg: "{{ services_running }}"