Reboot - Fix command not found, add Apline support, fix Solaris command (#49272)

* Fix various bugs related in reboot

- Use format strings for consistency and improve debug log messages
- Use local variables instead of class attributes in order to be thread safe
- Run setup module to get distribution and version
- Run find module to get full path of shutdown command
- Use ansible_os_family and ansible_distribution to find commands and args
- Use same command for all Solaris/SunOS distributions
- Move delay calculations to properties
- Reliably check for module run failure
- Fix bug in run_test_command() that accidentally made the method work properly
- Use better exceptions rather than Exception
- Use dict literals rather than constructors
- Correct _check_delay() so it always returns a value, not None
- Don't store and return result in run_test_command() because it's not used anywhere
- add test for post reboot command that fails
- test negative values for delay parameters
This commit is contained in:
Sam Doran
2018-12-11 11:05:10 -05:00
committed by GitHub
parent 2a469fd959
commit c1589c33c4
8 changed files with 291 additions and 156 deletions

View File

@@ -0,0 +1,10 @@
- name: Get current boot time
command: "{{ boot_time_command[ansible_facts['distribution'] | lower] | default('cat /proc/sys/kernel/random/boot_id') }}"
register: after_boot_time
- name: Ensure system was actually rebooted
assert:
that:
- reboot_result is changed
- reboot_result.elapsed > 10
- before_boot_time.stdout != after_boot_time.stdout

View File

@@ -0,0 +1,3 @@
- name: Get current boot time
command: "{{ boot_time_command[ansible_facts['distribution'] | lower] | default('cat /proc/sys/kernel/random/boot_id') }}"
register: before_boot_time

View File

@@ -7,6 +7,8 @@
content: 'I am the control node'
dest: /tmp/Anything-Nutlike-Nuzzle-Plow-Overdue
delegate_to: localhost
connection: local
when: inventory_hostname == ansible_play_hosts[0]
- name: See if the temp file exists on the managed node
stat:
@@ -22,29 +24,40 @@
that:
- not controller_temp_file.stat.exists
- name: Get current boot time
command: who -b
register: before_boot_time
- import_tasks: get_boot_time.yml
- name: Reboot with default settings
reboot:
register: reboot_result
- name: Get current boot time
command: who -b
register: after_boot_time
- import_tasks: check_reboot.yml
- name: Ensure system was actually rebooted
assert:
that:
- reboot_result is changed
- reboot_result.elapsed > 10
- before_boot_time.stdout != after_boot_time.stdout
- import_tasks: get_boot_time.yml
- name: Reboot with all options
reboot:
connect_timeout: 30
msg: Rebooting
post_reboot_delay: 1
pre_reboot_delay: 61
test_command: uptime
reboot_timeout: 500
- import_tasks: check_reboot.yml
- import_tasks: get_boot_time.yml
- name: Test with negative values for delays
reboot:
post_reboot_delay: -0.5
pre_reboot_delay: -61
- import_tasks: check_reboot.yml
- name: Use invalid parameter
reboot:
foo: bar
ignore_errors: yes
ignore_errors: true
register: invalid_parameter
- name: Ensure task fails with error
@@ -53,10 +66,22 @@
- invalid_parameter is failed
- "invalid_parameter.msg == 'Invalid options for reboot: foo'"
- name: Reboot with test command that fails
reboot:
test_command: 'FAIL'
reboot_timeout: "{{ timeout }}"
register: reboot_fail_test
failed_when: "reboot_fail_test.msg != 'Timed out waiting for post-reboot test command (timeout=' ~ timeout ~ ')'"
vars:
timeout: "{{ timeout_value[ansible_facts['distribution'] | lower] | default(60) }}"
always:
- name: Cleanup temp file
file:
path: /tmp/Anything-Nutlike-Nuzzle-Plow-Overdue
state: absent
delegate_to: localhost
connection: local
when: inventory_hostname == ansible_play_hosts[0]
when: ansible_virtualization_type | default('') != 'docker'

View File

@@ -0,0 +1,9 @@
boot_time_command:
freebsd: '/sbin/sysctl kern.boottime'
openbsd: '/sbin/sysctl kern.boottime'
macosx: 'who -b'
solaris: 'who -b'
sunos: 'who -b'
timeout_value:
solaris: 120

View File

@@ -7,6 +7,11 @@
- name: reboot with defaults
win_reboot:
- name: test with negative values for delays
win_reboot:
post_reboot_delay: -0.5
pre_reboot_delay: -61
- name: schedule a reboot for sometime in the future
win_command: shutdown.exe /r /t 599
@@ -23,6 +28,13 @@
win_reboot:
test_command: powershell.exe -NoProfile -EncodedCommand {{lookup('template', 'post_reboot.ps1')|b64encode(encoding='utf-16-le')}}
- name: reboot with test command that fails
win_reboot:
test_command: 'FAIL'
reboot_timeout: 120
register: reboot_fail_test
failed_when: "reboot_fail_test.msg != 'Timed out waiting for post-reboot test command (timeout=120)'"
# try and reboot the host with a non admin user, we expect an error here
# this requires a bit of setup to create the user and allow it to connect
# over WinRM
@@ -59,28 +71,28 @@
Set-Item -Path WSMan:\localhost\Service\RootSDDL -Value $new_sddl -Force
- block:
- name: fail to reboot with non admin user
win_reboot:
vars:
ansible_user: '{{standard_user}}'
ansible_password: '{{standard_pass}}'
ansible_winrm_transport: ntlm
register: fail_shutdown
failed_when: "fail_shutdown.msg != 'Shutdown command failed, error was: Access is denied.(5)'"
- name: fail to reboot with non admin user
win_reboot:
vars:
ansible_user: '{{standard_user}}'
ansible_password: '{{standard_pass}}'
ansible_winrm_transport: ntlm
register: fail_shutdown
failed_when: "fail_shutdown.msg != 'Reboot command failed, error was: Access is denied.(5)'"
always:
- name: set the original SDDL to the WinRM listener
win_shell: Set-Item -Path WSMan:\localhost\Service\RootSDDL -Value "{{original_sddl.stdout_lines[0]}}" -Force
- name: set the original SDDL to the WinRM listener
win_shell: 'Set-Item -Path WSMan:\localhost\Service\RootSDDL -Value "{{original_sddl.stdout_lines[0]}}" -Force'
- name: remove standard user
win_user:
name: '{{standard_user}}'
state: absent
- name: remove standard user
win_user:
name: '{{standard_user}}'
state: absent
- name: Use invalid parameter
reboot:
foo: bar
ignore_errors: yes
ignore_errors: true
register: invalid_parameter
- name: Ensure task fails with error