280 | 281 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,80 @@ |
0 |
+#!powershell |
|
1 |
+# This file is part of Ansible |
|
2 |
+# |
|
3 |
+# Ansible is free software: you can redistribute it and/or modify |
|
4 |
+# it under the terms of the GNU General Public License as published by |
|
5 |
+# the Free Software Foundation, either version 3 of the License, or |
|
6 |
+# (at your option) any later version. |
|
7 |
+# |
|
8 |
+# Ansible is distributed in the hope that it will be useful, |
|
9 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 |
+# GNU General Public License for more details. |
|
12 |
+# |
|
13 |
+# You should have received a copy of the GNU General Public License |
|
14 |
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
|
15 |
+ |
|
16 |
+# WANT_JSON |
|
17 |
+# POWERSHELL_COMMON |
|
18 |
+ |
|
19 |
+$ErrorActionPreference = "Stop" |
|
20 |
+ |
|
21 |
+$params = Parse-Args $args -supports_check_mode $true |
|
22 |
+$check_mode = Get-AnsibleParam -obj $params "_ansible_check_mode" -type "bool" -default $false |
|
23 |
+ |
|
24 |
+$name = Get-AnsibleParam -obj $params -name 'name' -type 'str' -failifempty $true |
|
25 |
+ |
|
26 |
+$result = @{ |
|
27 |
+ changed = $false |
|
28 |
+} |
|
29 |
+ |
|
30 |
+$svc = Get-Service -Name $name -ErrorAction SilentlyContinue |
|
31 |
+if ($svc) { |
|
32 |
+ $wmi_svc = Get-WmiObject Win32_Service | Where-Object { $_.Name -eq $svc.Name } |
|
33 |
+ |
|
34 |
+ # Delayed start_mode is in reality Automatic (Delayed), need to check reg key for type |
|
35 |
+ $delayed_key = "HKLM:\System\CurrentControlSet\Services\$name" |
|
36 |
+ try { |
|
37 |
+ $delayed = ConvertTo-Bool ((Get-ItemProperty -Path $delayed_key).DelayedAutostart) |
|
38 |
+ } catch { |
|
39 |
+ $delayed = $false |
|
40 |
+ } |
|
41 |
+ $actual_start_mode = $wmi_svc.StartMode.ToString().ToLower() |
|
42 |
+ if ($delayed -and $actual_start_mode -eq 'auto') { |
|
43 |
+ $actual_start_mode = 'delayed' |
|
44 |
+ } |
|
45 |
+ |
|
46 |
+ $existing_depenencies = @() |
|
47 |
+ $existing_depended_by = @() |
|
48 |
+ if ($svc.ServicesDependedOn.Count -gt 0) { |
|
49 |
+ foreach ($dependency in $svc.ServicesDependedOn.Name) { |
|
50 |
+ $existing_depenencies += $dependency |
|
51 |
+ } |
|
52 |
+ } |
|
53 |
+ if ($svc.DependentServices.Count -gt 0) { |
|
54 |
+ foreach ($dependency in $svc.DependentServices.Name) { |
|
55 |
+ $existing_depended_by += $dependency |
|
56 |
+ } |
|
57 |
+ } |
|
58 |
+ |
|
59 |
+ $description = $wmi_svc.Description |
|
60 |
+ if ($description -eq $null) { |
|
61 |
+ $description = "" |
|
62 |
+ } |
|
63 |
+ |
|
64 |
+ $result.exists = $true |
|
65 |
+ $result.name = $svc.Name |
|
66 |
+ $result.display_name = $svc.DisplayName |
|
67 |
+ $result.state = $svc.Status.ToString().ToLower() |
|
68 |
+ $result.start_mode = $actual_start_mode |
|
69 |
+ $result.path = $wmi_svc.PathName |
|
70 |
+ $result.description = $description |
|
71 |
+ $result.username = $wmi_svc.startname |
|
72 |
+ $result.desktop_interact = (ConvertTo-Bool $wmi_svc.DesktopInteract) |
|
73 |
+ $result.dependencies = $existing_depenencies |
|
74 |
+ $result.depended_by = $existing_depended_by |
|
75 |
+} else { |
|
76 |
+ $result.exists = $false |
|
77 |
+} |
|
78 |
+ |
|
79 |
+Exit-Json $result |
0 | 80 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,104 @@ |
0 |
+#!/usr/bin/python |
|
1 |
+# -*- coding: utf-8 -*- |
|
2 |
+# |
|
3 |
+# This file is part of Ansible |
|
4 |
+# |
|
5 |
+# Ansible is free software: you can redistribute it and/or modify |
|
6 |
+# it under the terms of the GNU General Public License as published by |
|
7 |
+# the Free Software Foundation, either version 3 of the License, or |
|
8 |
+# (at your option) any later version. |
|
9 |
+# |
|
10 |
+# Ansible is distributed in the hope that it will be useful, |
|
11 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
+# GNU General Public License for more details. |
|
14 |
+# |
|
15 |
+# You should have received a copy of the GNU General Public License |
|
16 |
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
+ |
|
18 |
+# this is a windows documentation stub. actual code lives in the .ps1 |
|
19 |
+# file of the same name |
|
20 |
+ |
|
21 |
+ANSIBLE_METADATA = {'status': ['preview'], |
|
22 |
+ 'supported_by': 'community', |
|
23 |
+ 'version': '1.0'} |
|
24 |
+ |
|
25 |
+DOCUMENTATION = r''' |
|
26 |
+--- |
|
27 |
+module: win_service_stat |
|
28 |
+version_added: "2.3" |
|
29 |
+short_description: returns information about a Windows service |
|
30 |
+description: |
|
31 |
+- This module will return information about a service such as whether it |
|
32 |
+ exist or not as well as things like the state, startup type and more. |
|
33 |
+options: |
|
34 |
+ name: |
|
35 |
+ description: The name of the Windows service to get info for. |
|
36 |
+ required: True |
|
37 |
+author: Jordan Borean (@jborean93) |
|
38 |
+''' |
|
39 |
+ |
|
40 |
+EXAMPLES = r''' |
|
41 |
+- name: get details on a service |
|
42 |
+ win_service_stat: |
|
43 |
+ name: spooler |
|
44 |
+ register: spooler_info |
|
45 |
+''' |
|
46 |
+ |
|
47 |
+RETURN = r''' |
|
48 |
+exists: |
|
49 |
+ description: whether the service exists or not |
|
50 |
+ returned: success |
|
51 |
+ type: boolean |
|
52 |
+ sample: true |
|
53 |
+name: |
|
54 |
+ description: the service name or id of the service |
|
55 |
+ returned: success and service exists |
|
56 |
+ type: string |
|
57 |
+ sample: CoreMessagingRegistrar |
|
58 |
+display_name: |
|
59 |
+ description: the display name of the installed service |
|
60 |
+ returned: success and service exists |
|
61 |
+ type: string |
|
62 |
+ sample: CoreMessaging |
|
63 |
+status: |
|
64 |
+ description: the current running status of the service |
|
65 |
+ returned: success and service exists |
|
66 |
+ type: string |
|
67 |
+ sample: stopped |
|
68 |
+start_mode: |
|
69 |
+ description: the startup type of the service |
|
70 |
+ returned: success and service exists |
|
71 |
+ type: string |
|
72 |
+ sample: manual |
|
73 |
+path: |
|
74 |
+ description: |
|
75 |
+ returned: success and service exists |
|
76 |
+ type: string |
|
77 |
+ sample: C:\Windows\system32\svchost.exe -k LocalServiceNoNetwork |
|
78 |
+description: |
|
79 |
+ description: the path to the executable of the service |
|
80 |
+ returned: success and service exists |
|
81 |
+ type: string |
|
82 |
+ sample: Manages communication between system components. |
|
83 |
+username: |
|
84 |
+ description: the username that runs the service |
|
85 |
+ returned: success and service exists |
|
86 |
+ type: string |
|
87 |
+ sample: LocalSystem |
|
88 |
+desktop_interact: |
|
89 |
+ description: Whether the current user is allowed to interact with the desktop |
|
90 |
+ returned: success and service exists |
|
91 |
+ type: boolean |
|
92 |
+ sample: False |
|
93 |
+dependencies: |
|
94 |
+ description: A list of dependencies the service relies on |
|
95 |
+ returned: success and service exists |
|
96 |
+ type: List |
|
97 |
+ sample: False |
|
98 |
+depended_by: |
|
99 |
+ description: A list of dependencies this service relies on |
|
100 |
+ returned: success and service exists |
|
101 |
+ type: List |
|
102 |
+ sample: False |
|
103 |
+''' |
0 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,86 @@ |
0 |
+--- |
|
1 |
+# Until changes are merged into win_service to create a new module we will need |
|
2 |
+# to do it ourselves |
|
3 |
+- name: stop test services before test |
|
4 |
+ win_command: sc stop TestService |
|
5 |
+ ignore_errors: True |
|
6 |
+ |
|
7 |
+- name: make sure we clean up any test services |
|
8 |
+ win_command: sc delete TestService |
|
9 |
+ ignore_errors: True |
|
10 |
+ |
|
11 |
+- name: get stats on service that doesn't exist |
|
12 |
+ win_service_stat: |
|
13 |
+ name: TestService |
|
14 |
+ register: missing_service |
|
15 |
+ |
|
16 |
+- name: assert missing service doesn't exist |
|
17 |
+ assert: |
|
18 |
+ that: |
|
19 |
+ - not missing_service|changed |
|
20 |
+ - missing_service.exists == False |
|
21 |
+ |
|
22 |
+- name: create a new test service |
|
23 |
+ win_command: powershell.exe "New-Service -Name TestService -BinaryPathname C:\Windows\System32\snmptrap.exe" |
|
24 |
+ |
|
25 |
+- name: get stats on newly created service |
|
26 |
+ win_service_stat: |
|
27 |
+ name: TestService |
|
28 |
+ register: new_service |
|
29 |
+ |
|
30 |
+- name: assert new service results are what we expect |
|
31 |
+ assert: |
|
32 |
+ that: |
|
33 |
+ - not new_service|changed |
|
34 |
+ - new_service.exists == True |
|
35 |
+ - new_service.depended_by == [] |
|
36 |
+ - new_service.dependencies == [] |
|
37 |
+ - new_service.description == "" |
|
38 |
+ - new_service.desktop_interact == False |
|
39 |
+ - new_service.display_name == 'TestService' |
|
40 |
+ - new_service.name == 'TestService' |
|
41 |
+ - new_service.path == 'C:\Windows\System32\snmptrap.exe' |
|
42 |
+ - new_service.start_mode == 'auto' |
|
43 |
+ - new_service.state == 'stopped' |
|
44 |
+ - new_service.username == 'LocalSystem' |
|
45 |
+ |
|
46 |
+- name: change details about the service |
|
47 |
+ win_command: powershell.exe "Set-Service -Name TestService -DisplayName NewDisplayName -Description description -StartupType Manual" |
|
48 |
+ |
|
49 |
+- name: get stats on changed service |
|
50 |
+ win_service_stat: |
|
51 |
+ name: TestService |
|
52 |
+ register: changed_service |
|
53 |
+ |
|
54 |
+- name: assert changed service details have changed |
|
55 |
+ assert: |
|
56 |
+ that: |
|
57 |
+ - not changed_service|changed |
|
58 |
+ - changed_service.exists == True |
|
59 |
+ - changed_service.description == 'description' |
|
60 |
+ - changed_service.display_name == 'NewDisplayName' |
|
61 |
+ - changed_service.start_mode == 'manual' |
|
62 |
+ |
|
63 |
+- name: start the service |
|
64 |
+ win_service: |
|
65 |
+ name: TestService |
|
66 |
+ state: started |
|
67 |
+ register: started_service |
|
68 |
+ |
|
69 |
+- name: get stats on started service |
|
70 |
+ win_service_stat: |
|
71 |
+ name: TestService |
|
72 |
+ register: started_service |
|
73 |
+ |
|
74 |
+- name: assert service stats is started |
|
75 |
+ assert: |
|
76 |
+ that: |
|
77 |
+ - started_service.state == 'running' |
|
78 |
+ |
|
79 |
+# TODO: Change other service info through win_service once we can do it with a module |
|
80 |
+ |
|
81 |
+- name: stop test services before deleting |
|
82 |
+ win_command: sc stop TestService |
|
83 |
+ |
|
84 |
+- name: clean up test service at the end |
|
85 |
+ win_command: sc delete TestService |