Ansible 2.12를 사용하여 호스트:레이블 클래스를 통해 AWS EC2에 액세스합니다.

Ansible 2.12를 사용하여 호스트:레이블 클래스를 통해 AWS EC2에 액세스합니다.

내 로컬 하드웨어에는 Ansible 2.12.2를 사용하는 Ubuntu 20을 실행하는 Vagrant 상자가 있습니다.

AWS에 액세스할 수 있었고 VPN에서 EC2 인스턴스도 생성할 수 있었습니다.

인벤토리를 보면 EC2 서버가 다음과 같이 표시됩니다.

"ec2-64-135-69-12.us-west-1.compute.amazonaws.com": {
    ...,
    "tags": {
        "Details": "File server and api",
        "Name": "File server via Ansible",
        "OS": "Ubuntu20",
        "Type": "Image Server",
        "class": "classfileserver2022"
    },
    ...
},

다음 플레이북에서는 다음을 통해 서버에 액세스할 수 있습니다.

hosts: "ec2-64-135-69-12.us-west-1.compute.amazonaws.com"

하지만 저는 위 json의 태그를 통해 액세스하고 싶습니다.

나는 열심히 노력했다

hosts: "tags_class_classfileserver2022"

그리고

hosts:
  - tags:Class="classfileserver2022"

하지만 다음과 같은 오류가 발생합니다.

[WARNING]: Could not match supplied host pattern, ignoring: tags_class_classfileserver2022
skipping: no hosts matched

카테고리 태그를 사용하여 EC2 호스트에 액세스하는 방법은 무엇입니까? (또는 다른 태그..)

내 스크립트는 다음과 같습니다.

---
  - name: "Prepare base of {{ server_name }} box"
    vars_files:
      - vars/0000_vars.yml
      - vars/vars_for_base_provision.yml
      - vars/vars_for_geerling.security.yml
#    hosts: "ec2-54-153-39-10.us-west-1.compute.amazonaws.com"   <-- this works
    hosts: "tags_Class_{{ tag_class }}"
    remote_user: ubuntu
    become: yes
    gather_facts: no

    pre_tasks:
    - name: Check for single host
      fail: msg="Single host check failed.  Try --limit or change `hosts` above."
      when: "{{ ansible_play_batch|length }} != 1"

    roles:
      - { role: geerlingguy.security }

답변1

"인벤토리 플러그인" 섹션을 읽어보세요.신뢰할 수 있는 문서.

YAML 구성 소스와 함께 매니페스트 플러그인 사용을 시작하려면 관련 플러그인에 대해 문서화된 허용 가능한 파일 이름 스키마가 포함된 파일을 생성한 다음 플러그인:플러그인_이름을 추가합니다. 플러그인이 컬렉션에 있는 경우 정규화된 이름을 사용하세요.

# demo.aws_ec2.yml
plugin: amazon.aws.aws_ec2

[...] 구성된 keyed_groups 옵션과 함께 호스트 변수를 사용하여 동적 그룹을 생성할 수 있습니다. 옵션 그룹을 사용하여 그룹을 생성하고 결합하여 호스트 변수를 생성 및 수정할 수도 있습니다. 다음은 구성 기능을 활용하는 aws_ec2 예제입니다.

# demo.aws_ec2.yml
plugin: amazon.aws.aws_ec2
regions:
  - us-east-1
  - us-east-2
keyed_groups:
  # add hosts to tag_Name_value groups for each aws_ec2 host's tags.Name variable
  - key: tags.Name
    prefix: tag_Name_
    separator: ""
groups:
  # add hosts to the group development if any of the dictionary's keys or values is the word 'devel'
  development: "'devel' in (tags|list)"
compose:
  # set the ansible_host variable to connect with the private IP address without changing the hostname
  ansible_host: private_ip_address

[...] 를 ansible-doc -t inventory -l사용하여 사용 가능한 플러그인 목록을 볼 수 있습니다. ansible-doc -t inventory <plugin name>플러그인별 문서 및 예제를 보려면

답변2

Panki의 답변을 바탕으로 이것이 문제를 해결했습니다.

# demo.aws_ec2.yml
inventory-plugins
plugin: amazon.aws.aws_ec2
regions:
  - us-west-1
keyed_groups:
  - key: tags.class    # <-- note: lowercase c
    prefix: tags_Class_
    separator: ""

플레이북 일치 태그 예시: class: Uniqueclassname

# example_playbook.yml
---
  - name: "Playbook for {{ server_name }} EC2 instance"
    vars_files:
      - vars/0000_vars.yml
    hosts: "tags_Class_{{ tag_class }}"
    remote_user: ubuntu
    become: yes
    gather_facts: no

    roles:
      - { role: xxxxxxx }

플레이북으로 생성된 변수:

# vars/0000_vars.yml
tag_class: "uniqueclassname"
server_name: "My Fancy Server"

관련 정보