0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Ansible】Dynamic InventoryでEC2の構成管理を行う

0
Last updated at Posted at 2026-04-08

はじめに

Ansibleでは以下のようにインベントリファイルに自動化対象のサーバを定義します。

[webservers]
web-server01 ansible_host=192.168.0.10 ansible_connection=ssh ansible_user=ec2-user
web-server02 ansible_host=192.168.0.11 ansible_connection=ssh ansible_user=ec2-user

[dbservers]
db-server01 ansible_host=192.168.1.100 ansible_connection=ssh ansible_user=ec2-user
db-server02 ansible_host=192.168.1.101 ansible_connection=ssh ansible_user=ec2-user

見てわかる通り、IPアドレスを直で指定しているため、変更が発生したら手動で書き換える必要があります。
こちらの対策として、Dynamic Inventoryを使用することで手動での書き換えが不要となるようなので、検証を実施していきます。

今回はAWSのEC2をターゲットとして操作するため、aws_ec2 dynamic inventory pluginを利用します。

検証

環境準備

まずは、コントロールノード側でaws_ec2 dynamic inventoryを利用できるように以下を実施します。

  • Pythonライブラリのインストール(botocore, boto3
  • amazon.awsコレクションのインストール
pip install botocore boto3
ansible-galaxy collection install amazon.aws

続いて認証情報の設定を行います。
いくつかやり方はありますが、今回はAWS CLIをインストールし、Profileをセットアップしました。下記の公式Docを参考に実施しましょう。

インベントリファイルの作成

ファイル名の末尾が aws_ec2.(yml | yaml) となる形でインベントリファイルを作成します。
許可されているパラメータは以下に記載されています。

今回はprod.aws_ec2.ymlとして以下のように作成しました。

prod.aws_ec2.yml
---
# プラグイン利用の宣言
plugin: amazon.aws.aws_ec2

# 利用するaws_profile (環境変数に設定されていない場合はdevを使用する)
aws_profile: "{{ lookup('env', 'AWS_PROFILE') | default('dev', true) }}"

# 対象とするリージョンを設定
# 空の場合は制限付きリージョンを除くすべてのリージョンが対象となる
regions:
  - ap-northeast-1

# ホスト名の表示形式 (デフォルトでは最初の一致のみを返す)
hostnames:
  - 'tag:Name'
  - private-ip-address

# インベントリに含めるインスタンスをフィルター
filters:
  tag:Environment: Prod
  instance-state-name: running

# 動的グループを作成
# createdBy_value, type_value の2グループを自動で作成
keyed_groups:
  - key: tags.CreatedBy
    prefix: createdby
  - key: instance_type
    prefix: type

# jinja2でホスト変数を作成/変更
compose:
  ansible_host: private_ip_address
  ansible_user: '"ec2-user"'
  ansible_ssh_private_key_file: '"./ansible.pem"'

ファイルの作成が完了したら、インベントリの状況を確認します。
ansible-inventory -i <インベントリファイル> --graphを実行することで、グループやホストを確認することができます。

$ ansible-inventory --graph -i inventory/prod.aws_ec2.yml 

@all:
  |--@ungrouped:
  |--@aws_ec2:
  |  |--webserver01
  |--@createdby_terraform:
  |  |--webserver01
  |--@type_t3_micro:
  |  |--webserver01

aws_ec2というグループは自動で生成されていますが、他2つはkeyed_groupsで指定した内容が自動で作成されています。

Playbookの実行

Dynamic Inventoryを使用してPlaybookを実行してみます。
Playbookの方は通常の記載方法と大差ありません。
hostsの部分で動的グループを指定して実行できるのがポイントです。

---
- name: Test Play
  hosts: type_t3_micro
  tasks:
    - name: Debug
      ansible.builtin.debug:
        msg: Hello, Ansible!

まとめ

今回はaws_ec2 dynamic inventory pluginを利用したPlaybookの実行を行いました。
インベントリーの管理は地味に面倒なのでだいぶ楽になるなと感じました。
それと同時に、予期しないホストでのPlaybook実行といった事故も増えそうなので、タグの命名規則など厳密に決めておく必要がありそうです。
Dynami InventoryはAWS EC2以外でも利用できるようなので、また時間があるときに試してみようと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?