ansible 远程自动交互式命令的响应 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
firejoke
V2EX    Ansible

ansible 远程自动交互式命令的响应

  •  
  •   firejoke 2019-12-23 15:23:35 +08:00 5566 次点击
    这是一个创建于 2165 前的主题,其中的信息可能已经有所发展或是发生改变。

    因为管理一般都是用 ssh 密钥连接远程主机, 但要获取 ssh 密钥还是需要先用密码远程登陆一次去生成和获取 但 ssh-keygen 这个命令是一个交互式命令, 如果用 psexec 模块来做响应匹配, 则需要目标主机安装 pypsexec 包, 但我并不想在部署服务之前在远程主机安装任何依赖, 所以改用 shell 模块 原 shell 脚本:

    if [ -f ~/.ssh/id_rsa.pub ] then cat ~/.ssh/id_rsa.pub elif [ -f ~/.ssh/id_rsa ] && [ ! -f ~/.ssh/id_rsa.pub ] then echo -e "\ny\n\n\n" | ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub else echo -e "\n\n\n\n" | ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub fi 

    改成 play

    - hosts: 192.168.8.128 tasks: - name: create host ssh rsa shell: if [ -f ~/.ssh/id_rsa.pub ];then cat ~/.ssh/id_rsa.pub;elif [ -f ~/.ssh/id_rsa ] && [ ! -f ~/.ssh/id_rsa.pub ];then echo -e "\ny\n\n\n" | ssh-keygen -t rsa; cat ~/.ssh/id_rsa.pub;else echo -e "\n\n\n\n" | ssh-keygen -t rsa; cat ~/.ssh/id_rsa.pub; fi 

    但似乎 echo -e "\ny\n\n\n" | ssh-keygen -t rsa; 这一句并没有起效果, 有遇到过这种问题的吗? 如果还是不行的话, 那就只能 script 模块传送脚本, 或者用 paramiko 封装一个, 之前在其他项目用的是 fabric, 但这个想用 ansible 做

    9 条回复    2019-12-26 14:07:59 +08:00
    stcheng
        1
    stcheng  
       2019-12-23 23:37:55 +08:00 via iPhone
    楼主有没有试过 Ansible 的 expect 模块?
    firejoke
        2
    firejoke  
    OP
       2019-12-24 10:36:11 +08:00
    @stcheng #1 试过, 需要目标主机安装了 pexpect
    stcheng
        3
    stcheng  
       2019-12-25 14:02:34 +08:00
    @firejoke what about using 'yes' command?
    stcheng
        4
    stcheng  
       2019-12-25 14:27:24 +08:00
    我研究了一下应该是 ssh-keygen 的问题 需要用-q 和-N 的参数
    你可以试试这行命令
    ansible localhost -m shell -a "cat /dev/zero | ssh-keygen -q -N ''"

    这个可以保证在主机已经有 id_rsa 和 id_rsa.pub 的时候报错
    也可以让主机在没有 id_rsa 和 id_rsa.pub 的时候产生这两个文件

    ansible localhost -m shell -a "cat /dev/zero | ssh-keygen -q -N ''"
    localhost | FAILED | rc=1 >>
    /home/shuotian/.ssh/id_rsa already exists.
    Overwrite (y/n)? Enter file in which to save the key (/home/shuotian/.ssh/id_rsa): cat: write error: Broken pipe

    ansible localhost -m shell -a "cat /dev/zero | ssh-keygen -q -N ''"
    localhost | SUCCESS | rc=0 >>
    Enter file in which to save the key (/home/shuotian/.ssh/id_rsa): cat: write error: Broken pipe

    这样,也不需要一开始的 if 条件来判断是否存在文件了
    firejoke
        5
    firejoke  
    OP
       2019-12-26 12:32:03 +08:00
    @stcheng #4
    加 -N 参数会报错
    ``` bash
    ansible localhost -m shell -a "cat /dev/zero | ssh-keygen -N -q"

    localhost | FAILED | rc=1 >>
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): Saving key "/root/.ssh/id_rsa" failed: passphrase is too short (minimum five characters)non-zero return code
    ```
    不管远程还是本地, 报的错一样

    本地的不加 -N 可以
    ```bash
    ansible localhost -m shell -a "cat /dev/zero | ssh-keygen -q"
    ```
    但是远程机器还是会卡住, 似乎是管道没起作用
    firejoke
        6
    firejoke  
    OP
       2019-12-26 12:34:07 +08:00
    @firejoke #5 你那个-N 后面是一对单引号吗? 我还以为你是打了两遍双引号......
    stcheng
        7
    stcheng  
       2019-12-26 12:38:52 +08:00
    @firejoke N 后面是一对单引号
    stcheng
        8
    stcheng  
       2019-12-26 12:40:02 +08:00
    -N new_passphrase
    两个单引号是给一个空的 passphrase
    firejoke
        9
    firejoke  
    OP
       2019-12-26 14:07:59 +08:00
    @stcheng #8 远程也可以, 谢谢了!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3408 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 00:43 PVG 08:43 LAX 16:43 JFK 19:43
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86