ansible all -i inventory.ini -m pingansible all -m ping は、インベントリに書いた管理対象のすべてに、Ansible が接続できるかを確かめるコマンドです。SUCCESS と "ping": "pong" が返ってくれば、その相手には Playbook を流せる状態です。Playbook を初めて流す前や、管理するサーバーを増やしたあとに、まず打つ1行です。
ansible -m ping の書式
ansible <対象> -i <インベントリ> -m ping- 対象: どのホストに打つか。
allですべて、webのようにグループ名を書くとそのグループだけになります - インベントリ: 管理対象の一覧を書いたファイル。
-iを省くと/etc/ansible/hostsが読まれます -m ping: ping モジュールを使う、という指定です
Linux サーバーを相手にするときのインベントリは、たとえば次のように書きます。[web] がグループ名で、その下に IP アドレスかホスト名を並べます。
[web]
192.0.2.10
192.0.2.11
[db]
192.0.2.20Ansible の ping は、ネットワークの ping コマンド(ICMP)とは別物です。SSH でログインし、相手のサーバーで Python が動くところまでを確かめて、pong を返します。インベントリや Playbook の考え方は Ansibleとは?構成管理の基本で説明しています。
よく使うオプション
| オプション | 意味 |
|---|---|
-i | インベントリのファイルを指定する |
-m | 使うモジュールを指定する(ここでは ping) |
-l | 対象をさらに絞る(例 -l web) |
-u | SSH でログインするユーザー名を指定する |
-k | SSH のパスワードを聞いてもらう(鍵ではなくパスワードでログインするとき) |
ansible -m ping の実行例
macOS 27.2 上の ansible-core 2.22.0 で、自分のPC(localhost)を相手に確認しました。インベントリの中身は次のとおりです。ansible_connection=local は、SSH を使わずにこのPCの上で実行する指定です。
[web]
localhost ansible_connection=local$ ansible all -i inventory.ini -m ping
[WARNING]: Host 'localhost' is using the discovered Python interpreter at '/opt/homebrew/bin/python3.14', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/2.22/reference_appendices/interpreter_discovery.html for more information.
localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/opt/homebrew/bin/python3.14"
},
"changed": false,
"ping": "pong"
}見るところは、ホスト名の横の SUCCESS と、最後の "ping": "pong" です。"changed": false は、相手に何も変更を加えていないという意味です。先頭の WARNING は、見つけた Python の場所を知らせているだけで、エラーではありません。
よくあるエラーと対処
一番多いのは UNREACHABLE! です。SSH でつながらなかったことを表します。確かめるために、インベントリに db01 ansible_host=127.0.0.1 ansible_port=1(何も待ち受けていない1番ポート)を足して打つと、次のようになりました。
$ ansible all -i inventory.ini -m ping
[WARNING]: Host 'localhost' is using the discovered Python interpreter at '/opt/homebrew/bin/python3.14', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/2.22/reference_appendices/interpreter_discovery.html for more information.
[ERROR]: Task failed: Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 1: Connection refused
Origin: <adhoc 'ping' task>
{'action': 'ping', 'args': {}, 'timeout': 0, 'async_val': 0, 'poll': 15}
db01 | UNREACHABLE! => {
"changed": false,
"msg": "Task failed: Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 1: Connection refused",
"unreachable": true
}
localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/opt/homebrew/bin/python3.14"
},
"changed": false,
"ping": "pong"
}つながらないホストがあっても、ほかのホストの結果は出ます。msg の中の ssh のメッセージが原因の手がかりです。まず同じ相手に ssh ユーザー名@IPアドレス で手でログインできるかを試し、SSH の起動・ポート番号・ファイアウォール・ユーザー名と鍵の順に確かめます。
もう1つは、-i のファイル名を間違えたときです。エラーにはならず、警告だけ出て何も実行されません。
$ ansible all -i nosuch.ini -m ping
[WARNING]: Unable to parse /home/student/ansible/nosuch.ini as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'Unable to parse が出たら、ファイル名とカレントディレクトリを見直します。
ping コマンド・ssh との違い
| コマンド | 確かめられること |
|---|---|
ping 192.0.2.10 | ネットワークで相手に届くか(ICMP) |
ssh student@192.0.2.10 | SSH でログインできるか(1台ずつ) |
ansible all -m ping | SSH でログインでき、Python が動くか(インベントリの全台をまとめて) |
ping コマンドが通っても、SSH の設定や Python が無ければ Ansible の ping は失敗します。Ansible や Terraform をどの順番で学ぶかは、インフラエンジニアの学習ロードマップにまとめています。
よくある質問
pong が返れば Playbook も動きますか?
接続と Python の実行までは確かめられています。ただし、root 権限が要るタスクやパッケージのインストールなど、Playbook の中身によって別の理由で失敗することはあります。
Windows のサーバーにも ansible -m ping は使えますか?
Windows を相手にするときは ping ではなく ansible.windows.win_ping モジュールを使います。Ansible の公式の説明にも、Windows には win_ping を使うように書かれています。
毎回 -i を打つのを省けますか?
作業するディレクトリに ansible.cfg を置き、[defaults] の下に inventory = ./inventory.ini と書いておくと、-i を省いてもそのインベントリが読まれます。
