V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
css3
V2EX  ›  程序员

ansible 如何使用 ssh 通道连接

  •  
  •   css3 · 2021-05-31 14:26:22 +08:00 · 1578 次点击
    这是一个创建于 1062 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目标机 mysql 需要使用 192.168.13.44:1022 机器进行跳转才能登录,想在 ansible 中自动进行跳转,查阅了资料,按以下参数进行配置无效, 不清楚是没配置对还是咋的了:

    # inventory
    
    [mysql]
    ansible_host=10.9.13.22 ansible_ssh_user=root ansible_ssh_pass="admin&*()"
    
    [mysql:vars]
    ansible_ssh_common_args='-o ProxyCommand="sshpass -p passwork ssh -W %h:%p -q [email protected]:1022"'
    
    # 测试指令
    
    ansible myql -m ping
    
    # 执行结果
    
    mysql | UNREACHABLE! => {
        "changed": false,
        "msg": "Failed to connect to the host via ssh: kex_exchange_identification: Connection closed by remote host",
        "unreachable": true
    }
    

    请教下各位

    第 1 条附言  ·  2021-06-01 09:16:31 +08:00

    通过 ssh -p 10022 ... 的方式,直接连接还不是行,需要先手动 ssh 登录一遍 192.168.13.44 生成 known_hosts ,然后 ansible 就能自动使用代理登录了,但不清楚为啥要不会自动登录中转机自动生成 known_hosts

    # inventory 
    
    mysql:vars]
    
    ansible_ssh_common_args='-o ProxyCommand="sshpass -p passwork ssh -p 10022 -W %h:%p -q [email protected]"'
    
    7 条回复    2021-06-01 13:09:31 +08:00
    NessajCN
        1
    NessajCN  
       2021-05-31 14:57:01 +08:00 via Android
    ssh 命令那个 root@ip 后面不能跟:端口
    css3
        2
    css3  
    OP
       2021-05-31 15:56:29 +08:00 via iPhone
    @NessajCN 那端口不是 22 的话,咋指定啊
    NessajCN
        3
    NessajCN  
       2021-05-31 16:11:38 +08:00
    @css3 ssh -p 或去 ssh config 里写 Host 配置
    css3
        4
    css3  
    OP
       2021-05-31 16:15:19 +08:00
    @NessajCN 改成这样也不行:
    ansible_ssh_common_args='-o ProxyCommand="sshpass -p passwork ssh -p1022 -W %h:%p -q [email protected]"'
    css3
        5
    css3  
    OP
       2021-05-31 16:16:07 +08:00
    -p1022 中间有空格的,回帖忘了加
    ry_wang
        6
    ry_wang  
       2021-05-31 22:32:13 +08:00
    hostvar ansible_port
    css3
        7
    css3  
    OP
       2021-06-01 13:09:31 +08:00
    @ry_wang 不是主机的 port, 是中转机的 port
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5737 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 06:25 · PVG 14:25 · LAX 23:25 · JFK 02:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.