Ansible - Error when trying to stop a windows service with "$" in it

52 Views Asked by At

How to stop Windows services with special characters using Ansible win_service module?

I am facing an error when I try to stop SQL Server services using win_service module when the service has $ in it like:

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

This is my playbook:

- 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 }}" 

The error it gives me is:

{
  "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": []
}

Is there a way to stop this kind of services using Ansible?

Thanks in advance.

0

There are 0 best solutions below