ansible reboot 2.1.1.0 fails
Asked Answered
H

3

7

I've been trying to create a really simple Ansible playbook that will reboot a server and wait for it to come back.

I've had one in the past on Ansible 1.9 that worked, but i've recently upgraded to 2.1.1.0 and it fails.

The host i'm rebooting is called idm and has an IP of 192.168.200.23.

the playbook is being run from my host 192.168.200.1 with the command

$ ansible-playbook reboot.yml  -vvvv

This is the playbook i'm using

---
- hosts: idm

  tasks:
    - name: Restart server
      become: yes
      shell: sleep 2 && /sbin/shutdown -r now "Ansible system package upgraded"


    - name: waiting 60 secs for server to come back
      local_action: wait_for host=192.168.200.23 port=22 delay=60 connect_timeout=200
      become: false

It reboots the machine fine, but almost immediately fails with

<192.168.200.23> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.200.23> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/home/myork/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.200.23 '/bin/sh -c '"'"'LANG=en_GB.UTF-8 LC_ALL=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1472242630.58-176546106960362/command; rm -rf "/root/.ansible/tmp/ansible-tmp-1472242630.58-176546106960362/" > /dev/null 2>&1 && sleep 0'"'"''
fatal: [idm]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}

Any idea what i've missed here ?

Thanks!

Heshvan answered 26/8, 2016 at 20:20 Comment(1)
Possible duplicate of How to wait for server restart using Ansible?Modulator
S
11

I reboot servers with async fire and forget mode:

- name: Restart server
  become: yes
  shell: sleep 2 && /sbin/shutdown -r now "Ansible system package upgraded"
  async: 1
  poll: 0
Songful answered 26/8, 2016 at 20:36 Comment(2)
Thanks, I'll test that now. I was hoping to try and keep control. I don't want to take another server down until this one is back up.Heshvan
@Heshvan you still have full control with that. async is for ansible to know, that you don't care about that exact task. But you have the next one to be sure that server is booted.Songful
H
4

The following code from Konstantin worked perfectly.

- name: Restart server
  become: yes
  shell: sleep 2 && /sbin/shutdown -r now "Ansible system package upgraded"
  async: 1
  poll: 0

- name: waiting 60 secs for server to come back
  local_action: wait_for host=192.168.200.23 port=22 delay=20 connect_timeout=200
  become: false
  delegate_to:  127.0.0.1
Heshvan answered 26/8, 2016 at 20:45 Comment(0)
N
1

UPDATE with the comment of @Konstantin Suvorov.

Here a better solution to avoid hard IP

- name: Restart server
  shell: /sbin/shutdown -r now "Ansible system package upgraded"
  async: 1
  poll: 0
- set_fact: wait_host="{{ ansible_host }}"
- name: Wait for server to come back
  local_action: wait_for host={{wait_host}} delay=20 port=22 state=started
  become: false

Eric

Nasser answered 6/7, 2017 at 15:9 Comment(3)
Better to use ansible_host, because inventory names can be non-routable aliases.Songful
ansible_host will return localhost (because local_action). github.com/ansible/ansible/issues/16139Nasser
good catch! need to prepend with helper task set_fact: wait_host="{{ ansible_host }}" and use {{ wait_host }}.Songful

© 2022 - 2024 — McMap. All rights reserved.