Ansibleでネットワーク疎通確認

#環境
* RHEL 7.3
* Ansible 2.3

#スクリプト
```ini:inventory.ini
[test-servers]
ansible-target-01
ansible-target-02
#対象ホストを列挙する
```

```yaml:main.yml
# 指定したサーバ間のネットワークの疎通確認を行う
---
- hosts: test-servers

  tasks:
  - name: ncをインストール
    yum:
      name: nc
      state: present
    become: yes

  - name: ncを実行
    shell: nc {{ item.host }} {{ item.port }}
    with_items: 
      - { host: ansible-target-01, port: 22 }
      - { host: ansible-target-01, port: 80 }
      - { host: ansible-target-02, port: 22 }
      - { host: ansible-target-02, port: 80 }

  #------アンインストール必要な場合はコメントアウトを外す-------
  #- name: ncをアンインストール
  #  yum:
  #    name: nc
  #    state: absent
  #  become: yes
```

#実行

```bash:terminal
#以下のコマンドを実行する
ansible-playbook -i inventory.ini main.yml --ask-pass --ask-sudo-pass

#sshユーザを指定する場合は以下のコマンド
ansible-playbook -i inventory.ini main.yml -u <REMOTE-USER> --ask-pass --ask-sudo-pass

```
一度もSSH接続したことがないtargetは実行時に失敗する。SSH接続するか、Ansibleの設定を変更する。

#結果
結果の一部を抜粋。
疎通できた場合はchanged、できなかった場合はfailedで出力される。

```bash

TASK [ncを実行] *************************************************************************************************************************************************************************
changed: [ansible-target-02] => (item={u'host': u'ansible-target-01', u'port': 22})
changed: [ansible-target-01] => (item={u'host': u'ansible-target-01', u'port': 22})
failed: [ansible-target-02] (item={u'host': u'ansible-target-01', u'port': 80}) => {"changed": true, "cmd": "nc ansible-target-01 80", "delta": "0:00:00.035383", "end": "2017-06-14 20:15:32.066090", "failed": true, "item": {"host": "ansible-target-01", "port": 80}, "rc": 1, "start": "2017-06-14 20:15:32.030707", "stderr": "Ncat: No route to host.", "stderr_lines": ["Ncat: No route to host."], "stdout": "", "stdout_lines": []}
failed: [ansible-target-01] (item={u'host': u'ansible-target-01', u'port': 80}) => {"changed": true, "cmd": "nc ansible-target-01 80", "delta": "0:00:00.041357", "end": "2017-06-14 20:15:32.289085", "failed": true, "item": {"host": "ansible-target-01", "port": 80}, "rc": 1, "start": "2017-06-14 20:15:32.247728", "stderr": "Ncat: Connection refused.", "stderr_lines": ["Ncat: Connection refused."], "stdout": "", "stdout_lines": []}
changed: [ansible-target-02] => (item={u'host': u'ansible-target-02', u'port': 22})
changed: [ansible-target-01] => (item={u'host': u'ansible-target-02', u'port': 22})
failed: [ansible-target-02] (item={u'host': u'ansible-target-02', u'port': 80}) => {"changed": true, "cmd": "nc ansible-target-02 80", "delta": "0:00:00.040886", "end": "2017-06-14 20:15:34.765019", "failed": true, "item": {"host": "ansible-target-02", "port": 80}, "rc": 1, "start": "2017-06-14 20:15:34.724133", "stderr": "Ncat: Connection refused.", "stderr_lines": ["Ncat: Connection refused."], "stdout": "", "stdout_lines": []}
failed: [ansible-target-01] (item={u'host': u'ansible-target-02', u'port': 80}) => {"changed": true, "cmd": "nc ansible-target-02 80", "delta": "0:00:00.282982", "end": "2017-06-14 20:15:35.361255", "failed": true, "item": {"host": "ansible-target-02", "port": 80}, "rc": 1, "start": "2017-06-14 20:15:35.078273", "stderr": "Ncat: No route to host.", "stderr_lines": ["Ncat: No route to host."], "stdout": "", "stdout_lines": []}

```
#参考
* [AnsibleでNWの疎通テストをやってみた \- Qiita](http://qiita.com/st_1t/items/cad3a39aec365b7f2f5d)