Only running command if file doesn’t exist Ansible

I have a simple build script which installs wkhtmltophf on a server. However I only want this script to be run if the software isn’t already installed. Essentially, I want an if statement or check statement in Ansible. I.e. If file doesn’t exist then run this command.

I have read the Ansible documentation multiple times and it isn’t completely clear how to do this. Anyhow, here is the answer:

- name: If file don't exist run command
  command: /root/ creates=/usr/bin/wkhtmltopdf

This is command say “This command creates this file and so if the file doesn’t exist then run the command”.

I hope this helps someone

2 thoughts to “Only running command if file doesn’t exist Ansible”

  1. – name: check for file
    stat: path=/usr/bin/wkhtmltopdf
    register: file_exists

    – name: run command
    command: /root/
    when: not file_exists.stat.exists

Leave a Reply to Jay Grossman Cancel reply