Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update win_reboot.py #707

Closed
wants to merge 1 commit into from
Closed

Update win_reboot.py #707

wants to merge 1 commit into from

Conversation

p3ck
Copy link

@p3ck p3ck commented Dec 4, 2024

SUMMARY

The included example for testing if the rebooted system is ready does not work. Removing the exit from the command fixes this.

ISSUE TYPE
  • Docs Pull Request
COMPONENT NAME

win_reboot.py

ADDITIONAL INFORMATION

If you use the included example you will retry until the default 600 second timeout.

The included example for testing if the rebooted system is ready does not work.  Removing the exit from the command fixes this.
@@ -102,7 +102,7 @@
# Or you can make win_reboot validate exactly what you need to work before running the next task
- name: Validate that the netlogon service has started, before running the next task
ansible.windows.win_reboot:
test_command: 'exit (Get-Service -Name Netlogon).Status -ne "Running"'
test_command: '(Get-Service -Name Netlogon).Status -ne "Running"'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not having exit here means the command runs and will always return 0 as the exit code and be seen as successful even if the service isn't running. Are you sure that Netlogon was actually running in your example?

Here is an example when running this example with -vvvvv

ansible.windows.win_reboot running post reboot test command
ansible.windows.win_reboot: attempting post-reboot test command
ansible.windows.win_reboot: running command: exit (Get-Service -Name Netlogon).Status -ne "Running"
<SERVER2025.domain.test> PSRP: EXEC Set-StrictMode -Version Latest
exit (Get-Service -Name Netlogon).Status -ne "Running"
If (-not $?) { If (Get-Variable LASTEXITCODE -ErrorAction SilentlyContinue) { exit $LASTEXITCODE } Else { exit 1 } }
<SERVER2025.domain.test> PSRP RC: 0
<SERVER2025.domain.test> PSRP STDOUT: 
<SERVER2025.domain.test> PSRP STDERR: 
ansible.windows.win_reboot: command result - rc: 0, stdout: , stderr: 
ansible.windows.win_reboot: post-reboot test command success

We can see the command returned an rc of 0 as the service was running. Now if we were to reverse the logic and do -ne "Stopped" we can see that it did timeout because the rc was always 1 and thus the test command was seen to have failed.

ansible.windows.win_reboot: attempting post-reboot test command
ansible.windows.win_reboot: running command: exit (Get-Service -Name Netlogon).Status -ne "Stopped"
<SERVER2025.domain.test> PSRP: EXEC Set-StrictMode -Version Latest
exit (Get-Service -Name Netlogon).Status -ne "Stopped"
If (-not $?) { If (Get-Variable LASTEXITCODE -ErrorAction SilentlyContinue) { exit $LASTEXITCODE } Else { exit 1 } }
<SERVER2025.domain.test> PSRP RC: 1
<SERVER2025.domain.test> PSRP STDOUT: 
<SERVER2025.domain.test> PSRP STDERR: 
ansible.windows.win_reboot: command result - rc: 1, stdout: , stderr: 
ansible.windows.win_reboot: post-reboot test command fail _TestCommandFailure 'ansible.windows.win_reboot: Test command failed - rc: 1, stdout: , stderr: ', retrying in 12.58 seconds...
Traceback (most recent call last):
  File "/home/jborean/dev/ansible_collections/ansible/windows/plugins/plugin_utils/_reboot.py", line 392, in _do_until_success_or_condition
    res = func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jborean/dev/ansible_collections/ansible/windows/plugins/plugin_utils/_reboot.py", line 594, in _run_test_command
    raise _TestCommandFailure(msg)
ansible_collections.ansible.windows.plugins.plugin_utils._reboot._TestCommandFailure: ansible.windows.win_reboot: Test command failed - rc: 1, stdout: , stderr: 
ansible.windows.win_reboot: attempting post-reboot test command
ansible.windows.win_reboot: running command: exit (Get-Service -Name Netlogon).Status -ne "Stopped"
<SERVER2025.domain.test> PSRP: EXEC Set-StrictMode -Version Latest
exit (Get-Service -Name Netlogon).Status -ne "Stopped"
If (-not $?) { If (Get-Variable LASTEXITCODE -ErrorAction SilentlyContinue) { exit $LASTEXITCODE } Else { exit 1 } }
<SERVER2025.domain.test> PSRP RC: 1
<SERVER2025.domain.test> PSRP STDOUT: 
<SERVER2025.domain.test> PSRP STDERR: 
ansible.windows.win_reboot: command result - rc: 1, stdout: , stderr: 
ansible.windows.win_reboot: post-reboot test command fail _TestCommandFailure 'ansible.windows.win_reboot: Test command failed - rc: 1, stdout: , stderr: ', retrying in 12.31 seconds...
...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still having trouble getting this to work.

I ran the following:

ansible -m ansible.windows.win_reboot -i win-inventory -a 'test_command="exit (Get-Service -Name Netlogon).status -ne \"Running\""' windows -vvvvv

ansible.windows.win_reboot: attempting post-reboot test command
ansible.windows.win_reboot: running command: exit (Get-Service -Name Netlogon).status -ne "Running"
EXEC (via pipeline wrapper)
<192.168.121.93> WINRM EXEC 'PowerShell' ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted', '-EncodedCommand', <192.168.121.93> WINRM RESULT <Response code 1, out '', err '#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>'>
ansible.windows.win_reboot: command result - rc: 1, stdout: , stderr: 
ansible.windows.win_reboot: post-reboot test command fail _TestCommandFailure 'ansible.windows.win_reboot: Test command failed - rc: 1, stdout: , stderr: ', retrying in 12.42 seconds...

But it keeps looping with a return code of 1 even though if I run the following powershell in another window which shows it is doing the comparison correctly..

(venv-ansible) [admin@okd-master-1 ~]$ ansible -m ansible.windows.win_powershell -i win-inventory -a 'script="(Get-Service -Name Netlogon).status -ne \"Running\""' windows
win2022 | CHANGED => {
    "changed": true,
    "debug": [],
    "error": [],
    "host_err": "",
    "host_out": "",
    "information": [],
    "output": [
        true
    ],
    "result": {},
    "verbose": [],
    "warning": []
}
(venv-ansible) [admin@okd-master-1 ~]$ ansible -m ansible.windows.win_powershell -i win-inventory -a 'script="(Get-Service -Name Netlogon).status -eq \"Running\""' windows
win2022 | CHANGED => {
    "changed": true,
    "debug": [],
    "error": [],
    "host_err": "",
    "host_out": "",
    "information": [],
    "output": [
        false
    ],
    "result": {},
    "verbose": [],
    "warning": []
}

@p3ck
Copy link
Author

p3ck commented Dec 10, 2024

I understand my mistake now, Netlogon is not always setup by default. Sorry for the noise.

@p3ck p3ck closed this Dec 10, 2024
@jborean93
Copy link
Collaborator

That would do it, glad you were able to sort it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants