2019-01-18 18:13:28 +01:00
.. _module_defaults:
2018-05-25 10:15:44 +02:00
Module defaults
===============
2020-10-14 19:16:30 +02:00
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 `` keyword.
2018-05-25 10:15:44 +02:00
Here is a basic example::
- hosts: localhost
module_defaults:
2020-11-04 16:58:47 +01:00
ansible.builtin.file:
2018-05-25 10:15:44 +02:00
owner: root
group: root
mode: 0755
tasks:
2020-11-04 16:58:47 +01:00
- name: Create file1
ansible.builtin.file:
2018-05-25 10:15:44 +02:00
state: touch
path: /tmp/file1
2020-11-04 16:58:47 +01:00
- name: Create file2
ansible.builtin.file:
2018-05-25 10:15:44 +02:00
state: touch
path: /tmp/file2
2020-11-04 16:58:47 +01:00
- name: Create file3
ansible.builtin.file:
2018-05-25 10:15:44 +02:00
state: touch
path: /tmp/file3
2020-10-14 19:16:30 +02:00
The `` module_defaults `` keyword 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::
2018-05-25 10:15:44 +02:00
- block:
2020-11-04 16:58:47 +01:00
- name: Print a message
ansible.builtin.debug:
msg: "Different message"
2018-05-25 10:15:44 +02:00
module_defaults:
2020-11-04 16:58:47 +01:00
ansible.builtin.debug:
msg: "Default message"
2018-05-25 10:15:44 +02:00
2020-05-15 22:18:07 +02:00
You can remove any previously established defaults for a module by specifying an empty dict::
2018-05-25 10:15:44 +02:00
2020-11-04 16:58:47 +01:00
- name: Create file1
ansible.builtin.file:
2018-05-25 10:15:44 +02:00
state: touch
path: /tmp/file1
module_defaults:
file: {}
.. note ::
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::
- hosts: localhost
module_defaults:
2020-11-04 16:58:47 +01:00
ansible.builtin.uri:
2018-05-25 10:15:44 +02:00
force_basic_auth: true
user: some_user
password: some_password
tasks:
2020-11-04 16:58:47 +01:00
- name: Interact with a web service
ansible.builtin.uri:
2018-05-25 10:15:44 +02:00
url: http://some.api.host/v1/whatever1
2020-11-04 16:58:47 +01:00
- name: Interact with a web service
ansible.builtin.uri:
2018-05-25 10:15:44 +02:00
url: http://some.api.host/v1/whatever2
2020-11-04 16:58:47 +01:00
- name: Interact with a web service
ansible.builtin.uri:
2018-05-25 10:15:44 +02:00
url: http://some.api.host/v1/whatever3
Setting a default AWS region for specific EC2-related modules::
- hosts: localhost
vars:
my_region: us-west-2
module_defaults:
2020-11-04 16:58:47 +01:00
amazon.aws.ec2:
2018-05-25 10:15:44 +02:00
region: '{{ my_region }}'
2020-11-04 16:58:47 +01:00
community.aws.ec2_instance_info:
2018-05-25 10:15:44 +02:00
region: '{{ my_region }}'
2020-11-04 16:58:47 +01:00
amazon.aws.ec2_vpc_net_info:
2018-05-25 10:15:44 +02:00
region: '{{ my_region }}'
2019-01-18 18:13:28 +01:00
.. _module_defaults_groups:
Module defaults groups
2019-01-29 21:23:27 +01:00
----------------------
2019-01-18 18:13:28 +01:00
.. versionadded :: 2.7
2020-05-15 22:18:07 +02:00
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.
2019-01-29 21:23:27 +01:00
2019-10-09 21:12:02 +02:00
+---------+---------------------------+-----------------+
| 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 |
+---------+---------------------------+-----------------+
2019-11-05 08:00:41 +01:00
| ovirt | oVirt | 2.10 |
+---------+---------------------------+-----------------+
2019-10-09 21:12:02 +02:00
| vmware | VMware | 2.10 |
+---------+---------------------------+-----------------+
* The `docker_stack <docker_stack_module> `_ module is not included in the `` docker `` defaults group.
2020-09-03 21:47:57 +02:00
Use the groups with `` module_defaults `` by prefixing the group name with `` group/ `` - for example `` group/aws `` .
2019-01-18 18:13:28 +01:00
In a playbook, you can set module defaults for whole groups of modules, such as setting a common AWS region.
.. code-block :: YAML
# example_play.yml
- hosts: localhost
module_defaults:
group/aws:
region: us-west-2
tasks:
2020-11-04 16:58:47 +01:00
- name: Get info
aws_s3_bucket_info:
2019-08-13 14:01:37 +02:00
# now the region is shared between both info modules
2020-11-04 16:58:47 +01:00
- name: Get info
ec2_ami_info:
2019-01-18 18:13:28 +01:00
filters:
name: 'RHEL*7.5* '