Docsite: update user_guide/playbooks_module_defaults (#72051) (#72493)

* Docsite: update user_guide/playbooks_module_defaults, add name: lines and use FQCN consistently
(cherry picked from commit dbe43e071e)
This commit is contained in:
Andrew Klychkov 2020-11-13 23:51:37 +03:00 committed by GitHub
parent fc4411adcd
commit 913aebb140
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,33 +9,40 @@ Here is a basic example::
- hosts: localhost - hosts: localhost
module_defaults: module_defaults:
file: ansible.builtin.file:
owner: root owner: root
group: root group: root
mode: 0755 mode: 0755
tasks: tasks:
- file: - name: Create file1
ansible.builtin.file:
state: touch state: touch
path: /tmp/file1 path: /tmp/file1
- file:
- name: Create file2
ansible.builtin.file:
state: touch state: touch
path: /tmp/file2 path: /tmp/file2
- file:
- name: Create file3
ansible.builtin.file:
state: touch state: touch
path: /tmp/file3 path: /tmp/file3
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:: 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::
- block: - block:
- debug: - name: Print a message
msg: "a different message" ansible.builtin.debug:
msg: "Different message"
module_defaults: module_defaults:
debug: ansible.builtin.debug:
msg: "a default message" msg: "Default message"
You can remove any previously established defaults for a module by specifying an empty dict:: You can remove any previously established defaults for a module by specifying an empty dict::
- file: - name: Create file1
ansible.builtin.file:
state: touch state: touch
path: /tmp/file1 path: /tmp/file1
module_defaults: module_defaults:
@ -50,16 +57,21 @@ Interacting with an API that requires auth::
- hosts: localhost - hosts: localhost
module_defaults: module_defaults:
uri: ansible.builtin.uri:
force_basic_auth: true force_basic_auth: true
user: some_user user: some_user
password: some_password password: some_password
tasks: tasks:
- uri: - name: Interact with a web service
ansible.builtin.uri:
url: http://some.api.host/v1/whatever1 url: http://some.api.host/v1/whatever1
- uri:
- name: Interact with a web service
ansible.builtin.uri:
url: http://some.api.host/v1/whatever2 url: http://some.api.host/v1/whatever2
- uri:
- name: Interact with a web service
ansible.builtin.uri:
url: http://some.api.host/v1/whatever3 url: http://some.api.host/v1/whatever3
Setting a default AWS region for specific EC2-related modules:: Setting a default AWS region for specific EC2-related modules::
@ -68,11 +80,11 @@ Setting a default AWS region for specific EC2-related modules::
vars: vars:
my_region: us-west-2 my_region: us-west-2
module_defaults: module_defaults:
ec2: amazon.aws.ec2:
region: '{{ my_region }}' region: '{{ my_region }}'
ec2_instance_info: community.aws.ec2_instance_info:
region: '{{ my_region }}' region: '{{ my_region }}'
ec2_vpc_net_info: amazon.aws.ec2_vpc_net_info:
region: '{{ my_region }}' region: '{{ my_region }}'
.. _module_defaults_groups: .. _module_defaults_groups:
@ -120,8 +132,12 @@ In a playbook, you can set module defaults for whole groups of modules, such as
group/aws: group/aws:
region: us-west-2 region: us-west-2
tasks: tasks:
- aws_s3_bucket_info: - name: Get info
aws_s3_bucket_info:
# now the region is shared between both info modules # now the region is shared between both info modules
- ec2_ami_info:
- name: Get info
ec2_ami_info:
filters: filters:
name: 'RHEL*7.5*' name: 'RHEL*7.5*'