晴耕雨読

working in the fields on fine days and reading books on rainy days

Ansibleのpingモジュールを使う

Ansibleでホストとの接続確認をするには、組み込みの ansible.builtin.ping モジュールを使用します。

ansible.builtin.ping の使用例

Ansible Playbookでpingする場合は以下のように書きます。

---
- hosts: servers
  tasks:
  - name: Ping Connection
    ansible.builtin.ping:

実行結果:

$ ansible-playbook -i inventory.ini sample-playbook.yml 
PLAY [servers] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [aws-rhel]
TASK [Ping Connection] *********************************************************
ok: [aws-rhel]
PLAY RECAP *********************************************************************
aws-rhel                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

Playbookを使わないで直接呼び出す場合

pingモジュールを直接呼び出して使用することもできます。

$ ansible -i inventory.ini servers -m ping
aws-rhel | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

以上です。

参考文献