1

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 %}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
d1530
  • 151
  • 8
  • Additionally to the already given answer you may take advantage from [Ansible: How to get disabled but running services?](https://stackoverflow.com/a/70376280/6771046). – U880D Jun 08 '22 at 06:06

1 Answers1

1

There is no need to have an intermediary variable and surely not to use the result of a debug task to do that, your template should be as simple as:

Service Checks:
{% for result in server_facts.services | default(servicescheck) %}
Service: {{ result }}
State:   {{ ansible_facts.services[result ~ '.service'].state }} 
{% endfor %}

Because, ‘moustaches don’t stack’.


Regarding the usage of register in a debug task, this is a non-sense that you should banish from your playbooks.
Registering a variable should be achieved with the set_fact module.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83