pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.91k stars 382 forks source link

Allow specifying an alternative ~/.ssh/config file #715

Closed artizirk closed 2 years ago

artizirk commented 2 years ago

That would allow shipping the ssh_config file in the repo next to other inventory and deploy files. I mostly use the ssh_config file for configuring ProxyJump config arguments.

OpenSSH client has -F option that allows using other config file.

Currently i'm hacking around this limitation by injecting this snippet into my config.py

from os import path
from pyinfra.api.connectors.sshuserclient import client
from pyinfra.api.connectors.sshuserclient.config import SSHConfig
from pyinfra.api.util import memoize

@memoize
def get_ssh_config():
    user_config_file = 'ssh_config'
    if path.exists(user_config_file):
        with open(user_config_file) as f:
            ssh_config = SSHConfig()
            ssh_config.parse(f)
            return ssh_config

client.get_ssh_config = get_ssh_config
Fizzadar commented 2 years ago

Great idea! I've added an implementation of this in https://github.com/Fizzadar/pyinfra/pull/734/commits/7bdf55db6e5e94f7b3d87cbf0094ddc5646304f8, pending 1.5.1 or 1.6.

Fizzadar commented 2 years ago

This is now live in v1.6 :)

themanifold commented 2 years ago

Just wanted to make it clear that what you do to set this is, for example, in group_data/all.py, add ssh_config_file = "custom_ssh_config". If you wanted to set a custom config per group, then you would put that into an appropriate group_data/some_group.py.

Writing this because there are no docs apart from this line of code: https://github.com/Fizzadar/pyinfra/blob/7bdf55db6e5e94f7b3d87cbf0094ddc5646304f8/pyinfra/api/connectors/ssh.py#L170