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.
win_service: does not work with special chars" ...SQLAgent\$SQL01;SQLAgent\\$SQL01;SQLAgent\\\$SQL01;SQLAgent$$SQL01. The repetition of backslashes up to three are because\do have a certain meaning in Python and YAML, so you could want to escape the special meaning of that character so it reach the actual command Ansible will run on the Windows node.SQLAgent`$SQL01based on stackoverflow.com/questions/17452401/…