---
- name: revert
  hosts: all
  vars_files:
    - vars/vars_prod.yml
  tasks:
    #
    # Get last two deploy versions
    #
    - shell: ls -1 {{ versions_dir }} |  sort -n | tac | head -n 2
      register: last_deploy
    #
    # Get number of deployments available on servers
    #
    - shell: ls -1 {{ versions_dir }} |  sort -n | tac | head -n 2 | wc -l
      register: last_deploy_num

    #
    # If there are less then 2 deployments on server, just fail.
    # There is nothing to rollback to.
    #
    - fail: msg="Less than 2 deploys on server left, cannot rollback!."
      when: " last_deploy_num.stdout != '2' "

    #
    # After rollback, change symlink to previous deployment
    #
    - file: state=link src={{ versions_dir }}/{{ last_deploy.stdout_lines[1] }} dest={{ project_dir }}/current

    #
    # After modifying symlink, reload wsgi app using supervisord
    #
    - supervisorctl: name={{ app_name }}-wsgi state=restarted
      sudo: True

    #
    # Clean up failed deployment
    #
    - file: state=absent recurse=yes path={{ versions_dir }}/{{ last_deploy.stdout_lines[0]}}



---
- name: revert
  hosts: all
  vars_files:
    - vars/vars_prod.yml
  tasks:
    #
    # Get last two deploy versions
    #
    - shell: ls -1 {{ versions_dir }} | grep -o '[0-9]\{10\}' |  sort -n | tac | head -n 2
      register: last_deploy
    #
    # Get number of deployments available on servers
    #
    - shell: ls -1 {{ versions_dir }} | grep -o '[0-9]\{10\}' |  sort -n | tac | head -n 2 | wc -l
      register: last_deploy_num

    #
    # If there are less then 2 deployments on server, just fail.
    # There is nothing to rollback to.
    #
    - fail: msg="Less than 2 deploys on server left, cannot rollback!."
      when: " last_deploy_num.stdout != '2' "

    #
    # After rollback, change symlink to previous deployment
    #
    - file: state=link src={{ versions_dir }}/{{ last_deploy.stdout_lines[1] }} dest={{ project_dir }}/current

    #
    # After modifying symlink, reload wsgi app using supervisord
    #
    - supervisorctl: name={{ app_name }}-wsgi state=restarted
      sudo: True

    #
    # Clean up failed deployment
    #
    - file: state=absent recurse=yes path={{ versions_dir }}/{{ last_deploy.stdout_lines[0]}}



Revert only to dirs, that have the name as they would be timestamps.
Revert only to dirs, that have the name as they would be timestamps.

Better safe than sorry.
