HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

reboot server during Ansible tasks

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
rebootduringtasksserveransible

Problem

I have 3 scripts that must be run on server one by one. for example:

- name: find config
  shell: /data/scripts/config1
  become: yes
  become_user: root
  tags: find_config

- name: change config
  shell: /data/scripts/config2
  become: yes
  become_user: root
  tags: change_config

- name: confirm config
  shell: /data/scripts/config3
  become: yes
  become_user: dbusr1
  tags: confirm_config


in second script config2 there is a reboot command and then the rest of script will be run at start up. something like this:

some variables
.
.
commands before reboot
.
.
reboot command
.
.
commands that run at start up


does this reboot affect ansible tasks? how i can write tasks that wait until commands that run at start up be run then execute config3? is this even possible in ansible? what modules should i use?

Solution

Use this source code for reboot your client machine

---
  - name: System Reboot
    hosts: debian
    become_method: sudo
    become_user: root
    become: true
    tasks:
      - name: reboot nodes     # Reboot client side debian machine 
        shell: sleep 2 && shutdown -r now "Ansible reboot"
        async: 1
        poll: 0
        ignore_errors: true
      - name: wait for the server to come back  # when server timeout it through error. That error is handle by ignore_errors.
        local_action: wait_for
        args:
          host: "{{ inventory_hostname }}"
          port: 22
          state: started
          delay: 120
          timeout: 200
        ignore_errors: true

Code Snippets

---
  - name: System Reboot
    hosts: debian
    become_method: sudo
    become_user: root
    become: true
    tasks:
      - name: reboot nodes     # Reboot client side debian machine 
        shell: sleep 2 && shutdown -r now "Ansible reboot"
        async: 1
        poll: 0
        ignore_errors: true
      - name: wait for the server to come back  # when server timeout it through error. That error is handle by ignore_errors.
        local_action: wait_for
        args:
          host: "{{ inventory_hostname }}"
          port: 22
          state: started
          delay: 120
          timeout: 200
        ignore_errors: true

Context

StackExchange DevOps Q#8725, answer score: 4

Revisions (0)

No revisions yet.