Skip to content

Commit

Permalink
Check if SSH alias exists (pyinfra-dev#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhammes authored and Fizzadar committed Aug 18, 2024
1 parent a610304 commit 23a3bb7
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pyinfra_cli/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pyinfra import logger
from pyinfra.api.inventory import Inventory
from pyinfra.connectors.sshuserclient.client import get_ssh_config
from pyinfra.context import ctx_inventory

from .exceptions import CliError
Expand Down Expand Up @@ -88,7 +89,28 @@ def _resolves_to_host(maybe_host: str) -> bool:
socket.getaddrinfo(maybe_host, port=None)
return True
except socket.gaierror:
return False
logger.debug('Checking if "%s" is an SSH alias', maybe_host)
alias = _get_ssh_alias(maybe_host)
if not alias:
return False

try:
socket.getaddrinfo(alias, port=None)
return True
except socket.gaierror:
return False


def _get_ssh_alias(maybe_host: str) -> Optional[str]:
ssh_config = get_ssh_config()

options = ssh_config.lookup(maybe_host)
alias = options.get("hostname")

if alias is None or maybe_host == alias:
return None

return alias


def make_inventory(
Expand Down

0 comments on commit 23a3bb7

Please sign in to comment.