英文:
How can I filter 'stdout_lines'?
问题
我正在尝试从Ansible的stdout_lines
结果中筛选出一行。然后将输出脚本通过电子邮件发送。无法获得所需的显示格式。所需的输出格式如下:
host1:
Service1: Status
Service2: Status
host2:
Service1: Status
Service2: Status
我的Playbook如下:
- hosts: [MyServers]
tasks:
- name: 运行脚本
win_shell: Get-Service -DisplayName Hyper-V* | Select-Object DisplayName, Status
register: result
- name: Debug_services
debug:
msg: = {{ result }}
- name: 发送电子邮件
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
我的输出格式如下:
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', '', ''].
如何正确筛选此列表或如何使用另一个脚本结构更优雅地完成这项任务?
英文:
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?
答案1
得分: 1
根据您的初始描述,
win_shell: Get-Service -DisplayName Hyper-V* | Select-Object DisplayName, Status
我理解您希望获取有关Windows远程节点的信息,特别是某些服务的状态。
如何优雅地完成这个任务?
您可以使用 win_service_info
模块 - 收集关于Windows服务的信息,它将提供"基于条件找到的服务列表"。它将返回一个带有元素=字典
的列表
,因此无需过滤stdout_lines
、解析输出、编写脚本等操作。
类似的问答
- Ansible:如何获取Windows服务的列表以及它们的当前状态?
- Ansible - 如何检查目标Windows主机上是否运行了指定的服务?
- 如何在Ansible playbook中筛选
win_service_info
的结果?
如果有另一个 cmdlet,输出格式和任务相同:
win_shell: Get-VirtualDisk | Select-Object FriendlyName, HealthStatus
对于大多数任务和资源,都有专用模块可用,例如 Get-VirtualDisk
这样的额外信息,您可以查看 win_disk_facts
模块 - 显示目标主机的附加磁盘和磁盘信息。
英文:
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
- Ansible: How to obtain a list of Windows services along with their current status?
- Ansible - How to check given services are running in Target Windows Host?
- How to filter
win_service_info
result in Ansible playbook?
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论