如何从ansible playbook构建定制的ssh连接

问题描述 投票:0回答:2

以下 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. 我可以通过修改剧本代码来禁用

    -tt
    ControlPath
    吗?这是我的第一选择。请推荐一下?

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

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

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

但是,尽管通过了

ControlPath
,却无法找到禁用
-e control_path=""

的方法

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

你能推荐一下吗?

ssh parameters ansible arguments connection
2个回答
2
投票

您无法通过添加 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 = %(目录)s/%%h-%%r

您还可以设置该文件的 ssh_args :

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

您的 ssh 配置部分将类似于带有自定义 ssh 参数的部分:

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

0
投票

您可以在任务变量中设置连接参数,例如:

   - name: Get HTTPD userid on server
     raw: id mwweb || id webadm || ls -ld /web
     vars:
       ansible_ssh_use_tty: false # disable -tt flag
       ansible_control_path: none # disable connection sharing

变量名称和默认值在 ansible.builtin.ssh 连接插件文档中列出。

并且

ansible_control_path: none
应根据 ssh_config man:

禁用连接共享
       ControlPath
               Specify the path to the control socket used for
               connection sharing as described in the ControlMaster
               section above or the string none to disable connection
               sharing. ...
© www.soinside.com 2019 - 2024. All rights reserved.