Skip to content

Commit 0072e5c

Browse files
committed
Updated 'src/usage/playbook/using/mod_defaults.md'.
1 parent 70672de commit 0072e5c

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
- hosts: db
2+
gather_facts: no
3+
4+
module_defaults:
5+
ansible.builtin.file:
6+
owner: hector
7+
group: hector
8+
mode: 0700
9+
10+
tasks:
11+
- name: Create file1
12+
ansible.builtin.file:
13+
state: touch
14+
path: /tmp/file1
15+
module_defaults:
16+
file: {}
17+
18+
- name: Create file2
19+
ansible.builtin.file:
20+
state: touch
21+
path: /tmp/file2
22+
23+
- name: Create file3
24+
ansible.builtin.file:
25+
state: touch
26+
path: /tmp/file3

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
- [使用变量](usage/playbook/using/vars.md)
7474
- [重用 Ansible 工件](usage/playbook/using/reuse.md)
7575
- [角色](usage/playbook/using/roles.md)
76+
- [模组默认值](usage/playbook/using/mod_defaults.md)
7677
- [交互式输入:提示符](usage/playbook/using/prompts.md)
7778
- [使用变量](usage/playbook/using/vars.md)
7879
- [发现变量:事实与魔法变量](usage/playbook/using/facts_and_magic_vars.md)
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# 模组默认值
2+
3+
若咱们会频繁使用相同参数,调用同一模组,那么使用 `module_defaults` 关键字,为特定模组定义默认参数,就可能会很有用。
4+
5+
下面是个基本示例:
6+
7+
8+
```yaml
9+
- hosts: localhost
10+
module_defaults:
11+
ansible.builtin.file:
12+
owner: root
13+
group: root
14+
mode: 0755
15+
tasks:
16+
- name: Create file1
17+
ansible.builtin.file:
18+
state: touch
19+
path: /tmp/file1
20+
21+
- name: Create file2
22+
ansible.builtin.file:
23+
state: touch
24+
path: /tmp/file2
25+
26+
- name: Create file3
27+
ansible.builtin.file:
28+
state: touch
29+
path: /tmp/file3
30+
```
31+
32+
`module_defaults` 关键字可用在 play、区块即任务级别。某个任务中明确指定出了的任何模组参数,都将覆盖该模组参数的任何既定默认值。
33+
34+
35+
```yaml
36+
- block:
37+
- name: Print a message
38+
ansible.builtin.debug:
39+
msg: "Different message"
40+
41+
module_defaults:
42+
ansible.builtin.debug:
43+
msg: "Default message"
44+
45+
```
46+
47+
> **译注**:区块的模组默认值,应写在最后?实验发现若把其中的 `module_defaults` 小节,移至任务前,将报出错误。
48+
49+
50+
咱们可通过指定一个空的字典,移除先前为某个模组设置的默认值。
51+
52+
```yaml
53+
- name: Create file1
54+
ansible.builtin.file:
55+
state: touch
56+
path: /tmp/file1
57+
module_defaults:
58+
file: {}
59+
```
60+
61+
> **注意**:在 play 级别(以及使用 `include_role` 或 `import_role` 时在块/任务级别),设置的任何模组默认值,都将应用于所用到的全部角色,这可能会导致角色中的未预期行为。
62+
63+
64+
下面是该特性的一些更现实的用例。
65+
66+
67+
与某个需要认证的 API 交互。
68+
69+
```yaml
70+
- hosts: localhost
71+
72+
module_defaults:
73+
ansible.builtin.uri:
74+
force_basic_auth: true
75+
user: some_user
76+
password: some_password
77+
78+
tasks:
79+
- name: Interact with a web service
80+
ansible.builtin.uri:
81+
url: http://some.api.host/v1/whatever1
82+
83+
- name: Interact with a web service
84+
ansible.builtin.uri:
85+
url: http://some.api.host/v1/whatever2
86+
87+
- name: Interact with a web service
88+
ansible.builtin.uri:
89+
url: http://some.api.host/v1/whatever3
90+
```
91+
92+
为一些特定 EC2 相关模组,设置默认 AWS 区域。
93+
94+
```yaml
95+
- hosts: localhost
96+
vars:
97+
my_region: us-west-2
98+
module_defaults:
99+
amazon.aws.ec2:
100+
region: '{{ my_region }}'
101+
community.aws.ec2_instance_info:
102+
region: '{{ my_region }}'
103+
amazon.aws.ec2_vpc_net_info:
104+
region: '{{ my_region }}'
105+
```
106+
107+
## 模组默认值的分组
108+
109+
模组的默认组别,允许为属于一组的模组,提供共用参数。专辑可在其 `meta/runtime.yml` 文件中定义一些这样的组别。
110+
111+
> **注意**:`module_defaults` 不会考虑 `collections` 关键字,因此在 `module_defaults` 中新建组时,必须使用完全限定的组名,the fully qualified group name,FQGN。
112+
113+
114+
下面是专辑 `ns.coll` 的 `runtime.yml` 文件示例。该文件定义了个名为 `ns.coll.my_group` 的操作组,并放置了 `ns.coll` 中的 `sample_module` 和 `another.collection` 中的 `another_module`。
115+
116+
117+
```yaml
118+
# collections/ansible_collections/ns/coll/meta/runtime.yml
119+
action_groups:
120+
my_group:
121+
- sample_module
122+
- another.collection.another_module
123+
```
124+
125+
在某个 playbook 中,这个分组现在可以这样使用:
126+
127+
```yaml
128+
- hosts: localhost
129+
module_defaults:
130+
group/ns.coll.my_group:
131+
option_name: option_value
132+
133+
tasks:
134+
- ns.coll.sample_module:
135+
- another.collection.another_module:
136+
```
137+
138+
139+
出于历史原因和向后兼容性,有一些特殊组别:
140+
141+
142+
| 组别 | 扩展出的模组分组 |
143+
| :-- | :-- |
144+
| `aws` | `amazon.aws.aws` 与 `community.aws.aws` |
145+
| `azure` | `azure.azcollection.azure` |
146+
| `gcp` | `google.cloud.gcp` |
147+
| `k8s` | `community.kubernetes.k8s`、`community.general.k8s`、`community.kubevirt.k8s`、`community.okd.k8s` 与 `kubernetes.core.k8s` |
148+
| `os` | `openstack.cloud.os` |
149+
| `acme` | `community.crypto.acme` |
150+
| `docker*` | `community.general.docker` 与 `community.docker.docker` |
151+
| `ovirt` | `ovirt.ovirt.ovirt` 与 `community.general.ovirt` |
152+
| `vmware` | `community.vmware.vmware` |
153+
154+
- 请查看该专辑的文档,或其 `meta/runtime.yml`,以了解该组中包含了哪些操作插件和模组。
155+
156+
要通过在组名称前添加 `group/` 前缀,与 `module_defaults` 使用这些组别 - 例如 `group/aws`。
157+
158+
159+
在某个 playbook 中,咱们可以为整组的模组,设置一些模组默认值,例如设置一个共用的 AWS 区域。
160+
161+
162+
```yaml
163+
# example_play.yml
164+
- hosts: localhost
165+
module_defaults:
166+
group/aws:
167+
region: us-west-2
168+
169+
tasks:
170+
- name: Get info
171+
aws_s3_bucket_info:
172+
173+
# now the region is shared between both info modules
174+
175+
- name: Get info
176+
ec2_ami_info:
177+
filters:
178+
name: 'RHEL*7.5*'
179+
```
180+
181+
有关 `meta/runtime.yml` 的更多信息,包括 `action_groups` 的完整格式,请参阅 [`runtime.yml`](https://docs.ansible.com/ansible/latest/dev_guide/developing_collections_structure.html#meta-runtime-yml)。
182+
183+
184+
(End)
185+
186+

0 commit comments

Comments
 (0)