# set module\_defaults globally?

**URL:** <https://forum.ansible.com/t/set-module-defaults-globally/38796>\
**Category:** Ansible Project\
**Created:** [May 28, 2024, 12:05pm UTC](https://forum.ansible.com/t/set-module-defaults-globally/38796 "2024-05-28T12:05:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [May 28, 2024, 12:05pm UTC](https://forum.ansible.com/t/set-module-defaults-globally/38796/1 "2024-05-28T12:05:51Z")

</div>

Hii

I know that it is possible to set defaults for modules: [https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_module\_defaults.html](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_module_defaults.html)

The highest level at which this can be set is play level.  
But I have many plays, so I’d have to edit all those plays and explicitly define them.  
I can use a variable for it (I think) but still it’s a lot of extra code.

Is there a way to set this globally, in some (single) configuration/file?

Thx

Dick

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 28, 2024, 5:50pm UTC](https://forum.ansible.com/t/set-module-defaults-globally/38796/2 "2024-05-28T17:50:39Z")

</div>

At that point copy the module into a custom one and set the defaults you want.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [May 29, 2024, 5:42am UTC](https://forum.ansible.com/t/set-module-defaults-globally/38796/3 "2024-05-29T05:42:37Z")

</div>

Hi Brian and Dick,

> At that point copy the module into a custom one and set the defaults  
> you want.

that doesn't sound very sustainable, especially if this is the case for  
multiple modules in multiple collections.

It might be helpful to allow module defaults to be included instead of  
having to re-specify them in every play (include\_module\_defaults:  
my-defaults.yaml).

You'll still have to add that include to all your plays then, but at  
least you only have to do it once (and even can use a linting rule to  
make sure that all your plays include that default) and have a single  
place where to fill these defaults in.

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 29, 2024, 1:44pm UTC](https://forum.ansible.com/t/set-module-defaults-globally/38796/4 "2024-05-29T13:44:17Z")

</div>

Hi all,

> \> At that point copy the module into a custom one and set  
> \> the defaults you want.
> 
> ... allow module defaults to be included instead of  
> having to re-specify them in every play  
> (include\_module\_defaults: my-defaults.yaml).

FWIW, a substitution would solve many use-cases

```yaml
- hosts: all
  module_defaults: "{{ mdefaults }}"
  ...

```

Unfortunately, it fails:

ERROR! The field 'module\_defaults' is supposed to be a  
dictionary or list of dictionaries, the keys of which must  
be static action, module, or group names. Only the values  
may contain templates. For example: {'ping': "{{  
ping\_defaults }}"}

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 29, 2024, 1:59pm UTC](https://forum.ansible.com/t/set-module-defaults-globally/38796/5 "2024-05-29T13:59:17Z")

</div>

Top level keys must be static. Quoting from ansible-core  
2.12 Release notes:  
[https://github.com/ansible/ansible/blob/stable-2.12/changelogs/CHANGELOG-v2.12.rst#breaking-changes--porting-guide](https://github.com/ansible/ansible/blob/stable-2.12/changelogs/CHANGELOG-v2.12.rst#breaking-changes--porting-guide)

Breaking Changes / Porting Guide:

\* Action, module, and group names in module\_defaults must  
&nbsp;&nbsp;be static values. Their values can still be templates.
