前回の続き
今回は、ansible で Vagrant イメージに対して簡単な playbook を実行するところまで
Vagrant イメージの情報
- OS:CentOS7
- IP:10.0.0.5
ansible バージョン確認
$ ansible --version
ansible 2.0.0.2
config file =
configured module search path = Default w/o overrides
ansible で ping する
とりあえず、チュートリアル通りに、pingしてみる
$ ansible 10.0.0.5 -m ping
[WARNING]: provided hosts list is empty, only localhost is available
$ echo 10.0.0.5 > hosts ## ここでカレントディレクトリに hosts ファイルを作成する
$ ansible -i hosts 10.0.0.5 -m ping
The authenticity of host '10.0.0.5 (10.0.0.5)' can't be established.
ECDSA key fingerprint is SHA256:0ybr41VsIkCo7+PFfVmvBN1M5mVraiVhkGTn7yhloV4.
Are you sure you want to continue connecting (yes/no)? yes
10.0.0.5 | UNREACHABLE! => {
"changed": false,
"msg": "ERROR! SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue",
"unreachable": true
}
UNREACHABLE! って言われる。。。
~/.ssh/config
に、Vagrant イメージの SSH 接続設定が必要だそうなので、以下のように作成してみる
$ vagrant ssh-config --host 10.0.0.5 >> ~/.ssh/config
$ ansible -i hosts 10.0.0.5 -m ping
10.0.0.5 | SUCCESS => {
"changed": false,
"ping": "pong"
}
繋がった!
~/.ssh/config の内容抜粋は以下
~/.ssh/config
Host 10.0.0.5
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile "/Users/koga/hobby_src/ansibleplaybook/.vagrant/machines/default/virtualbox/private_key"
IdentitiesOnly yes
LogLevel FATAL
簡単な playbook を作成して実行してみる
簡単な playbook を作成して実行してみた。
playbook.yml
- hosts: DevServer1
become: yes
tasks:
- ping:
playbook の hosts を DevServer1 にしたので、 hosts を書き直す
hosts
[DevServer1]
10.0.0.5
$ ansible-playbook -i hosts playbook.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [10.0.0.5]
TASK [ping] ********************************************************************
ok: [10.0.0.5]
PLAY RECAP *********************************************************************
10.0.0.5 : ok=2 changed=0 unreachable=0 failed=0
成功した!
次回はいよいよ LAMP 環境構築する!