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.