Can't make Ansible to wait for a server to reboot and continue playbook to work












4















I am creating a playbook to install a software and I need to restart the server int he middle of the playbook.



I need my Ansible host to wait till the server is back and execute the rest of my tasks but this is not happenning.



These are my Ansible tasks related to reboot and wait:



...
- name: restart server
shell: shutdown -r now
async: 1
poll: 0
become: yes
become_method: sudo
ignore_errors: true
- name: waiting for server to come back after reboot
local_action: wait_for host={{ ansible_ssh_host }} state=started port=22 delay=30 timeout=300 connect_timeout=15
....


This is the output of running my playbook:



...

TASK [restart server] **********************************************************
fatal: [X.X.X.X]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
to retry, use: --limit @ansible_pb.retry

PLAY RECAP *********************************************************************
X.X.X.X : ok=2 changed=0 unreachable=1 failed=0


Any idea what is wrong and how can I fix it?










share|improve this question





























    4















    I am creating a playbook to install a software and I need to restart the server int he middle of the playbook.



    I need my Ansible host to wait till the server is back and execute the rest of my tasks but this is not happenning.



    These are my Ansible tasks related to reboot and wait:



    ...
    - name: restart server
    shell: shutdown -r now
    async: 1
    poll: 0
    become: yes
    become_method: sudo
    ignore_errors: true
    - name: waiting for server to come back after reboot
    local_action: wait_for host={{ ansible_ssh_host }} state=started port=22 delay=30 timeout=300 connect_timeout=15
    ....


    This is the output of running my playbook:



    ...

    TASK [restart server] **********************************************************
    fatal: [X.X.X.X]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
    to retry, use: --limit @ansible_pb.retry

    PLAY RECAP *********************************************************************
    X.X.X.X : ok=2 changed=0 unreachable=1 failed=0


    Any idea what is wrong and how can I fix it?










    share|improve this question



























      4












      4








      4


      2






      I am creating a playbook to install a software and I need to restart the server int he middle of the playbook.



      I need my Ansible host to wait till the server is back and execute the rest of my tasks but this is not happenning.



      These are my Ansible tasks related to reboot and wait:



      ...
      - name: restart server
      shell: shutdown -r now
      async: 1
      poll: 0
      become: yes
      become_method: sudo
      ignore_errors: true
      - name: waiting for server to come back after reboot
      local_action: wait_for host={{ ansible_ssh_host }} state=started port=22 delay=30 timeout=300 connect_timeout=15
      ....


      This is the output of running my playbook:



      ...

      TASK [restart server] **********************************************************
      fatal: [X.X.X.X]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
      to retry, use: --limit @ansible_pb.retry

      PLAY RECAP *********************************************************************
      X.X.X.X : ok=2 changed=0 unreachable=1 failed=0


      Any idea what is wrong and how can I fix it?










      share|improve this question
















      I am creating a playbook to install a software and I need to restart the server int he middle of the playbook.



      I need my Ansible host to wait till the server is back and execute the rest of my tasks but this is not happenning.



      These are my Ansible tasks related to reboot and wait:



      ...
      - name: restart server
      shell: shutdown -r now
      async: 1
      poll: 0
      become: yes
      become_method: sudo
      ignore_errors: true
      - name: waiting for server to come back after reboot
      local_action: wait_for host={{ ansible_ssh_host }} state=started port=22 delay=30 timeout=300 connect_timeout=15
      ....


      This is the output of running my playbook:



      ...

      TASK [restart server] **********************************************************
      fatal: [X.X.X.X]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
      to retry, use: --limit @ansible_pb.retry

      PLAY RECAP *********************************************************************
      X.X.X.X : ok=2 changed=0 unreachable=1 failed=0


      Any idea what is wrong and how can I fix it?







      ansible






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 29 '16 at 5:56









      techraf

      4,107111730




      4,107111730










      asked Jul 29 '16 at 5:47









      Manuel Sopena BallesterosManuel Sopena Ballesteros

      13317




      13317






















          3 Answers
          3






          active

          oldest

          votes


















          7














          This is a commonly known problem. See Reboot a server and wait for it to come back. Since Ansible 1.9.4 SSH loses connection before proceeding to the next task.



          You need to add a delay (sleep) before the shutdown command:



          - name: restart server
          shell: sleep 2 && shutdown -r now
          async: 1
          poll: 0
          become: yes
          become_method: sudo
          ignore_errors: true
          - name: waiting for server to come back after reboot
          wait_for_connection:
          ....





          share|improve this answer

































            1














            On Ansible 2.7, the reboot module was introduced, which solves this problem.



            You can use:



            - name: restart server
            reboot:





            share|improve this answer































              0














              I found that this worked for me for EX-407 (which is based on Ansible 2.3):



              - name: restart the server
              shell: (sleep 2 && shutdown -r now) &
              async: 1
              poll: 0
              ignore_errors: true

              - name: wait for the server to come back
              wait_for:
              host: "{{ inventory_hostname }}"
              port: 22
              delay: 15
              delegate_to: localhost


              What was a surprise to me was that "state: started" option in the wait_for module was initially causing the whole thing to fail; wait_for was not able to discover that port 22 is opened again. Can't explain that behavior though. Once "state" option was removed, the playbook started to work as expected.






              share|improve this answer

























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "3"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1106349%2fcant-make-ansible-to-wait-for-a-server-to-reboot-and-continue-playbook-to-work%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                7














                This is a commonly known problem. See Reboot a server and wait for it to come back. Since Ansible 1.9.4 SSH loses connection before proceeding to the next task.



                You need to add a delay (sleep) before the shutdown command:



                - name: restart server
                shell: sleep 2 && shutdown -r now
                async: 1
                poll: 0
                become: yes
                become_method: sudo
                ignore_errors: true
                - name: waiting for server to come back after reboot
                wait_for_connection:
                ....





                share|improve this answer






























                  7














                  This is a commonly known problem. See Reboot a server and wait for it to come back. Since Ansible 1.9.4 SSH loses connection before proceeding to the next task.



                  You need to add a delay (sleep) before the shutdown command:



                  - name: restart server
                  shell: sleep 2 && shutdown -r now
                  async: 1
                  poll: 0
                  become: yes
                  become_method: sudo
                  ignore_errors: true
                  - name: waiting for server to come back after reboot
                  wait_for_connection:
                  ....





                  share|improve this answer




























                    7












                    7








                    7







                    This is a commonly known problem. See Reboot a server and wait for it to come back. Since Ansible 1.9.4 SSH loses connection before proceeding to the next task.



                    You need to add a delay (sleep) before the shutdown command:



                    - name: restart server
                    shell: sleep 2 && shutdown -r now
                    async: 1
                    poll: 0
                    become: yes
                    become_method: sudo
                    ignore_errors: true
                    - name: waiting for server to come back after reboot
                    wait_for_connection:
                    ....





                    share|improve this answer















                    This is a commonly known problem. See Reboot a server and wait for it to come back. Since Ansible 1.9.4 SSH loses connection before proceeding to the next task.



                    You need to add a delay (sleep) before the shutdown command:



                    - name: restart server
                    shell: sleep 2 && shutdown -r now
                    async: 1
                    poll: 0
                    become: yes
                    become_method: sudo
                    ignore_errors: true
                    - name: waiting for server to come back after reboot
                    wait_for_connection:
                    ....






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 17 '18 at 8:50

























                    answered Jul 29 '16 at 5:52









                    techraftechraf

                    4,107111730




                    4,107111730

























                        1














                        On Ansible 2.7, the reboot module was introduced, which solves this problem.



                        You can use:



                        - name: restart server
                        reboot:





                        share|improve this answer




























                          1














                          On Ansible 2.7, the reboot module was introduced, which solves this problem.



                          You can use:



                          - name: restart server
                          reboot:





                          share|improve this answer


























                            1












                            1








                            1







                            On Ansible 2.7, the reboot module was introduced, which solves this problem.



                            You can use:



                            - name: restart server
                            reboot:





                            share|improve this answer













                            On Ansible 2.7, the reboot module was introduced, which solves this problem.



                            You can use:



                            - name: restart server
                            reboot:






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 8 '18 at 9:09









                            Gert van den BergGert van den Berg

                            279411




                            279411























                                0














                                I found that this worked for me for EX-407 (which is based on Ansible 2.3):



                                - name: restart the server
                                shell: (sleep 2 && shutdown -r now) &
                                async: 1
                                poll: 0
                                ignore_errors: true

                                - name: wait for the server to come back
                                wait_for:
                                host: "{{ inventory_hostname }}"
                                port: 22
                                delay: 15
                                delegate_to: localhost


                                What was a surprise to me was that "state: started" option in the wait_for module was initially causing the whole thing to fail; wait_for was not able to discover that port 22 is opened again. Can't explain that behavior though. Once "state" option was removed, the playbook started to work as expected.






                                share|improve this answer






























                                  0














                                  I found that this worked for me for EX-407 (which is based on Ansible 2.3):



                                  - name: restart the server
                                  shell: (sleep 2 && shutdown -r now) &
                                  async: 1
                                  poll: 0
                                  ignore_errors: true

                                  - name: wait for the server to come back
                                  wait_for:
                                  host: "{{ inventory_hostname }}"
                                  port: 22
                                  delay: 15
                                  delegate_to: localhost


                                  What was a surprise to me was that "state: started" option in the wait_for module was initially causing the whole thing to fail; wait_for was not able to discover that port 22 is opened again. Can't explain that behavior though. Once "state" option was removed, the playbook started to work as expected.






                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    I found that this worked for me for EX-407 (which is based on Ansible 2.3):



                                    - name: restart the server
                                    shell: (sleep 2 && shutdown -r now) &
                                    async: 1
                                    poll: 0
                                    ignore_errors: true

                                    - name: wait for the server to come back
                                    wait_for:
                                    host: "{{ inventory_hostname }}"
                                    port: 22
                                    delay: 15
                                    delegate_to: localhost


                                    What was a surprise to me was that "state: started" option in the wait_for module was initially causing the whole thing to fail; wait_for was not able to discover that port 22 is opened again. Can't explain that behavior though. Once "state" option was removed, the playbook started to work as expected.






                                    share|improve this answer















                                    I found that this worked for me for EX-407 (which is based on Ansible 2.3):



                                    - name: restart the server
                                    shell: (sleep 2 && shutdown -r now) &
                                    async: 1
                                    poll: 0
                                    ignore_errors: true

                                    - name: wait for the server to come back
                                    wait_for:
                                    host: "{{ inventory_hostname }}"
                                    port: 22
                                    delay: 15
                                    delegate_to: localhost


                                    What was a surprise to me was that "state: started" option in the wait_for module was initially causing the whole thing to fail; wait_for was not able to discover that port 22 is opened again. Can't explain that behavior though. Once "state" option was removed, the playbook started to work as expected.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Feb 13 at 22:00









                                    Scott

                                    16.1k113990




                                    16.1k113990










                                    answered Feb 13 at 21:39









                                    Tomasz Papir-ZwierzTomasz Papir-Zwierz

                                    1




                                    1






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Super User!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1106349%2fcant-make-ansible-to-wait-for-a-server-to-reboot-and-continue-playbook-to-work%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                        Mangá

                                        Eduardo VII do Reino Unido