set module_defaults globally?

Hii

I know that it is possible to set defaults for modules: 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

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

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

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

- 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 }}"}

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

Breaking Changes / Porting Guide:

* Action, module, and group names in module_defaults must
  be static values. Their values can still be templates.