nwinkler
nwinkler

Reputation: 54467

How to determine whether a script has previously run using Ansible?

I'm using Ansible to deploy (Git clone, run the install script) a framework to a server. The install step means running the install.sh script like this:

- name: Install Foo Framework
  shell: ./install.sh
  args:
    chdir: ~/foo

How can I determine whether I have executed this step in a previous run of Ansible? I want to add a when condition to this step that only executes if the install.sh script hasn't been run previously.

The install.sh script does a couple of things (replacing some files in the user's home directory), but it's not obvious whether the script was run before from just taking a look at the files. The ~/foo.sh file might have existed before, it's not clear whether it was replaced by the install script or was there before.

Is there a way in Ansible to store a value on the server that let's me determine whether this particular task has been executed before? Or should I just create a marker file in the user's home directory (e.g. ~/foo-installed) that I check in later invocations of the playbook?

Upvotes: 2

Views: 2772

Answers (2)

nwinkler
nwinkler

Reputation: 54467

Here's how I solved it in the end. The pointer to using the creates option helped:

- name: Install Foo Framework
  shell: ./install.sh && touch ~/foo_installed
  args:
    chdir: ~/foo
    creates: ~/foo_installed

Using this approach, the ~/foo_installed file is only created when the install script finished without an error.

Upvotes: 3

udondan
udondan

Reputation: 60029

I suggest to use the script module instead. This module has a creates parameter:

a filename, when it already exists, this step will not be run. (added in Ansible 1.5)

So your script then could simply touch a file which would prevent execution of the script in subsequent calls.

Upvotes: 3

Related Questions