Reputation: 197
Ansible is great for rolling out (in our case JBoss). One part of the playbook is:
- name: copy jboss-eap-6.2.0.tar.gz to server
action: copy src=jboss-eap-6.2.0.tar.gz
dest=/tmp/jboss-eap-6.2.0.tar.gz
owner=root
group=root
- name: Extracting jboss-eap-6.2.0.tar.gz
command: /bin/tar xfz /tmp/jboss-eap-6.2.0.tar.gz -C /opt
which works like a charm, except it works every time, and I would like Ansible to stop the playbook when JBoss is already installed (in our case, the test is if /opt/jboss-eap-6.2.0
does exist. What is the neatest way to do this ? Can you provide a test if something exist on a target server in a when-clause ?
Upvotes: 0
Views: 1904
Reputation: 2950
If you only wanted to skip command execution then creates
parameter of command
module would solve your problem. It instructs the module to skip execution of the command if destination file/directory already exists.
Here's an example:
- name: Extracting jboss-eap-6.2.0.tar.gz
command: creates=/opt/jboss_dir /bin/tar xfz /tmp/jboss-eap-6.2.0.tar.gz -C /opt
Your particular case is a bit more tricky. Firstly, your archive may not survive restarts because you're copying to /tmp
, so it may be copied every time you execute the playbook. Secondly it may be required to introduce an extra task only to check for jboss directory existence.
The following task will:
/opt/jboss_dir
directory if it doesn't exist yet, the result of this task will be stored in jbossDirectory
variablejbossDirectory
has changed
status, the archive will be copied to /tmp
changed
status will also cause extraction of archiveExample playbook:
- name: create jboss directory
file: state=directory dest=/opt/jboss_dir owner=root user=root
register: jbossDirectory
- name: copy jboss-eap-6.2.0.tar.gz to server
copy: src=jboss-eap-6.2.0.tar.gz dest=/tmp/jboss-eap-6.2.0.tar.gz owner=root group=root
when: jbossDirectory|changed
- name: extracti jboss-eap-6.2.0.tar.gz
command: /bin/tar xfz /tmp/jboss-eap-6.2.0.tar.gz -C /opt
when: jbossDirectory|changed
Upvotes: 2