Skip to content

Instantly share code, notes, and snippets.

@flaf
Created December 20, 2018 16:41
Show Gist options
  • Save flaf/e182e5146cee98a414b3d45434026856 to your computer and use it in GitHub Desktop.
Save flaf/e182e5146cee98a414b3d45434026856 to your computer and use it in GitHub Desktop.
Ansible: good syntax to manage or remove file in function of a variable

Is there a ad hoc idiomatic syntax to do that in Ansible, ie manage the content of a file if a variable is defined or remove the file if the same variable is not defined?

Or maybe this is the good way to do that?

- name: manage the content of /tmp/foo.txt
  template:
    src: foo.txt.j2
    dest: /tmp/foo.txt
    owner: root
    group: root
    mode: '0644'
  when: foo_var is not none

- name: ensure the file /tmp/foo.txt is absent
  file:
    state: absent
    path: /tmp/foo.txt
  when: foo_var is none
@flaf
Copy link
Author

flaf commented Jan 10, 2019

- name: manage the content of /tmp/foo.txt
  template:
    src: foo.txt.j2
    dest: /tmp/foo.txt
    owner: root
    group: root
    mode: '0644'
  when: foo_var

- name: ensure the file /tmp/foo.txt is absent
  file:
    state: absent
    path: /tmp/foo.txt
  when: not foo_var

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment