初めてのAnsible

# Ansibleとは

+ オープンソースの設定管理ツール
+ YAMLの読みやすい構文
+ リモートホストへのインストールが不要
+ プッシュベース
+ スケールダウン
+ 組み込みモジュール


# インストール

## Python

+ バージョン2.5移行

```shell-session
$ python -V
Python 2.7.10
```

## Ansible

```shell-session
$ brew install ansible
$ ansible --version
ansible 2.0.2.0
```

## テスト用サーバのセットアップ

```shell-session
$ mkdir ansible_playbooks
$ cd ansible_playbooks
$ vagrant init ubuntu/trusty64
$ vagrant up
```

# Ansibleでサーバーに接続

## 引数を明示的に指定して接続する方法

### hosts作成

```shell-session
$ vi hosts
```
```ini
testserver ansible_ssh_host=127.0.0.1 \ 
ansible_ssh_port=2222 \
ansible_ssh_user=vagrant \
ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key
```

### pingモジュールで接続確認

```shell-session
$ ansible testserver -i hosts -m ping
testserver | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
```

## ansible.cfg による接続方法

### ansible.cfg作成

```shell-session
$ vi ansible.cfg
```
```ini
[defaults]
hostfile = hosts
remote_user = vagrant
private_key_file = .vagrant/machines/default/virtualbox/private_key
host_key_checking = False
```

Ansibleは、以下の順序でansible.cfgを探す

1. 環境変数のANSIBLE_CONFIGで指定された場所
2. ./ansible.cfg
3. ~/.ansible.cfg
4. /etc/ansible/ansible.cfg


### pingモジュールで接続確認

```shell-session
$ ansible testserver -m ping
testserver | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
```

### commandモジュール

```shell-session
$ ansible testserver -m command -a uptime
testserver | SUCCESS | rc=0 >>
 01:16:21 up 27 min,  1 user,  load average: 0.00, 0.01, 0.05
```

### tail

```shell-session
$ ansible testserver -a "tail /var/log/dmesg"
testserver | SUCCESS | rc=0 >>
[    8.605835] type=1400 audit(1462582124.605:15): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/lib/connman/scripts/dhclient-script" pid=1005 comm="apparmor_parser"
[    8.605953] type=1400 audit(1462582124.605:16): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/lib/connman/scripts/dhclient-script" pid=1005 comm="apparmor_parser"
[    8.786303] type=1400 audit(1462582124.785:17): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/sbin/tcpdump" pid=1007 comm="apparmor_parser"
[    8.813174] vboxvideo: Unknown symbol drm_open (err 0)
[    8.813179] vboxvideo: Unknown symbol drm_poll (err 0)
[    8.813181] vboxvideo: Unknown symbol drm_pci_init (err 0)
[    8.813185] vboxvideo: Unknown symbol drm_ioctl (err 0)
[    8.813188] vboxvideo: Unknown symbol drm_mmap (err 0)
[    8.813190] vboxvideo: Unknown symbol drm_pci_exit (err 0)
[    8.813192] vboxvideo: Unknown symbol drm_release (err 0)
```

### sudo

```shell-session
$ ansible testserver -s -a "tail /var/log/syslog"
testserver | SUCCESS | rc=0 >>
May  7 00:48:53 vagrant-ubuntu-trusty-64 pollinate[1695]: system was previously seeded at [2016-05-07 00:48:43.913686907 +0000]
May  7 00:48:53 vagrant-ubuntu-trusty-64 pollinate[1697]: To re-seed this system again, use the -r|--reseed option
May  7 00:48:57 vagrant-ubuntu-trusty-64 ntpdate[1400]: adjust time server 91.189.89.199 offset -0.002563 sec
May  7 00:49:05 vagrant-ubuntu-trusty-64 ntpdate[1953]: step time server 91.189.89.199 offset -0.001220 sec
May  7 01:02:16 vagrant-ubuntu-trusty-64 ansible-ping: Invoked with data=None
May  7 01:06:50 vagrant-ubuntu-trusty-64 ansible-ping: Invoked with data=None
May  7 01:16:21 vagrant-ubuntu-trusty-64 ansible-command: Invoked with warn=True executable=None _uses_shell=False _raw_params=uptime removes=None creates=None chdir=None
May  7 01:17:01 vagrant-ubuntu-trusty-64 CRON[2164]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  7 01:18:17 vagrant-ubuntu-trusty-64 ansible-command: Invoked with warn=True executable=None _uses_shell=False _raw_params=tail /var/log/dmesg removes=None creates=None chdir=None
May  7 01:19:37 vagrant-ubuntu-trusty-64 ansible-command: Invoked with warn=True executable=None _uses_shell=False _raw_params=tail /var/log/syslog removes=None creates=None chdir=None
```