moppag
moppag

Reputation: 1096

How can I run a command only if an online file has changed in ansible

I have the following task in my ansible playbook:

- name: download last buildtools
  get_url:
    url: https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
    dest: ~/buildtools/BuildTools.jar

I want the following command to be run only if the BuildTools.jar file has changed:

java -jar BuildTools.jar --rev latest

How can I do this in my ansible playbook ?

EDIT: Just to be clear, I want the command to be triggered only if the version of the file at the url above has changed. So, in the case, each time a new version of minecraft is supported by the buildtools.

Upvotes: 2

Views: 2272

Answers (1)

moppag
moppag

Reputation: 1096

Finally, I found an elegant way to do what I wanted:

From the doc of the force parameter in the get_url module,

If yes and dest is not a directory, will download the file every time and replace the file if the contents change.

So my config is now:

- name: Download the last buildtools
  get_url:
    url: https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
    dest: ~/buildtools/BuildTools.jar
    force: yes
  register: buildtools

- name: Update BuildTools
  command: java -jar BuildTools.jar --rev latest
  args:
    chdir: ~/buildtools/
  when: buildtools.changed

So the Update BuildTools task is only run if the file downloaded at the url is different of the original file on the server, or if it is the first time that the file is downloaded.

Upvotes: 3

Related Questions