Ansible in a Minute · Ansible Core ·
Use default omit to skip an optional module argument
Short answer
Use default(omit) so Ansible omits a module argument when the variable is undefined instead of sending a value.
Subscribe for updates
Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.
By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.
Request
touch files with optional mode using default(omit)
Example
A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.
---
- name: Touch files with an optional mode
hosts: all
gather_facts: false
tasks:
- name: Touch files with an optional mode
ansible.builtin.file:
dest: "{{ item.path }}"
state: touch
mode: "{{ item.mode | default(omit) }}"
loop:
- path: /tmp/foo
- path: /tmp/bar
- path: /tmp/baz
mode: "0444"How it works
default(omit) removes the module argument when the value is undefined. Ansible does not send mode for /tmp/foo or /tmp/bar, so the system umask applies. Only /tmp/baz receives mode 0444.
When to use it
- Optional module arguments that should fall back to system defaults
- Loop items that set a field only on some entries
Requirements
- Ansible: >=2.14
Validation
- Syntax validated
- Not execution-tested
- Last verified 2026-09-16
Notes
default(5) substitutes a value. default(omit) omits the argument. Do not chain filters after omit in a way that turns it back into a normal value.
Reference
- Making variables optional (Ansible docs)
Built for every Ansible use case in every organization
BacklogZero can generate this kind of automation in your environment, just like it did here.