带有剧本的Ansible重启Linux机器或服务器

时间:2020-01-09 10:37:08  来源:igfitidea点击:

我需要使用Ansible重启VM或裸机Linux机器/服务器,然后等待它回来,但是它不适用于此处所述的剧本。
如何使用Ansible重启Linux服务器?
如何在多台Linux服务器的Ansible剧本中重新启动并等待重新启动完成?
您可以重新引导基于Linux或Unix的计算机,等待其关闭(例如内核更新),重新启动并响应命令。
内核更新后,可以使用命令或外壳模块重新引导Linux服务器。
但是,现在我们有了一个重启模块,可以使用Ansible重启计算机。

配置要求

请注意,您必须具有Ansible 2.7或更高版本才能使用重启模块:

$ ansible --version

如果未使用Ansible版本2.7,请尝试根据您的Linux发行版使用dnf命令/yum命令/apt命令/apt-get命令对其进行更新:

$ sudo apt update ## Debian or Ubuntu box ##
$ sudo yum update ## RHEL/CentOS 7 ##

带有playbook的Ansible重启Linux服务器

重新启动的语法非常简单:

- name: Reboot the machine with all defaults using Ansible
  reboot:

这是使用cat命令显示的示例主机文件:

[all:vars]
k_ver="linux-image-4.15.0-36-generic"
ansible_user='{{ my_c_user }}'
ansible_become=yes
ansible_become_method=sudo
ansible_become_pass='{{ my_c_sudo_pass }}'
 
[legacy]
do-de.public
 
[cluster]
ln.cbz01 
ln.cbz02 
ln.cbz04 
ln.forum 
 
[lxd]
ln.cbz01
ln.cbz02
ln.cbz04
do-de.public 
 
[vpn:vars]
ansible_python_interpreter='/usr/bin/env python3'
 
[vpn]
do-blr-vpn
 
[backup]
gcvm.backup
 
[nodes:children]
vpn
backup
cluster
legacy
 
[isrestart:children]
backup
cluster
vpn

这是我的reboot.yml:

--
- hosts: isrestart
  become: true
  become_user: root
  tasks:
          - name: Rebooting the cloud server/bare metal box
            reboot:

如何使用Ansible重启模块手册来重启盒子

现在您所要做的就是运行剧本(请参阅如何为Ansible Vault设置和使用sudo密码)

$ ansible-playbook -i hosts --ask-vault-pass --extra-vars '@cluster.data.yml' reboot.yml

如何重新启动计算机并设置超时值

默认情况下,Ansible重启模块等待600秒。
您可以使用以下语法增加价值:

- name: Reboot a Linux machine 
  reboot:
    reboot_timeout: 1800

如何设置命令以在重新引导的主机上运行并期望成功,从而确定机器已准备好执行其他任务

缺省情况下,ansbile使用的whoami命令。
您可以如下进行更改:

- name: Reboot a Linux machine 
  reboot:
    test_command: uptime

要么

- name: Reboot a Linux machine 
  reboot:
    test_command: ping -c 4 192.168.2.254

如何设置重启前后的延迟

在重新启动成功并在几秒钟内重新建立连接后,可以迫使Ansible等待,如下所示:

- name: Unconditionally reboot the machine with all defaults
  reboot:
    post_reboot_delay: 180

如果您想等待其他网络连接/存储或服务器vpn启动,尽管连接已经在工作,则上述内容很有用。
您还可以设置关机时间以等待重新启动之前等待:

- name: Unconditionally reboot the machine with all defaults
  reboot:
    pre_reboot_delay: 180

查看Linux服务器上的重启日志历史记录

假设我正在有条件地重启我的Ubuntu或Debian Linux机器。
例如,我的Ansible剧本:

- name: Check if a reboot is needed on AWS EC2 Ubuntu/Debian based servers
        register: reboot_required_file
        stat: path=/var/run/reboot-required get_md5=no
 
      - name: Reboot the box if kernel updated/installed on EC2 
        reboot:
          msg: "Reboot initiated by Ansible for kernel updates"
          connect_timeout: 5
          reboot_timeout: 300
          pre_reboot_delay: 0
          post_reboot_delay: 30
          test_command: uptime
        when: reboot_required_file.stat.exists

我们可以在服务器上搜索由Ansible发起的重新启动以获取服务器上的内核更新,以查看何时使用grep命令/zgrep命令重新启动我的机器:

$ ssh [email protected]
$ sudo grep 'reboot' /var/log/auth.log
$ sudo zgrep 'Reboot initiated by Ansible for kernel updates' /var/log/auth.log*
$ sudo zgrep 'reboot' /var/log/auth.log*

输出示例:

auth.log:Jun  9 11:06:57 ls-debian-10 systemd-logind[488]: System is rebooting (Reboot initiated by Ansible for kernel updates).
auth.log.2.gz:May 27 04:55:54 ip-172-26-14-129 sudo:    admin : TTY=pts/0 ; PWD=/home/admin ; USER=root ; COMMAND=/sbin/reboot

另一个选择是运行最后一个命令:

$ sudo last -x "reboot"

重启历史:

reboot   system boot  4.19.0-9-amd64   Wed Jun 10 03:51   still running
reboot   system boot  4.19.0-9-amd64   Tue Jun  9 11:07 - 03:51  (16:43)
reboot   system boot  4.19.0-9-amd64   Wed May 27 04:56 - 11:06 (13+06:10)
reboot   system boot  4.9.0-12-amd64   Wed May 27 04:15 - 04:55  (00:40)
reboot   system boot  4.9.0-8-amd64    Wed May 27 04:09 - 04:14  (00:05)
reboot   system boot  4.9.0-8-amd64    Wed May 27 04:08 - 04:09  (00:01)

wtmp begins Wed May 27 04:08:01 2020

有关更多信息,请参见如何查找最后的系统Linux重新启动时间和日期命令。