I'm trying to use service_facts to report if a service is in a running state, then email that out via a Jinja template. To grab the service info I'm using the following:
- name: get service facts
service_facts:
- name: services check
debug:
var: ansible_facts.services['{{ item }}.service'].state
loop: "{{ server_facts.services|default (servicescheck) }}"
register: servicestatus
which gives me the following:
{
"ansible_facts.services['firewalld.service'].state": "running",
"_ansible_verbose_always": true,
"_ansible_no_log": false,
"changed": false,
"item": "firewalld",
"ansible_loop_var": "item",
"_ansible_item_label": "firewalld"
}
and the registered variable looks like:
{
"msg": [
{
"results": [
{
"ansible_facts.services['firewalld.service'].state": "running",
"failed": false,
"changed": false,
"item": "firewalld",
"ansible_loop_var": "item"
},
{
"ansible_facts.services['sshd.service'].state": "running",
"failed": false,
"changed": false,
"item": "sshd",
"ansible_loop_var": "item"
}
],
"msg": "All items completed",
"changed": false
}
],
"_ansible_verbose_always": true,
"_ansible_no_log": false,
"changed": false
}
I'm able to pull out the service name with servicestatus.results.0.item in a debug task, and in the Jinja template I'm building out.
However, pulling the service state has be stumped.
I have tried the following without any success:
"{{ servicestatus.results.0.ansible_facts.services.state }}""{{ ansible_facts.services['{{ server_facts.services }}.service.state'] }}"
Obviously, I'm pretty clueless on how to pull the service state out of the variable.
The Jinja template looks like this, currently, which fails since the state variable is wrong:
Service Checks:
{% for result in servicestatus.results %}
Service: {{ result.item }}
State: {{ ansible_facts.services['{{ server_facts.services }}.service'].state }}
{% endfor %}