1

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?

nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • Did some further research and it looks like a combination of `touch` and the `creates` option should do the trick, similar to the answer here: http://stackoverflow.com/questions/26409164/ansible-ignore-the-run-once-configuration-on-task – nwinkler Mar 19 '15 at 12:54

2 Answers2

3

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.

udondan
  • 57,263
  • 20
  • 190
  • 175
  • Is the `~/.ansible` folder a good location for putting that marker file? Or should I steer clear of that? – nwinkler Mar 19 '15 at 12:20
  • Maybe it's not the best idea to use it since the Ansible guys could decide to wipe it at any time, e.g. on the upcoming update to Ansible 2.0. I'd prefer a folder you control, like your home folder or misuse `/var/log` (`/var/lock` sounds better suited for this but is wiped on reboot) – udondan Mar 19 '15 at 12:32
  • 1
    BTW: I just noticed that the `shell` module also has the `creates` parameter: http://docs.ansible.com/shell_module.html – nwinkler Mar 19 '15 at 12:53
3

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.

nwinkler
  • 52,665
  • 21
  • 154
  • 168