Ansible - 尝试停止其中包含“$”的 Windows 服务时出错

问题描述 投票:0回答:1

如何使用 Ansible

win_service
模块停止带有特殊字符的 Windows 服务?

当我尝试使用

win_service
模块停止 SQL Server 服务时,当服务中有
$
时,我遇到错误,例如:

SQLAgent$SQL01
SQLAgent$SQL02
MSSQL$SSQLS01
MSSQL$SSQLS02

这是我的剧本:

- name: Get MSSSQL Services
  ansible.windows.win_service_info:
    name: "MSSQL*"
  register: mssql_info

- name: Set MSSQL services
  set_fact:
    mssql_services: "{{ mssql_info.services | selectattr('state', '==', 'started') | map(attribute='name') | list | replace('$', '%24') }}"

- name: Get SQL Agent Services
  ansible.windows.win_service_info:
    name: "SQLA*"
  register: sqla_info

- name: Set Agent services
  set_fact:
    sqla_services: "{{ sqla_info.services | selectattr('state', '==', 'started') | map(attribute='name') | list | replace('$', '%24') }}"

- name: Stop SQL services
  win_service: 
    name: "{{ item }}"
    state: "stopped"
  loop:
    - "{{ sqla_services }}"
    - "{{ mssql_services }}"
  register: stop_output
  ignore_errors: yes

- debug:
    msg: "{{ stop_output }}" 

它给我的错误是:

{
  "exception": "The specified wildcard character pattern is not valid: System.Object[]\r\nAt line:282 char:5\r\n+     Get-Service -Name $Name -ErrorAction SilentlyContinue\r\n+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : NotSpecified: (:) [Get-Service], WildcardPatternException\r\n    + FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.GetServiceCommand\r\n\r\nScriptStackTrace:\r\nat Get-ServiceFromName, <No file>: line 282\r\nat <ScriptBlock>, <No file>: line 952\r\n\r\nSystem.Management.Automation.WildcardPatternException: The specified wildcard character pattern is not valid: System.Object[]\r\n   at System.Management.Automation.WildcardPatternParser.Parse(WildcardPattern pattern, WildcardPatternParser parser)\r\n   at System.Management.Automation.WildcardPatternMatcher.MyWildcardPatternParser.Parse(WildcardPattern pattern, CharacterNormalizer characterNormalizer)\r\n   at System.Management.Automation.WildcardPatternMatcher..ctor(WildcardPattern wildcardPattern)\r\n   at System.Management.Automation.WildcardPattern.Init()\r\n   at System.Management.Automation.WildcardPattern.IsMatch(String input)\r\n   at Microsoft.PowerShell.Commands.MultipleServiceCommandBase.MatchingServicesByServiceName()\r\n   at Microsoft.PowerShell.Commands.MultipleServiceCommandBase.MatchingServices()\r\n   at Microsoft.PowerShell.Commands.GetServiceCommand.ProcessRecord()\r\n   at System.Management.Automation.CommandProcessor.ProcessRecord()",
  "msg": "Unhandled exception while executing module: The specified wildcard character pattern is not valid: System.Object[]",
  "_ansible_no_log": null,
  "changed": false,
  "item": [],
  "ansible_loop_var": "item",
  "_ansible_item_label": []
}

有没有办法使用 Ansible 停止此类服务?

提前致谢。

error-handling ansible windows-services special-characters ansible-module
1个回答
0
投票

我也尝试过类似的事情,最终得到了同样的错误。下面解决了这个问题。也许会有帮助。

vars:
    windows_services:
      - SQLAgent$SQL01
      - SQLAgent$SQL02
...
- name: Get SQL Agent Services
  ansible.windows.win_service_info:
    name: "{{ item }}"
  loop: "{{ windows_services }}"
  register: services_info

- name: Stop SQL services
  ansible.windows.win_service:
    name: "{{ service.name }}"
    state: stopped
  loop: "{{ services_info.results | selectattr('services', 'defined') | map(attribute='services') | list | flatten }}"
  when: service.name in {{ windows_services }}
  register: stop_successful
  vars:
    service: "{{ item }}"

- name: Display the current status of the service
  debug:
    msg: "Status of {{ service.name }} is {{ service.state }}"
  loop: "{{ services_info.results | selectattr('services', 'defined') | map(attribute='services') | list | flatten }}"
  when: service.name in windows_services
  vars:
    service: "{{ item }}"
© www.soinside.com 2019 - 2024. All rights reserved.