Search code examples
ansible

How can I run a ansible task only if a file or directory does NOT exist?


In an Ansible playbook I want to run tasks if a directory does not exists.

- name: Check for java exists in /opt     
  stat: path=/opt/jdk1.8.0_71
  register: p
  when: p.stat.isdir is defined and p.stat.isdir

But what must I do to ensure that the following tasks runs only if this dir does not exist?

- name: Extract java if dir not existing
  command: tar xzf /tmp/jdk1.8.0_71 chdir=/opt

Solution

  • - name: Extract java if dir not existing
      command: tar xzf /tmp/jdk1.8.0_71 chdir=/opt
      when: not p.stat.exists