Ansibleをとりあえず試してみる

VirtualBoxにCentOSをとりあえずインストール
の続き

Ansibleをとりあえず試してみる。

Ansibleのインストール

MacにAsibleをインストールする。

$ brew update
$ brew install ansible

入った。

$ ansible --version
ansible 1.7.2

Ansibleを試す

今回使用するVirtualBoxIPアドレスは「192.168.56.101」

hostsのファイルを作成する。

$ vi hosts
192.168.56.101

コマンドを実行してみる。
しかし、エラーが出る。

$ ansible all -i hosts -u hoge -a "cat /etc/redhat-release"
192.168.56.101 | FAILED => 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

鍵認証にしてないので、パスワードを求めるようにしてみる。
しかし、まだエラーがでる。

$ ansible all -i hosts -u hoge -a "cat /etc/redhat-release" --ask-pass
SSH password:(パスワードを入力)
192.168.56.101 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program

OpenSSHだとエラーがでる場合があるみたいなので、paramikoを指定してみる。

【参考】
Windows(Cygwin)でAnsible動かしてみる | blog.s-uni.net
http://blog.s-uni.net/2013/08/27/ansible-running-on-cygwin/

すると、うまくいった。

$ ansible all -i hosts -u hoge -a "cat /etc/redhat-release" --ask-pass -c paramiko
SSH password:
192.168.56.101 | success | rc=0 >>
CentOS release 6.5 (Final)

ansible.cfgの設定

ansible.cfgを設定してみる。

$ vi ansible.cfg
[defaults]
hostfile = ./hosts
remote_user = hoge
ask_pass = true
transport = paramiko

オプションを色々省略できた。

$ ansible all -a "cat /etc/redhat-release"
SSH password:
192.168.56.101 | success | rc=0 >>
CentOS release 6.5 (Final)
ansible.cfgに指定できるオプションは下記を参照。

The Ansible Configuration File — Ansible Documentation
http://docs.ansible.com/intro_configuration.html

Ansibleのplaybookを試す

playbookを作成する。

$ vi sample.yml
---
- hosts: all
  tasks:
    - command: cat /etc/redhat-release

実行してみると、エラーは出てないが、
ちゃんとコマンドが実行されたのかどうかよく分からない。

$ ansible-playbook sample.yml
SSH password:

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.56.101]

TASK: [command cat /etc/redhat-release] ***************************************
changed: [192.168.56.101]

PLAY RECAP ********************************************************************
192.168.56.101             : ok=2    changed=1    unreachable=0    failed=0

vオプションを付けて、詳細を表示してみると、
コマンドが実行されてるのが確認できる。

$ ansible-playbook sample.yml -v
SSH password:

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.56.101]

TASK: [command cat /etc/redhat-release] ***************************************
changed: [192.168.56.101] => {"changed": true, "cmd": ["cat", "/etc/redhat-release"], "delta": "0:00:00.003015", "end": "2014-11-08 01:05:28.355197", "rc": 0, "start": "2014-11-08 01:05:28.352182", "stderr": "", "stdout": "CentOS release 6.5 (Final)"}

PLAY RECAP ********************************************************************
192.168.56.101             : ok=2    changed=1    unreachable=0    failed=0

sudoでplaybookを試す

下記のようなコマンドを試す場合、rootユーザーじゃないとエラーになる。

$ vi sample.yml
---
- hosts: all
  tasks:
    - command: cat /etc/sudoers
$ ansible-playbook sample.yml
SSH password:

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.56.101]

TASK: [command cat /etc/sudoers] **********************************************
failed: [192.168.56.101] => {"changed": true, "cmd": ["cat", "/etc/sudoers"], "delta": "0:00:00.004112", "end": "2014-11-08 02:33:14.813564", "rc": 1, "start": "2014-11-08 02:33:14.809452"}
stderr: cat: /etc/sudoers: Permission denied

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/Users/yk/sample.retry

192.168.56.101             : ok=1    changed=0    unreachable=0    failed=1

playbookでsudoをyesにすると、sudoで実行される。

$ vi sample.yml
---
- hosts: all
  sudo: yes		←追加
  tasks:
    - command: cat /etc/sudoers

sudoでパスワードが必要な場合は、ask_sudo_passをtrueにする。

$ vi ansible.cfg
[defaults]
hostfile = ./hosts
remote_user = hoge
ask_pass = true
ask_sudo_pass = true	←追加
transport = paramiko

これで、sudoがうまくいく。

$ ansible-playbook sample.yml
SSH password:(SSHのパスワードを入力する)
sudo password [defaults to SSH password]:(sudoのパスワードを入力する)

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.56.101]

TASK: [command cat /etc/sudoers] **********************************************
changed: [192.168.56.101]

PLAY RECAP ********************************************************************
192.168.56.101             : ok=2    changed=1    unreachable=0    failed=0