我正尝试使用ansible重启远程主机。目前它可以正常工作,但是远程主机在同一时间重新启动。我想用睡眠时间一一重启。
我试图在下面的代码中放置wait_for,但是它不起作用。我遇到了与shell冲突的错误。
Playbook file
- name: Rebooting ...
wait_for:
time_out: 60
shell: sleep 2 && /sbin/shutdown -r now "Reboot required"
async: 1
poll: 0
ignore_errors: true
register: rebooting
Error message:
The error appears to have been in '/home/ansible/reboot-hosts.yml': line 20, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Rebooting ...
^ here
exception type: <class 'ansible.errors.AnsibleParserError'>
exception: conflicting action statements: shell, wait_for
The error appears to have been in '/home/ansible/reboot-hosts.yml': line 20, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
这是预期过程:
答案 0 :(得分:1)
如@Peschke所说,请尝试reboot模块。但是一次做一次,您需要在剧本中设置response.uri
:
serial: 1
答案 1 :(得分:0)
问题在于您在任务中有两个操作:wait_for
和shell
。除非您使用block,否则wait_for
模块必须位于其自己的任务中。
尝试这样的事情:
- name: Rebooting ...
shell: sleep 2 && /sbin/shutdown -r now "Reboot required"
async: 1
poll: 0
ignore_errors: true
register: rebooting
- name: wait for reboot
wait_for:
timeout: 60
delegate_to: localhost
另一种选择是使用reboot
模块。该模块将等待系统关闭并重新启动后再继续。默认情况下,它需要等待600秒才能恢复系统。
如果您只想等待60秒,则可以执行以下操作:
- name: Rebooting ...
reboot:
reboot_timeout: 60