-2

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?

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Ivan
  • 1
  • 1
  • 2
    You should use the purposed module instead of a `win_shell` to start with: https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_service_info_module.html#ansible-collections-ansible-windows-win-service-info-module – β.εηοιτ.βε Jun 29 '23 at 09:14
  • That's a bad idea, yes, this was what my comment was about. This is also an anti pattern in Ansible, when available, use the purposed module, they are going to give you dictionaries / list that are way more easy to parse / traverse than a random PowerShell command as you did. – β.εηοιτ.βε Jun 29 '23 at 09:29
  • I have a question how to filter the output, the Powershell cmdlet may be different – Ivan Jun 29 '23 at 09:30
  • I guess that using [`win_service_info` module – Gather information about Windows services](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_service_info_module.html) might be the better approach to gather facts about Windows services than re-implementing it. – U880D Jun 29 '23 at 09:31

1 Answers1

1

According your initial description

win_shell: Get-Service -DisplayName Hyper-V* | Select-Object DisplayName, Status

I understand that you like to gather facts about a Windows Remote Node, specifically the status of certain services.

How elegantly can it be done?

By using win_service_info module – Gather information about Windows services Note which will provide "A list of service(s) that were found based on the criteria". There will be a list with elements=dictionary returned, so there is no need for filtering stdout_lines, parsing the output, writing scripts, etc.

Similar Q&A


What if there is another cmdlet, and the output format and task is the same:

win_shell: Get-VirtualDisk | Select-Object FriendlyName, HealthStatus

Since for most tasks and resources dedicates modules are available, for additional facts like Get-VirtualDisk you may have a look into win_disk_facts module – Show the attached disks and disk information of the target host.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • What if there is another cmdlet, example Get-VirtualDisk and the output format is the same? – Ivan Jun 29 '23 at 10:14
  • That's an other kind of fact about the Remote Node which is either already gathered or can be gathered. Ansible uses dedicated modules and tasks for such. If such is not available it can be created. But Ansible is not for generic programming. – U880D Jun 29 '23 at 10:58