📜  具有不同端口的ansible ping (1)

📅  最后修改于: 2023-12-03 14:50:06.735000             🧑  作者: Mango

Ansible Ping with Different Ports

Introduction

Ansible is an open-source automation tool widely used in IT infrastructure provisioning, configuration management, and deployment. The Ansible ping module allows you to test connectivity to a host and verify if it's accessible via SSH. By default, Ansible uses SSH protocol with port 22 for connecting to remote hosts. However, sometimes you may need to communicate with hosts using different SSH ports.

In this guide, we will explore how to use Ansible to ping hosts with different SSH ports. We will also discuss the steps to configure and execute the Ansible ping module with custom ports.

Prerequisites

Before proceeding, ensure that you have the following prerequisites in place:

  • Ansible installed and configured on the control node.
  • Access to remote hosts for SSH connectivity.
Steps to Ping Hosts with Different Ports
1. Inventory Configuration

To specify different SSH ports for hosts, modify the Ansible inventory file (usually located at /etc/ansible/hosts).

[hosts]
host1 ansible_host=192.168.1.100 ansible_port=2222
host2 ansible_host=192.168.1.101 ansible_port=2222
host3 ansible_host=192.168.1.102 ansible_port=2222

In the above example, we have defined three hosts with their respective IP addresses and SSH ports.

2. Ansible Ping Configuration

Create a playbook to execute the Ansible ping module and test connectivity to hosts with different ports. You can save the playbook with a .yml extension, for example, ping.yml.

---
- hosts: hosts
  gather_facts: false
  tasks:
    - name: Ping hosts
      ping:

In the above playbook, we use the hosts directive to specify the hosts defined in the inventory. The ping module is used to test connectivity.

3. Execution

To execute the playbook and ping the hosts with custom ports, use the following command:

ansible-playbook ping.yml

Ensure that you are in the directory where the playbook ping.yml is located.

Conclusion

You have successfully learned how to use Ansible to ping hosts with different SSH ports. By modifying the inventory file and specifying the custom SSH ports for each host, you can connect to hosts running on non-default SSH ports. Ansible's flexibility allows you to manage various network configurations easily.