LoginSignup
1
0

More than 3 years have passed since last update.

ansibleで実行できた!と実感できるまで

Last updated at Posted at 2020-02-26

ansibleを利用して実行環境を設定

Ansibleを使えた!と思うまでかなり時間がかかったので、環境構築を共有したいと思います。
概要
1.コントロールノードと、ターゲットノードの作成
2.コントロールノードにAnsibleのインストール
3.インベントリファイル作成
4.アドホックコマンドで疎通確認

asnible簡単な実行環境設定

ec2-userでsshログイン

macbook作業
$ cd ~/.ssh
$ ssh -i "ansible-key.pem" ec2-user@13.114.50.230

ansibleのインストール

ec2-user/home
$ sudo yum update
$ sudo yum -y install python-devel openssl-devel gcc git
$ sudo easy_install pip
$ sudo pip install ansible
$ ansible --version

コントロールサーバの.sshディレクトリ内のauthorized_keysの権限を変更

ec2-user/home/
$ $ chmod 600 ~/.ssh/authorized_keys

EC2を作成時に利用するキーを指定し、秘密鍵がパソコンにあると思うので、macからコントロールノードにsshキーを渡す。

macbook作業
$ scp -i ~/.ssh/ansible-key.pem ~/.ssh/ansible-key.pem <user名>@<host名>:/home/ec2-user/.ssh/

コントロールノードにログイン後リモートノードにssh接続ができるか確認

ec2-user/home/.ssh
$ ssh -i "ansible-key.pem" ec2-user@13.115.139.232

作業ディレクトリの作成 ※ここでansibleコマンドは実行します。

ec2-user/home
$ mkdir ~/ansible/inventory
$ cd ~/ansible/inventory

インベントリファイルのhostsを編集

ec2-user/home/ansible/inventory
$ sudo vim hosts  #インベントリファイルの作成
hostsの中身
[client_node]
Clinet01 ansible_ssh_host=54.250.190.241 #targetノードのIPアドレス

[client_node:vars]
ansible_ssh_user=ec2-user
ansible_ssh_private_key_file=/home/ec2-user/.ssh/ansible-key.pem

アドホックコマンドのpingを利用して疎通確認。

アドホックコマンドは、めったに繰り返さないタスクに最適です。たとえば、クリスマス休暇のためにラボのすべてのマシンの電源をオフにしたい場合、プレイブックを作成せずにAnsibleで簡単なワンライナーを実行できます。アドホックコマンドは次のようになります。
$ ansible [pattern] -m [module] -a "[module options]"

[pattern]にはどのフォルダのどのファイルのどのグループを指定するかを書く
```:ec2-user/home/ansible
$ ansible -i invenrory//hosts client_node -m ping
[WARNING]: Platform linux on host Clinet01 is using the discovered Python
interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.8/referen
ce_appendices/interpreter_discovery.html for more information.

Clinet01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
```
これでコントロールノードから、ターゲットノードにつながりました。

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