Ansible
Python
Kubernetes
Java
SSH
Terraform
PowerShell
Custom SSH port when using Ansible
How to specify a custom SSH port when using Ansible
To specify a custom SSH port when using Ansible, you need to configure it in your inventory file or pass it as an argument to the ansible-playbook command. Here’s how you can do it:
Option 1: Specify SSH Port in the Inventory File
You can set the SSH port directly in your Ansible inventory file. This is useful if you have multiple hosts or if you want to keep your configuration centralized.
Example inventory.ini file:
ini [servers] server1 ansible_host=192.168.1.100 ansible_port=2222
Option 2: Specify SSH Port in the Playbook
You can also specify the SSH port in the playbook itself, but this is less common. Instead, it is more typical to use the inventory file or command-line arguments.
Option 3: Use Command-Line Arguments
If you prefer to specify the port when running the ansible-playbook command, you can do so using the -i option to provide the inventory and --ssh-common-args to set the port:
bash ansible-playbook -i inventory.ini your_playbook.yml --ssh-common-args='-p 2222'
Example Playbook
Here’s an example of a playbook that checks connectivity, using the inventory file from Option 1:
yaml --- - name: Check connectivity to servers hosts: servers tasks: - name: Ping the servers ansible.builtin.ping:
Steps to Run
1. Create the Inventory File:
2. Save the following as inventory.ini:
ini [servers] server1 ansible_host=192.168.1.100 ansible_port=2222
3. Create the Playbook File:
4. Save the following as check_connectivity.yml:
yaml --- - name: Check connectivity to servers hosts: servers tasks: - name: Ping the servers ansible.builtin.ping:
5.Run the Playbook:
6.Use the following command to execute the playbook:
bash ansible-playbook -i inventory.ini check_connectivity.yml
This setup ensures that Ansible uses port 2222 when connecting to server1 and will correctly execute the connectivity check.