模块默认值

如果您经常使用相同的参数调用相同的模块,则可以使用 module_defaults 关键字为该特定模块定义默认参数。

这是一个基本示例

- hosts: localhost
  module_defaults:
    ansible.builtin.file:
      owner: root
      group: root
      mode: 0755
  tasks:
    - name: Create file1
      ansible.builtin.file:
        state: touch
        path: /tmp/file1

    - name: Create file2
      ansible.builtin.file:
        state: touch
        path: /tmp/file2

    - name: Create file3
      ansible.builtin.file:
        state: touch
        path: /tmp/file3

module_defaults 关键字可以在剧本、块和任务级别使用。在任务中明确指定的任何模块参数将覆盖该模块参数的任何已建立的默认值。

- block:
    - name: Print a message
      ansible.builtin.debug:
        msg: "Different message"
  module_defaults:
    ansible.builtin.debug:
      msg: "Default message"

您可以通过指定一个空字典来删除模块的任何先前建立的默认值。

- name: Create file1
  ansible.builtin.file:
    state: touch
    path: /tmp/file1
  module_defaults:
    file: {}

注意

在剧本级别(以及在使用 include_roleimport_role 时在块/任务级别)设置的任何模块默认值将应用于使用的任何角色,这可能会导致角色中的意外行为。

以下是此功能的一些更现实的用例。

与需要身份验证的 API 交互。

- hosts: localhost
  module_defaults:
    ansible.builtin.uri:
      force_basic_auth: true
      user: some_user
      password: some_password
  tasks:
    - name: Interact with a web service
      ansible.builtin.uri:
        url: http://some.api.host/v1/whatever1

    - name: Interact with a web service
      ansible.builtin.uri:
        url: http://some.api.host/v1/whatever2

    - name: Interact with a web service
      ansible.builtin.uri:
        url: http://some.api.host/v1/whatever3

为特定与 EC2 相关的模块设置默认 AWS 区域。

- hosts: localhost
  vars:
    my_region: us-west-2
  module_defaults:
    amazon.aws.ec2:
      region: '{{ my_region }}'
    community.aws.ec2_instance_info:
      region: '{{ my_region }}'
    amazon.aws.ec2_vpc_net_info:
      region: '{{ my_region }}'

模块默认值组

版本 2.7 中的新功能。

Ansible 2.7 添加了一个预览状态功能来将共享常见参数集的模块分组在一起。这使得使用 API 模块(如云模块)的剧本更容易编写。

目的

Ansible 版本

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

通过在组名前面添加 group/(例如 group/aws)来使用 module_defaults 中的组。

在剧本中,您可以为整个模块组设置模块默认值,例如设置一个通用的 AWS 区域。

# example_play.yml
- hosts: localhost
  module_defaults:
    group/aws:
      region: us-west-2
  tasks:
  - name: Get info
    aws_s3_bucket_info:

  # now the region is shared between both info modules

  - name: Get info
    ec2_ami_info:
      filters:
        name: 'RHEL*7.5*'

在 ansible-core 2.12 中,集合可以在 meta/runtime.yml 文件中定义自己的组。module_defaults 不会考虑 collections 关键字,因此必须为 module_defaults 中的新组使用完全限定的组名。

以下是 ns.coll 集合的 runtime.yml 文件示例。此文件定义了一个名为 ns.coll.my_group 的操作组,并将 ns.coll 中的 sample_moduleanother.collection 中的 another_module 放入该组。

# collections/ansible_collections/ns/coll/meta/runtime.yml
action_groups:
  my_group:
    - sample_module
    - another.collection.another_module

现在可以在剧本中像这样使用该组

- hosts: localhost
  module_defaults:
    group/ns.coll.my_group:
      option_name: option_value
  tasks:
    - ns.coll.sample_module:
    - another.collection.another_module: