Debian Codegraph @

ansible_collections/community/windows/tests/integration/targets/win_power_plan/tasks/main.yml

raw
1 # I dislike this but 2008 doesn't support the Win32_PowerPlan WMI provider
2 - name: set fact for getting plan info
3 ansible.builtin.set_fact:
4 plan_info_script: |
5 $infoPattern = 'Power Scheme GUID: (?<guid>[a-fA-F0-9]{8}-(?>[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12})\s+\((?<name>.*)\)(?<active>\s+\*)?'
6
7 powercfg.exe /list | ForEach-Object {
8 if ($_ -match $infoPattern) {
9 [PSCustomObject]@{
10 name = $Matches['name']
11 guid = $Matches['guid']
12 active = (-not [string]::IsNullOrEmpty($Matches['active']))
13 }
14 }
15 } | Sort-Object -Property active -Descending
16
17 - name: get current plan details
18 ansible.windows.win_powershell:
19 script: '{{ plan_info_script }}'
20 changed_when: False
21 register: plan_info
22
23 - set_fact:
24 original_plan: '{{ plan_info.output[0] }}'
25 new_plan: '{{ plan_info.output[1] }}'
26
27 - block:
28 #Test that plan detects change is needed, but doesn't actually apply change
29 - name: set power plan (check mode)
30 win_power_plan:
31 name: "{{ new_plan.name }}"
32 register: set_plan_check
33 check_mode: yes
34
35 - name: get result of set power plan (check mode)
36 ansible.windows.win_powershell:
37 script: '{{ plan_info_script }}'
38 register: set_plan_check_result
39 changed_when: False
40
41 # verify that the powershell check is showing the plan as still inactive on the system
42 - name: assert setting plan (check mode)
43 assert:
44 that:
45 - set_plan_check is changed
46 - set_plan_check_result.output[0].guid == original_plan.guid
47
48 #Test that setting plan and that change is applied
49 - name: set power plan
50 win_power_plan:
51 name: "{{ new_plan.name }}"
52 register: set_plan
53
54 - name: get result of set power plan
55 ansible.windows.win_powershell:
56 script: '{{ plan_info_script }}'
57 register: set_plan_result
58 changed_when: False
59
60 - name: assert setting plan
61 assert:
62 that:
63 - set_plan is changed
64 - set_plan_result.output[0].guid == new_plan.guid
65
66 #Test that plan doesn't apply change if it is already set
67 - name: set power plan (idempotent)
68 win_power_plan:
69 name: "{{ new_plan.name }}"
70 register: set_plan_idempotent
71
72 - name: assert setting plan (idempotent)
73 assert:
74 that:
75 - set_plan_idempotent is not changed
76
77 always:
78 - name: always change back plan to the original when done testing
79 win_power_plan:
80 name: '{{ original_plan.name }}'
81
82 - block:
83 #Test that plan detects change is needed, but doesn't actually apply change
84 - name: set power plan guid (check mode)
85 win_power_plan:
86 guid: "{{ new_plan.guid }}"
87 register: set_plan_check_guid
88 check_mode: yes
89
90 - name: get result of set power plan guid (check mode)
91 ansible.windows.win_powershell:
92 script: '{{ plan_info_script }}'
93 register: set_plan_check_result_guid
94 changed_when: False
95
96 # verify that the powershell check is showing the plan as still inactive on the system
97 - name: assert setting plan guid (check mode)
98 assert:
99 that:
100 - set_plan_check_guid is changed
101 - set_plan_check_result_guid.output[0].guid == original_plan.guid
102
103 #Test that setting plan and that change is applied
104 - name: set power plan guid
105 win_power_plan:
106 guid: "{{ new_plan.guid }}"
107 register: set_plan_guid
108
109 - name: get result of set power plan guid
110 ansible.windows.win_powershell:
111 script: '{{ plan_info_script }}'
112 register: set_plan_result_guid
113 changed_when: False
114
115 - name: assert setting plan guid
116 assert:
117 that:
118 - set_plan_guid is changed
119 - set_plan_result_guid.output[0].guid == new_plan.guid
120
121 #Test that plan doesn't apply change if it is already set
122 - name: set power plan guid (idempotent)
123 win_power_plan:
124 guid: "{{ new_plan.guid }}"
125 register: set_plan_idempotent_guid
126
127 - name: assert setting plan guid (idempotent)
128 assert:
129 that:
130 - set_plan_idempotent_guid is not changed
131
132 always:
133 - name: always change back plan to the original when done testing guid
134 win_power_plan:
135 guid: '{{ original_plan.guid }}'