【问题标题】:How to construct customized ssh connection from ansible playbook如何从 ansible playbook 构建自定义的 ssh 连接
【发布时间】:2020-12-27 15:05:15
【问题描述】:

ansible 的以下 ssh 连接无法连接到远程主机

ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/app/ssh_keys/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o ControlPath=/home/ansibleuser/.ansible/cp/6abdc12511 -tt 10.9.88.205 'id mwweb || id webadm || ls -ld /web'

而当我从 ssh 中删除以下两个参数时,我的连接成功

1. -tt 
2.  -o ControlPath=/home/ansibleuser/.ansible/cp/6abdc12511 

需要 ansible 构建的工作 ssh 命令。

ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/app/ssh_keys/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no 10.9.88.205 'id mwweb || id webadm || ls -ld /web'

此要求(自定义 ssh 命令)适用于特定目标主机的特定 playbook,它作为参数提供给我下面的 ansible playbook。我不想在操作系统上修改现有的 ssh 配置:

- name: Play 2- Configure Source nodes
  hosts: all
  user: root
  ignore_errors: yes
  gather_facts: false
  tasks:

   - name: Get HTTPD userid on server
     raw: id mwweb || id webadm || ls -ld /web

   - name: Get OHS userid on server
     raw: id mwweb

上面的剧本使用这个命令运行:

ansible-playbook -i 10.9.88.205, -f 5 testpython.yml -vvvv

我正在使用 jenkin 的 ansible 插件来触发上面的剧本。

您能否为以下问题提供解决方案:

  1. 我可以通过修改 playbook 代码来禁用 -ttControlPath 吗?这是我的第一个偏好。请推荐?

  2. 如果修改 playbook 没有帮助,那么如何使用 ansible 参数禁用两个 ssh args?

我可以使用以下方法禁用 -tt:

ansible-playbook -i 10.9.88.205, -f 5 testpython.yml -e ansible_ssh_use_tty=no -vvvv

但是,尽管通过了-e control_path="",但无法找到禁用ControlPath的方法

参考:https://docs.ansible.com/ansible/latest/plugins/connection/ssh.html

你能推荐一下吗?

【问题讨论】:

    标签: ssh parameters ansible arguments connection


    【解决方案1】:

    您无法通过添加 ansible conf 文件来自定义 Ansible 配置。

    可以在配置文件中进行更改和使用,该配置文件将按以下顺序处理:

    • ANSIBLE_CONFIG(环境变量)
    • ansible.cfg(在当前目录中)
    • .ansible.cfg(在主目录中)
    • /etc/ansible/ansible.cfg

    关于 Ansible 文档:https://docs.ansible.com/ansible/2.3/intro_configuration.html#environmental-configuration

    在某些具有很长主机名或很长路径名(由长用户名或深度嵌套的主目录引起)的系统上,这可能会超过文件套接字名称的字符限制(大多数平台为 108 个字符)。在这种情况下,您可能希望将字符串缩短为如下所示: control_path = %(directory)s/%%h-%%r

    您还可以将 ssh_args 设置为该文件:

    ssh_args = -o ControlMaster=auto -o ControlPersist=60s
    

    您的 ssh 配置部分将与自定义 ssh args 类似:

    [ssh_connection]
    ssh_args = -o ControlMaster=auto -o ControlPersist=60s 
    

    【讨论】:

    • 我不是在寻找有关修改全局 ssh 配置文件的解决方案。我想要在剧本testpython.ymlansible-playbook 命令行中同时禁用ssh 参数-tt-o ControlPath 的解决方案。
    • 您好,如果您指定自定义 ssh_args,它将禁用默认 ssh args。您可以使用 ansible conf 文件与 ssh 参数进行交互。 (一个项目的本地)或所有ansible项目的全局)取决于您使用的conf文件
    • ssh_args 没有像 tty 那样在 ansible-playbook 命令行中传递它的选项,即-e ansible_ssh_use_tty=no。请求您通过修改我的testpython.yml 或将确切的-e <argument> 分享为ansible-playbook @idriss Eliguene 来提供解决方案
    • 根据你的命令行试试这个:ansible-playbook your_playbook.yml --ssh-common-args="-o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/app/ssh_keys/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no"。我使用 --ssh-common-arg 标志来传递您想要的确切 ssh 参数。
    • ansible-playbook your_playbook.yml --ssh-common-args="-o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile=\"/app/ssh_keys/id_rsa\"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User=\"root\"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2017-11-27
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多