How to wait for server restart using Ansible?

后端 未结 11 1175
半阙折子戏
半阙折子戏 2020-12-07 13:06

I\'m trying to restart the server and then wait, using this:

- name: Restart server
  shell: reboot

- name: Wait for server to restart
  wait_for:
    port=         


        
11条回答
  •  执笔经年
    2020-12-07 13:28

    Through trial and error + a lot of reading this is what ultimately worked for me using the 2.0 version of Ansible:

    $ ansible --version
    ansible 2.0.0 (devel 974b69d236) last updated 2015/09/01 13:37:26 (GMT -400)
      lib/ansible/modules/core: (detached HEAD bbcfb1092a) last updated 2015/09/01 13:37:29 (GMT -400)
      lib/ansible/modules/extras: (detached HEAD b8803306d1) last updated 2015/09/01 13:37:29 (GMT -400)
      config file = /Users/sammingolelli/projects/git_repos/devops/ansible/playbooks/test-2/ansible.cfg
      configured module search path = None
    

    My solution for disabling SELinux and rebooting a node when needed:

    ---
    - name: disable SELinux
      selinux: state=disabled
      register: st
    
    - name: reboot if SELinux changed
      shell: shutdown -r now "Ansible updates triggered"
      async: 0
      poll: 0
      ignore_errors: true
      when: st.changed
    
    - name: waiting for server to reboot
      wait_for: host="{{ ansible_ssh_host | default(inventory_hostname) }}" port={{ ansible_ssh_port | default(22) }} search_regex=OpenSSH delay=30 timeout=120
      connection: local
      sudo: false
      when: st.changed
    
    # vim:ft=ansible:
    

提交回复
热议问题