Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Ansible - Only if a file exists or does not exist

Published: 27-12-2014 | Author: Remy van Elst | Text only version of this article


Table of Contents


This Ansible playbook example helps you execute actions only if a file exists ordoes not exist. If you for example have a command you need to run to generate acertificate (or Diffie Hellman parameters for nginx) you only want to do thatonce. The command itself is not convergent so it will run with every ansiblerun. However, the command creates a file and Ansible is able to check if thatfile exists. If the file exists, it will not execute the action. The same goesfor checking if a file does exist and only executing the action if it exists.(The action you want to do will remove that file).

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

The below example command will generate Diffi Hellman parameters for NGINXssl. This command creates the file /etc/ssl/certs/dhparam.pem. It shouldrun only if that file does not exist (because only newly deployed servers willnot have the file), if the file exist there is no need to run again.

- name: generate dh paramscommand: sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048 args:   creates: /etc/ssl/certs/dhparam.pem

Ansible has the creates option in the command module. Give it a filename(directories will not work) and if it already exists Ansible will skip theaction.

The same goes for only executing an action if a file exists. The command you areusing will remove that file, so only if the file is there the action should beexecuted. Just as the creates option, there is the removes option. For theremoves option, you need at least Ansible 0.8.

The below example is for a custom piece of software one of my clients uses. Ifwe deploy a new version, we check out the code repository and run a script toinstall a new version. That script will only run when the configuration file isrenamed to software.conf.upgrade. After the upgrade it renamed that configfile to the original software.conf and also puts the config in its database.It is sadly proprietary software and the manufacturer has stated they are notchanging the behavior to a more sane default. The below example will only runthe upgrade script when the file /etc/software/software.conf.upgrade exists.Since the script removes it, the next time Ansible runs it does not try toupgrade the software.

- name: upgrade softwarecommand: /opt/software/bin/upgrade args:  removes: etc/software/software.conf.upgrade

Documentation for the Command Module

If you have other commands which do not support the creates option, you needto first use the stat module and register the result of that. This example isfor the Shorewall firewall. We first check if the rules file exists:

- name: check if rules file exists  stat:     path: /etc/shorewall/rules  register: shorewall_rules

We fill the shorewall_rules variable with the result of this action. The nexttwo actions add a rule to the rules file and restart the firewall, but only ifthe rules file exists:

- name: add firewall rule for ssh  lineinfile:     dest: /etc/shorewall/rules     state: present     regexp: "^ACCEPT net0:192\.0\.2\.22 \$FW tcp 5666"     line: "ACCEPT net0:192.0.2.22 $FW tcp 5666"  when: shorewall_rules.stat.exists == true- name: restart shorewall  command: "shorewall restart"  when: shorewall_rules.stat.exists == True

If you want to do stuff when a file is not present, you can check if the resultis False, like so:

- action: example  when: stat_result.stat.exists == False
Tags: ansible, configuration-management, deployment, devops, nginx, openssl, tutorials