0

I am running ansible playbook to restart some of our servers but we need to sleep for 40 minutes between each server restart so if I sleep for 40 minutes in my playbook then it sleeps for a while but then my session gets terminated on Ubuntu box in prod and whole script is also stopped. Is there anything I can add in ansible playbook so that it can keep my session alive during the time whole playbook is running?

# This will restart servers
---
- hosts: tester
  serial: "{{ num_serial }}"
  tasks:
      - name: copy files
        copy: src=conf.prod dest=/opt/process/config/conf.prod owner=goldy group=goldy

      - name: stop server
        command: sudo systemctl stop server_one.service

      - name: start server
        command: sudo systemctl start server_one.service

      - name: sleep for 40 minutes
        pause: minutes=40

I want to sleep for 40 minutes without terminating my linux session and then move to next set of servers restart.

I am running ansible 2.6.3 version.

flash
  • 1,455
  • 11
  • 61
  • 132

1 Answers1

1

You can run your ansible script inside screen in order to keep the session alive even after disconnection.

Basically what you want to do is ssh into the production server, run screen, then execute the playbook inside the newly created session.

If you ever get disconnected, you can connect back to the server, then run screen -r to get back into your saved session.

Alassane Ndiaye
  • 4,427
  • 1
  • 10
  • 19
  • so if I run my ansible script inside screen session then it won't get terminated at all? – flash Apr 15 '19 at 19:38
  • You might get disconnected from the server. But you can reconnect to the server, run screen -r and your session will be back. – Alassane Ndiaye Apr 15 '19 at 19:40
  • so lets say if I get disconnected but my ansible playbook will still keep running right? and I can check that by doing `screen -r` correct? when you say my session will be back, my playbook will be running in that session right? – flash Apr 15 '19 at 19:40
  • Yes, you connect back to the server, then execute screen -r – Alassane Ndiaye Apr 15 '19 at 19:41
  • If I don't connect back to the server for a while then still my playbook will be running right in that session? Just making sure. – flash Apr 15 '19 at 19:42
  • Yes, you just need to reconnect to the screen session, you can try it yourself. ssh into your server, run screen, execute a command, disconnect from the server, reconnect to the server and lauch screen -r. – Alassane Ndiaye Apr 15 '19 at 19:46