dehio3’s diary

仕事、生活、趣味のメモ

ansible dynamic inventory ec2

f:id:dehio3:20190711140040p:plain

目的

  • aws上に作成したec2に対して、全台に一括でコマンド操作を行う。
  • 動的インベントリを利用する事で、ec2の情報を常に最新の情報取得する。

Working With Dynamic Inventory — Ansible Documentation

インベントリ用ディレクト作成

mkdir ec2_command ; cd ec2_command
mkdir hosts ; cd hosts

動的インベントリ用ファイル取得

wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini

ec2.ini 修正

  • 利用しているリージョンに書き換える
regions = all
regions_exclude = ap-northeast-1

ec2リスト取得

$ ./ec2.py --list
ERROR: "Authentication error retrieving ec2 inventory.
 - No AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY environment vars found
 - Boto configs found at '~/.aws/credentials', but the credentials contained may not be correct", while: getting EC2 instances

credentialsファイルは存在し正しく設定してるのにこける。

どうやら時刻のずれが原因のよう。

https://blog.ozacc.com/posts/6e87ed9bblog.ozacc.com

確かに時間ずれてる&ntpd動いてない。

$ /etc/init.d/ntpd status
ntpd は停止しています
$ chkconfig | grep ntp
ntpd            0:off   1:off   2:off   3:off   4:off   5:off   6:off
ntpdate         0:off   1:off   2:off   3:off   4:off   5:off   6:off

時刻を合わせる

$ sudo /etc/init.d/ntpd start
ntpd を起動中:                                             [  OK  ]

再度実行

$ ./ec2.py --list