Ansible - module_defaults parameter
by
Jeremy Canfield |
Updated: January 04 2021
| Ansible articles
Let's say you are calling a module many times with similar parameters, like this.
---
- hosts: all
tasks:
- name: copy foo.txt
copy:
src: /tmp/foo.txt
dest: /tmp/foo.txt
owner: john.doe
group: john.doe
mode: "0644"
- name: copy bar.txt
copy:
src: /tmp/bar.txt
dest: /tmp/bar.txt
owner: john.doe
group: john.doe
mode: "0644"
- name: copy foobar.txt
copy:
src: /tmp/foobar.txt
dest: /tmp/foobar.txt
owner: john.doe
group: john.doe
mode: "0644"
The module_defaults parameter can be used to define default parameters for another module, like this.
---
- hosts: all
module_defaults:
copy:
owner: john.doe
group: john.doe
mode: "0644"
tasks:
- name: copy foo.txt
copy:
src: /tmp/foo.txt
dest: /tmp/foo.txt
- name: copy bar.txt
copy:
src: /tmp/bar.txt
dest: /tmp/bar.txt
- name: copy foobar.txt
copy:
src: /tmp/foobar.txt
dest: /tmp/foobar.txt
Did you find this article helpful?
If so, consider buying me a coffee over at