简体   繁体   English

如何使用 ansible playbook 重新启动 ubuntu 服务器?

[英]How can I use ansible playbook to reboot a ubuntu server?

I am trying to build an ansible playbook to configure a ubuntu vagrant box.我正在尝试构建一个 ansible playbook 来配置一个 ubuntu vagrant box。 The playbook is pretty much working with the exception of controlling the ubuntu box reboot after upgrading the kernel.除了在升级内核后控制 ubuntu 盒重启之外,该剧本几乎可以正常工作。

I have a host file for ansible as follow :我有一个 ansible 的主机文件,如下所示:

localhost ansible_connection=local
dockerhost ansible_ssh_port=2222 ansible_ssh_host=127.0.0.1

The latest iteration I tried to solve this problem is as follow :我试图解决这个问题的最新迭代如下:

  - name: Restart the server
    shell: sleep 2s && reboot & executable=/bin/bash

  - name: Wait until the virtual machine stop ie: ssh port stop responding
    local_action: wait_for host={{ansible_ssh_host}} port={{ansible_ssh_port}} state=stopped
    sudo: false

  - name: Wait for server to come up
    local_action: wait_for host={{ansible_ssh_host}} port={{ansible_ssh_port}} delay=30
    sudo: false

With this playbook steps the process block waiting for the ssh port to stop responding, until it reach timeout and exit the playbook, I am guessing that if the reboot is particularly fast it might happen in between the polling intervals of the wait_for command and miss the short time when the ssh port is actually down.有了这个剧本,进程块等待 ssh 端口停止响应,直到它达到超时并退出剧本,我猜如果重启特别快,它可能会发生在 wait_for 命令的轮询间隔之间并错过ssh 端口实际关闭的时间很短。 The error returned by ansible is : ansible 返回的错误是:

failed: [dockerhost] => {"elapsed": 300, "failed": true} msg: Timeout when waiting for 127.0.0.1:2222 to stop.失败:[dockerhost] => {"elapsed": 300, "failed": true} msg: 等待 127.0.0.1:2222 停止时超时。

At least once it managed the get to the step where ansible wait for the ssh port to be available again but hung there until timing out.至少有一次它设法到达 ansible 等待 ssh 端口再次可用但挂在那里直到超时的步骤。 I think this solution is too sensitive to difference in reboot speed, which can vary widely in a virtual environment.我认为这个解决方案对重启速度的差异过于敏感,这在虚拟环境中可能会有很大差异。 The ansible version I am using is 1.5.3 Ubuntu is 12.04lte with a Kernel upgrade to 3.8 The complete playbook install docker and all dependencies.我使用的 ansible 版本是 1.5.3 Ubuntu 是 12.04lte,内核升级到 3.8 完整的剧本安装 docker 和所有依赖项。

I tried many variations and ideas found in various web sites but never managed to properly control a reboot and continue my playbook with the next steps.我尝试了在各种网站上找到的许多变体和想法,但从未设法正确控制重启并继续我的剧本以进行下一步。

I am looking for a simple and fool proof way of rebooting the server and continuing with the next steps in a playbook once the machine is back up and running我正在寻找一种简单且万无一失的方法来重新启动服务器并在机器恢复运行后继续执行剧本中的后续步骤

I have not explored the possibility of running a local vagrant reload because I want to use this same playbook in operation where I will not be running vagrant, I only mentioned vagrant in case it creates some complexities that I am not aware of.我还没有探索运行本地 vagrant 重新加载的可能性,因为我想在不运行 vagrant 的操作中使用相同的 playbook,我只提到 vagrant,以防它造成一些我不知道的复杂性。 I also don't want to just pause 5 minutes and hope for the server to be up again, the point of using this kind of tools is to provision servers in a predictable and timely manner and to be portable from environment to environment, a pause just does not seem right.我也不想只是暂停 5 分钟并希望服务器再次启动,使用这种工具的目的是以可预测和及时的方式提供服务器,并且可以在不同环境之间移植,暂停一下只是好像不对。

I have also looked around for an ansible module that would manage this requirement but comments on the ansible web site seem to rule this out.我还四处寻找可以管理此要求的 ansible 模块,但 ansible 网站上的评论似乎排除了这一点。

Thanks谢谢

Might I suggest to use a bridge or private network.我是否建议使用网桥或专用网络。 Using the port forward might be tricky.使用端口转发可能会很棘手。 I used your code with private and bridged network worked perfectly with both.我将您的代码与私有和桥接网络一起使用,两者都完美地工作。

You can use the reboot module您可以使用重启模块

- name: Reboot a slow machine that might have lots of updates to apply
  reboot:
     reboot_timeout: 3600

https://docs.ansible.com/ansible/latest/modules/reboot_module.html https://docs.ansible.com/ansible/latest/modules/reboot_module.html

你需要“-y”来进行 apt-get 更新和 dist-upgrade ......我相信它挂在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 docker 容器上运行 ansible-playbook 以在 AWS EC2 Ubuntu 实例上执行命令? - How can I run ansible-playbook on docker container to execute commands on an AWS EC2 Ubuntu instance? Ubuntu 中 Ansible 剧本的问题 - Issue with Ansible Playbook in Ubuntu 在 Ubuntu/Ansible 上运行 playbook 时出现 ModuleNotFoundError - ModuleNotFoundError when running playbook on Ubuntu/Ansible Ansible 剧本,用于在 Ubuntu 上安装和配置 MySQL - Ansible playbook to install and configure MySQL on Ubuntu ansible playbook 的变量的值如何在另一个 paybook 中使用? - How can the value of a ansible playbook's variable is used in another paybook? 如何再次从 Windows 重新启动到 Windows? (远程,windows-ubuntu 多重引导) - How can I reboot from windows to windows again? (remotely , windows-ubuntu multiboot) Ansible playbook 在新的 Ubuntu/Ansible/Python 环境中停止工作 - Ansible playbook stops working in new Ubuntu/Ansible/Python environment 如何通过 ansible 剧本添加存储库 - How to add repository by ansible playbook 如何在 Ubuntu 中使用 Teamviewer GUI? - How can I use Teamviewer GUI in Ubuntu? ansible 剧本有时会失败,但并非总是在 aws ubuntu 20.04 上安装 jre - ansible playbook fails sometimes but not always to install jre on aws ubuntu 20.04
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM