LoginSignup
5
8

More than 5 years have passed since last update.

AnsibleでNWの疎通テストをやってみた

Posted at

概要

サーバを立てているとセグメントを跨いで通信を行うとき、新規サーバ構築後にテストしてみたら
FWがパケットを叩き落としてやばい!穴あけしなきゃ!間に合わないかも!!なんてことありませんか。
残念ながら私はあります。。。
それが1台から1台に対する通信ならまだしも、1対2、1対4みたいなサーバが5台も10台もあって対向先のサーバは1台に複数ポートとかあると、
とてもじゃないですが人の手ではチェックしきれないかと思います。
スクリプトを流す方法もあるのですが、個人的に今Ansibleを触っているので、それで実施できないかテストしてみました。

接続イメージ

今回テストしたい疎通試験はこんな感じ。
このsrchostやdsthostが増えたり、ポートが増えるとそれこそねずみ算式に増えてまさにカオスな感じ。

srchost01
 |--> dsthost01:80
 |--> dsthost01:443
 |--> dsthost02:80
 `--> dsthost02:443

srchost02
 |--> dsthost01:80
 |--> dsthost01:443
 |--> dsthost02:80
 `--> dsthost02:443

srchost03
 |--> dsthost01:80
 |--> dsthost01:443
 |--> dsthost02:80
 `--> dsthost02:443

srchost04
 |--> dsthost01:80
 |--> dsthost01:443
 |--> dsthost02:80
 `--> dsthost02:443

設定

hosts
[commtest_network]
srchost01
srchost02
srchost03
srchost04
commtest_network.yml
- hosts: commtest_network
  port: ${SSH_PORT}
  user: ${SSH_USER}
  tasks:
    - name: Communication test dsthost01:80
      shell: 'nc 192.168.1.1 80 -w1'
      ignore_errors: true

    - name: Communication test dsthost01:443
      shell: 'nc 192.168.1.1 443 -w1'
      ignore_errors: true

    - name: Communication test dsthost02:80
      shell: 'nc 192.168.1.2 80 -w1'
      ignore_errors: true

    - name: Communication test dsthost02:443
      shell: 'nc 192.168.1.2 443 -w1'
      ignore_errors: true
command
# 対象確認
ansible-playbook -i ${ANSIBLE_DIR}/hosts ${ANSIBLE_DIR}/commtest_network.yml --list-hosts --list-tasks

playbook: ${ANSIBLE_DIR}/commtest_network.yml

  play #1 (commtest_network): host count=4
    srchost01
    srchost02
    srchost03
    srchost04
  play #1 (commtest_network): TAGS: []
    Communication test dsthost01:80        TAGS: []
    Communication test dsthost01:443       TAGS: []
    Communication test dsthost02:80  TAGS: []
    Communication test dsthost02:443   TAGS: []

# 実行
ansible-playbook -i ${ANSIBLE_DIR}/hosts ${ANSIBLE_DIR}/commtest_network.yml

PLAY [commtest_network] *******************************************************

GATHERING FACTS *************************************************************** 
ok: [srchost01]
ok: [srchost02]
ok: [srchost03]
ok: [srchost04]

TASK: [Communication test dsthost01:80] ***************************************
changed: [srchost01]
changed: [srchost02]
changed: [srchost03]
changed: [srchost04]

TASK: [Communication test dsthost01:443] ***************************************
changed: [srchost01]
changed: [srchost02]
changed: [srchost03]
changed: [srchost04]

TASK: [Communication test dsthost02:80] ***************************************
changed: [srchost01]
failed: [srchost02] => {"changed": true, "cmd": "nc 192.168.1.2 80 -w1", "delta": "0:00:00.013571", "end": "2015-01-01 01:01:01.273768", "rc": 1, "start": "2015-01-01 01:01:01.260197", "warnings": []}
...ignoring
failed: [srchost03] => {"changed": true, "cmd": "nc 192.168.1.2 80 -w1", "delta": "0:00:00.014444", "end": "2015-01-01 01:01:25.613195", "rc": 1, "start": "2015-01-01 01:01:01.598751", "warnings": []}
...ignoring
changed: [srchost04]

TASK: [Communication test dsthost02:443] ***************************************
changed: [srchost01]
changed: [srchost02]
changed: [srchost03]
changed: [srchost04]

おぉ、超便利かつシンプル!
NWの疎通が出来たものは「changed」で出来なかったものは「failed」になっているみたいでした。
実際ncコマンドでタイムアウトは1を返してくれました。
これでもうNWの疎通地獄からおさらばだ!

5
8
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
5
8