Performing tasks based on whether file or folder exists

From Notes_Wiki
Revision as of 11:52, 28 July 2022 by Saurabh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Home > CentOS > CentOS 6.x > System administration tools > ansible > Ansible tips and tricks > 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



Home > CentOS > CentOS 6.x > System administration tools > ansible > Ansible tips and tricks > Performing tasks based on whether file or folder exists