I am trying to filter out a line out of Ansible stdout_lines
result. The output script is then sent by email. Cannot get the display format I wanted. Required output format:
host1:
Service1: Status
Service2: Status
host2:
Service1: Status
Service2: Status
My playbook is below:
- hosts: [MyServers]
tasks:
- name: Run a script
win_shell: Get-Service -DisplayName Hyper-V* | Select-Object DisplayName, Status
register: result
- name: Debug_services
debug:
msg: = {{ result }}
- name: Send e-mail
run_once: true
mail:
host: test.example.com
port: 1111
to: test@example.com
subject: Check_Service_State
subtype: html
body: "
{% for item in play_hosts %}
{{ item }}: {{ hostvars[item]['result']['stdout_lines'] }}. <br>
{% endfor %}
"
from: test@example.com
ignore_errors: yes
delegate_to: localhost
My output format looks like:
host1: ['', 'DisplayName Status', '----------- ------', 'Hyper-V Host Compute Service Running', 'Hyper-V Guest Service Interface Stopped', 'Hyper-V Heartbeat Service Stopped', 'Hyper-V Data Exchange Service Stopped', 'Hyper-V Guest Shutdown Service Stopped', 'Hyper-V Time Synchronization Service Stopped', 'Hyper-V PowerShell Direct Service Stopped', 'Hyper-V Volume Shadow Copy Requestor Stopped', 'Hyper-V Virtual Machine Management Running', '', ''].
host2: ['', 'DisplayName Status', '----------- ------', 'Hyper-V Host Compute Service Running', 'Hyper-V Guest Service Interface Stopped', 'Hyper-V Heartbeat Service Stopped', 'Hyper-V Data Exchange Service Stopped', 'Hyper-V Guest Shutdown Service Stopped', 'Hyper-V Time Synchronization Service Stopped', 'Hyper-V PowerShell Direct Service Stopped', 'Hyper-V Volume Shadow Copy Requestor Stopped', 'Hyper-V Virtual Machine Management Running', '', ''].
How can I filter this list correctly or how elegantly can it be done with another script construction?