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.