In this lesson, we explore how Ansible can efficiently manage multiple servers that use different SSH ports and unique usernames while running the same playbook (often referred to as a runbook). This approach is essential when dealing with servers that do not share a common configuration for connection parameters.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Typically, Ansible relies on a common hosts inventory file along with a shared configuration file (ansible.cfg) to define connection details such as the default username. For example, cloud setups like AWS might use the EC2 user as a default. However, when individual servers require distinct SSH ports and usernames, these default configurations become insufficient.When working with servers that have varying connection parameters, you must override the defaults by specifying the SSH port and username for each host directly within the inventory file.
Example Inventory Configuration
Below is an example that demonstrates how to define unique connection parameters for each host. In this configuration, two servers are specified with their respective IP addresses, SSH ports, and usernames:Interview Tip
This configuration strategy is a common interview topic. When asked how to manage multiple hosts with different connection credentials, the correct answer is to explicitly specify theansible_port and ansible_user for each host in the inventory file. This approach allows Ansible to connect using the correct credentials for every server.