3

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.

5
  • At least in the past there was an Ansible Issue #37621 - "win_service: does not work with special chars" ... Commented Jan 26, 2024 at 8:09
  • Have you tried escaping it? Here would be some escapes flavours I would try 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. Commented Jan 26, 2024 at 11:58
  • Oh and probably also SQLAgent`$SQL01 based on stackoverflow.com/questions/17452401/… Commented Jan 26, 2024 at 12:01
  • How can I add the "`" before "$"? I get the name of the SQL service interactively by running win_service_info. The first thing I tried is to change the character "$" to its ASCI form but also is not working. Commented Jan 26, 2024 at 16:43
  • I still have the problem, any udea how to solve it? Commented Jan 31, 2024 at 22:17

1 Answer 1

0

I also was trying something similar and ended up getting the same error. Below fixed the issue. Perhaps it would be helpful.

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 }}"
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.