Performing tasks based on whether file or folder exists

From Notes_Wiki
Revision as of 04:18, 10 March 2015 by Saurabh (talk | contribs) (Created page with "<yambe:breadcrumb>Ansible_tips_and_tricks|Ansible tips and tricks</yambe:breadcrumb> =Performing tasks based on whether file or folder exists= Now many modules already suppor...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<yambe:breadcrumb>Ansible_tips_and_tricks|Ansible tips and tricks</yambe:breadcrumb>

Performing tasks based on whether file or folder exists

Now many modules already support creates attribute to skip certain step if a there is already a given file or folder. But certain modules such as template module do not support such option. In such cases to copy template only when a certain file exists or does not exists following combination of stat and template module can be used:

- stat: path=/etc/sysconfig/iptables
  register: iptables_rules

- name: Copy iptables file if not present already
  template: src=iptables dest=/etc/sysconfig/iptables 
  when: iptables_rules.stat.exists == False
  notify: 
    - restart iptables