LoginSignup
1
1

More than 3 years have passed since last update.

EC2インスタンスをAnsibleで操作するための事前準備

Posted at

目的

本投稿では、AWSインスタンス(EC2)に対して、Ansibleでリモート操作するための事前準備について解説します。

構成

Ansible Client (Cloud9) -> Ansible Target(EC2 インスタンス)

  • Ansibleインストール対象となるインスタンスとして、Cloud9を今回は利用しましたが、通常のEC2インスタンスでも構いません。
  • Ansible ClientとAnsible Targetは、ネットワーク疎通が取れる状態にする必要があります。

設定手順

STEP1. Ansibleのインストール

以下コマンドでcloud9インスタンスに対して、ansibleのインストールを行います。

sudo su -
yum install ansible -y
cd /etc/ansible/

最新バージョンである2.6.20がインストールされていることを確認します。

ansible --version
ansible 2.6.20
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.6.9 (unknown, Nov  2 2017, 19:21:21) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

STEP2. 秘密鍵(pemファイル)の格納

Ansible Clinetは、Ansible Target(EC2インスタンス)に対して、pemファイルを用いたssh認証を行います。
したがって、Ansible Targetと紐づく秘密鍵(pemファイル)を任意のディレクトリに格納します。
今回のケースでは、/etc/ansible/pemfiletokyo01.pemを格納しました。

STEP3. hostsファイルの設定

/etc/ansible/hostsにAnsible Targetの接続情報を記載します。

# targethost: Elastic Stackをインストール対象となるホストのIPアドレス情報一覧
[targethost]
172.16.1.109

# targethost_vars: Ansible実行時に利用する変数一覧
[targethost:vars]
ansible_ssh_port=22
ansible_ssh_user=ec2-user
ansible_ssh_private_key_file=/etc/ansible/pemfile/tokyo01.pem
ansible_become=yes
host_key_checking=False

確認手順

pingモジュールを用いて、Ansible Targetに対して接続できていることを確認します。
以下のようにSUCCESSと表示されれば問題なく接続することが確認されます。

ansible targethost -m ping
172.16.1.109 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

1
1
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
1
1