首页 > 解决方案 > Ansible 与 SSH X11 转发

问题描述

我需要在一些需要 DISPLAY 编号的服务器上为 Firefox 创建默认配置文件。我不知道如何用 Ansible 做到这一点。我一直试图让 X11 转发工作,但没有成功。我不能更改 SSH 配置,所以我不能从那里启用 X11。我怎样才能用 Ansible 做到这一点?

我尝试了以下方法:

  1. - name: Create default profile  
      shell: firefox -createprofile "default /opt/profiles/default"
    

    并将ssh_argsin设置ansible.cfg为:ssh_args= -X

    这失败了:

    ssh连接主机失败:ssh: connect to host as1 port 22: No route to host

  2. - name: Create default profile  
      shell: |  
        export DISPLAY=:0.0  
        firefox -createprofile "default /opt/profiles/default"
    

    不添加ssh_args = -X option

    这失败了:

    未指定协议,错误:无法打开显示::0.0

我可以ssh -X user@as1成功运行:firefox -createprofile "default /opt/profiles/default"

必须有办法做到这一点,但我不知道还有什么可以尝试的。

标签: firefoxsshansible

解决方案


一般来说,不仅仅是针对特定的 firefox 情况,在 ssh 参数中添加“-o ForwardX11=yes”选项应该可以解决问题。你可以这样做:

  1. 从命令行设置 ANSIBLE_SSH_ARGS,如下所示:
ANSIBLE_SSH_ARGS='-C -o ForwardX11=yes -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no' ansible-playbook -i hosts.ini test.yml
  1. 更新 ansible.cfg 文件:
[ssh_connection]
    ssh_args =  -C -o ForwardX11=yes -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no
  1. 我的首选方式,在 hosts.ini 文件中设置 ansible_ssh_extra_args,例如:
    [all]
    hostname ansible_host=10.0.0.1 ansible_user=user ansible_ssh_extra_args='-o ForwardX11=yes'

推荐阅读