+

Search Tips   |   Advanced Search

Module defaults

If you frequently call the same module with the same arguments, it can be useful to define default arguments for that particular module using the module_defaults attribute.

Here is a basic example:

The module_defaults attribute can be used at the play, block, and task level. Any module arguments explicitly specified in a task will override any established default for that module argument:

You can remove any previously established defaults for a module by specifying an empty dict:

Any module defaults set at the play level (and block/task level when using include_role or import_role) will apply to any roles used, which may cause unexpected behavior in the role.

Here are some more realistic use cases for this feature.

Interacting with an API that requires auth:

Setting a default AWS region for specific EC2-related modules:


Module defaults groups

Ansible 2.7 adds a preview-status feature to group together modules that share common sets of parameters. This makes it easier to author playbooks making heavy use of API-based modules such as cloud modules.

Group

Purpose

Ansible Version

aws Amazon Web Services 2.7
azure Azure 2.7
gcp Google Cloud Platform 2.7
k8s Kubernetes 2.8
os OpenStack 2.8
acme ACME 2.10
docker* Docker 2.10
ovirt oVirt 2.10
vmware VMware 2.10

Use the groups with module_defaults by prefixing the group name with group/ - for example group/aws.

In a playbook, you can set module defaults for whole groups of modules, such as setting a common AWS region.

Next Previous