- hosts: "{{host}}" gather_facts: false vars: repo: "repo_name" remote_dir: "/path/to/deploy_dir" tasks: - name: check whether {{repo}}.bak exists shell: ls {{repo}}.bak || true args: chdir: "{{remote_dir}}" register: repo_bak_exists - name: restore old {{repo}} shell: cd {{remote_dir}} && rm -rf {{repo}} && mv {{repo}}.bak {{repo}} when: repo_bak_exists.stdout != '' - debug: msg: "Not found {{repo}}.bak, quit" when: repo_bak_exists.stdout == ''