리눅스
[draft] Ansible 동적 인벤토리 설정하는 방법(aws_es2 plugin)
SangChul Dot Kr Blog
2026. 2. 28. 14:59
Ansible 동적 인벤토리 설정하는 방법(aws_es2 plugin)
Ansible 동적 인벤토리 설정 (AWS EC2 Plugin)
Ansible은 동적 인벤토리(Dynamic Inventory) 를 사용하여 AWS EC2 인스턴스를 자동으로 검색하고, 태그·리전·상태 기준으로 인벤토리를 동적으로 생성할 수 있습니다.
1. 사전 요구사항 (Requirements)
- pthon >= 3.6
- boto3 >= 1.16.0
- botocore >= 1.19.0
- AWS API 접근 가능한 Credential 설정
2. boto3 설치
Ubuntu/Debian 계열
sudo apt install -y python3-boto3
설치 확인
python3 -c "import boto3; print(boto3.__version__)"
3. AWS EC2 인벤토리 플러그인 설치
Ansible 2.10+부터 AWS 플러그인은 Collection 방식으로 제공됩니다.
ansible-galaxy collection install amazon.aws
설치 확인
$ ansible-galaxy collection list | grep amazon.aws
amazon.aws 2.3.0
amazon.aws 4.1.0
4. ansible.cfg 설정
동적 인벤토리를 기본 Inventory로 지정합니다.
vim ansible.cfg
[defaults]
inventory = my_aws_ec2.yaml
5. aws_ec2 인벤토리 플러그인 문서 확인
공식 옵션 및 사용법은 다음 명령으로 확인할 수 있습니다.
ansible-doc -t inventory aws_ec2
$ ansible-doc -t inventory aws_ec2
> AMAZON.AWS.AWS_EC2 (/home/vagrant/.ansible/collections/ansible_collections/amazon/aws/plugins/inventory/aws_ec2.py)
Get inventory hosts from Amazon Web Services EC2. Uses a YAML configuration file that ends with `aws_ec2.{yml|yaml}'.
OPTIONS (= is mandatory):
- aws_access_key
The AWS access key to use.
(Aliases: aws_access_key_id)[Default: (null)]
set_via:
env:
- name: EC2_ACCESS_KEY
- name: AWS_ACCESS_KEY
- name: AWS_ACCESS_KEY_ID
type: str
- aws_profile
The AWS profile
(Aliases: boto_profile)[Default: (null)]
set_via:
env:
- name: AWS_DEFAULT_PROFILE
- name: AWS_PROFILE
type: str
- aws_secret_key
The AWS secret key that corresponds to the access key.
(Aliases: aws_secret_access_key)[Default: (null)]
set_via:
env:
- name: EC2_SECRET_KEY
- name: AWS_SECRET_KEY
- name: AWS_SECRET_ACCESS_KEY
type: str
- aws_security_token
The AWS security token if using temporary access and secret keys.
[Default: (null)]
set_via:
env:
- name: EC2_SECURITY_TOKEN
- name: AWS_SESSION_TOKEN
- name: AWS_SECURITY_TOKEN
type: str
6. AWS 인증 방식
권장 방식: AWS Profile 사용
aws configure --profile default
7. 동적 인벤토리 파일 작성
파일명 규칙 중요
반드시 aws_ec2.yaml 또는 aws_ec2.yml 형식
사용 예: my_aws_ec2.yaml (권장)
vim my_aws_ec2.yaml
EC2 태그 기반 인벤토리
plugin: aws_ec2
boto_profile: default ###aws configure profile : default
region:
- ap-northeast-2
filters:
# tag:Name: MyInstanceA
tag:Name:
- MyInstanceA
- MyInstanceB
tag:Environment: terraform
8. 동적 인벤토리 검증
그룹 트리 출력
ansible-inventory -i my_aws_ec2.yaml --graph
$ ansible-inventory -i my_aws_ec2.yaml --graph
@all:
|--@aws_ec2:
| |--ec2-3-18-215-225.ap-northeast-2.compute.amazonaws.com
| |--ec2-3-39-242-94.ap-northeast-2.compute.amazonaws.com
| |--ip-172-31-59-60.ap-northeast-2.compute.internal
| |--ip-172-31-61-183.ap-northeast-2.compute.internal
|--@ungrouped:
전체 JSON 구조 확인
ansible-inventory -i my_aws_ec2.yaml --list
참고URL
- https://docs.ansible.com/ansible/latest/collections/amazon/aws/aws_ec2_inventory.html