모듈 기본값

모듈 기본값 (Module defaults)

같은 모듈을 같은 인자로 자주 호출한다면, module_defaults 키워드로 그 특정 모듈의 기본 인자를 정의해 두는 게 유용해요. 이렇게 해 두면 매 작업마다 같은 인자를 반복해서 적지 않아도 되죠.

출처: 문서

본문

같은 모듈을 같은 인자로 자주 호출한다면, 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 키워드는 플레이(play), 블록(block), 작업(task) 레벨에서 사용할 수 있어요. 작업에서 명시적으로 지정한 모듈 인자는 그 모듈 인자에 대해 설정된 기본값을 덮어써요.

- 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_role이나 import_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 }}'

모듈 기본값 그룹 (Module defaults groups)

모듈 기본값 그룹을 사용하면 함께 쓰이는 모듈들에 공통 파라미터를 제공할 수 있어요. 컬렉션은 자신의 meta/runtime.yml 파일에 이런 그룹을 정의할 수 있어요.

참고: module_defaultscollections 키워드를 고려하지 않아요. 따라서 module_defaults에서 새 그룹을 쓸 때는 정규화된 그룹 이름(FQGN)을 사용해야 해요.

다음은 ns.coll 컬렉션의 runtime.yml 파일 예시예요. 이 파일은 ns.coll.my_group이라는 액션 그룹을 정의하고, ns.collsample_moduleanother.collectionanother_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:

역사적 이유와 하위 호환성을 위해 몇 가지 특별한 그룹이 있어요:

그룹 확장 모듈 그룹
aws amazon.aws.aws 및 community.aws.aws
azure azure.azcollection.azure
gcp google.cloud.gcp
k8s community.kubernetes.k8s, community.general.k8s, community.kubevirt.k8s, community.okd.k8s 및 kubernetes.core.k8s
os openstack.cloud.os
acme community.crypto.acme
docker* community.general.docker 및 community.docker.docker
ovirt ovirt.ovirt.ovirt 및 community.general.ovirt
vmware community.vmware.vmware
  • 어떤 액션 플러그인과 모듈이 그 그룹에 포함되는지 보려면 컬렉션 문서나 그 meta/runtime.yml을 확인하세요.

module_defaults에서 그룹을 사용하려면 그룹 이름 앞에 group/을 붙이세요. 예를 들어 group/aws처럼요.

플레이북에서는 모듈 전체 그룹에 대해 모듈 기본값을 설정할 수 있어요. 예를 들어 공통 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*'

meta/runtime.ymlaction_groups의 전체 형식에 대한 자세한 내용은 runtime.yml에서 확인할 수 있어요.

더 알아보기 (Learn more)