0

I'm trying to write some checks into an Ansible playbook to check and report on the status of major running services on a given server. The relevant bits of code are as follows:

Variables for purposes of test:

services: "{{ ['haproxy', 'keepalived'] if server_role == 'haproxy' }}" 

The first task loops over each item in the service and logs as a list the result of a check on the server status:

- name: Check Initial Service Status command: "systemctl is-active {{ item }}" register: service_status_before ignore_errors: yes loop: "{{ services }}" loop_control: loop_var: item when: server_role != 'n/a' 

This works without problems and logs all information relating to the command correctly into the list items. The second task should loop over every item in the previously registered list, and display a message - including the name of the itself, depending on the contents of stdout, and and here is where I'm having problems:

- name: "Display Initial Service Status" debug: msg: '{{ "item.item - Service running" if "active" in item.stdout else "item.item - Service is NOT running!" }}' loop: "{{ service_status_before.results }}" loop_control: loop_var: item when: server_role != 'n/a' 

This gives me the following output:

"msg": "item.item - Service running" 

I've tried just about every combination of single/double quotes and curly brace placement I can think of, but either the Item name fails to translate (the below instance pairs directly to the above code snippet), or I can make the variable evaluate successfully, but the rest of the conditional clause is printed verbatim:

 "msg": "'haproxy - Service running' if 'active' in item.stdout else 'haproxy - Service is NOT running!'" "msg": "'keepalived - Service running' if 'active' in item.stdout else 'keepalived - Service is NOT running!'" 

Can anybody tell me what I'm doing wrong? I'm struggling to find any combination of syntax which I don't think I've already tried.

Thanks.

2 Answers 2

2

Put the services into a dictionary. For example,

 services: haproxy: [docker, haproxy, keepalived] default: [] 

Given the inventory for testing

shell> cat hosts localhost server_role=haproxy 

The below task will iterate the list of services, get the active status, and register results in the variable service_status_before

 - command: "systemctl is-active {{ item }}" register: service_status_before ignore_errors: yes loop: "{{ services[server_role]|d(services.default) }}" 

Declare the dictionary of the services and their active status

 services_inactive: "{{ service_status_before.results| items2dict(key_name='item', value_name='failed') }}" 

gives for example,

 services_inactive: docker: false haproxy: true keepalived: true 

Now, the report is trivial

 - debug: msg: "{{ item.key }} {{ item.value|ternary('is NOT', 'is') }} running." with_dict: "{{ services_inactive }}" 

gives (abridged)

 msg: docker is running. msg: haproxy is NOT running. msg: keepalived is NOT running. 

Example of a complete playbook for testing

- hosts: all vars: services: haproxy: [docker, haproxy, keepalived] default: [] services_inactive: "{{ service_status_before.results| items2dict(key_name='item', value_name='failed') }}" tasks: - debug: var: server_role - command: "systemctl is-active {{ item }}" register: service_status_before ignore_errors: yes loop: "{{ services[server_role]|d(services.default) }}" - debug: var: services_inactive - debug: msg: "{{ item.key }} {{ item.value|ternary('is NOT', 'is') }} running." with_dict: "{{ services_inactive }}" 
1
  • Thanks for this, I ended finding a simpler fix for my existing syntax (in comment below) that didn't need such an extensive rewrite, so I unfortunately haven't tried this out, but appreciate the detailed reply. I've not really ever had a requirements to use dictionaries before in Ansible playbooks, but this has definitely given me food for thought for future playbook writing. Commented Oct 6, 2023 at 13:42
-1

Now found a working one line fix using concatenation, see below:

- name: "Display Initial Service Status" debug: msg: '{{ item.item ~ " - Service running" if "active" in item.stdout else item.item ~ " - Service is NOT running!" }}'** loop: "{{ service_status_before.results }}" loop_control: loop_var: item when: server_role != 'n/a' 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.