Juniper 设备上的 Ansible 操作失败并显示 "sftp/scp transfer mechanism failed"

Ansible operations on Juniper devices fail with "sftp/scp transfer mechanism failed"

我的剧本:

- name: get junos facts
  hosts: sw
#  connection: local
  gather_facts: no
  roles:
    - juniper.junos

  tasks:

    - name: Retrieve Junos OS version
      junipernetworks.junos.junos_command:
        commands: show version

主机:

[sw]
EX4200-2
EX4200-1

变量:

ansible_network_os: juniper_junos

Ansible 配置

ansible-playbook [core 2.12.1]
  config file = /usr/local/san/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/python/python38/lib/python3.8/site-packages/ansible
  ansible collection location = /root/.ansible/collections/ansible_collections
  executable location = /usr/local/python/python38/bin/ansible-playbook
  python version = 3.8.10 (default, Dec 30 2021, 10:44:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  jinja version = 3.0.3
  libyaml = True

运行 剧本的结果

[WARNING]: sftp transfer mechanism failed on [10.1.1.196]. Use ANSIBLE_DEBUG=1 to see detailed information

<10.1.1.196> SSH: EXEC sshpass -d13 scp -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'User="admin"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/0d45650285 /root/.ansible/tmp/ansible-local-3715dofr3qmk/tmpiaq5tnso '[10.1.1.196]:'"'"'error: unknown command: /bin/sh/AnsiballZ_junos_command.py'"'"''

[WARNING]: scp transfer mechanism failed on [10.1.1.196]. Use ANSIBLE_DEBUG=1 to see detailed information
<10.1.1.196> ESTABLISH SSH CONNECTION FOR USER: xxx
<10.1.1.196> SSH: EXEC sshpass -d13 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'User="admin"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/0d45650285 10.1.1.196 'dd of=error: unknown command: /bin/sh/AnsiballZ_junos_command.py bs=65536'
<10.1.1.254> (0, b'\nerror: unknown command: /bin/sh\n', b'')
...
fatal: [EX4200-2]: FAILED! => {
    "changed": false,
    "module_stderr": "Shared connection to 10.1.1.196 closed.\r\n",
    "module_stdout": "\r\nerror: unknown command: /bin/sh\r\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 0
}

SCP 和 SFTP 似乎有问题。但是我手动执行SCP和SFTP是正常的

我该如何修正这个错误?

我的配置文件错了 变量添加行: ansible_connection: ansible.netcommon.network_cli

留言

MODULE FAILURE
error: unknown command: /bin/sh

表示远程节点上没有shell、缺少权限等

正如您已经指出的,要使用来自 Ansible Collection Junipernetworks.Junos, according Junos OS Platform Options 的模块,需要正确配置连接设置。

---
- name: Get Junos facts
  hosts: sw
  gather_facts: false

  vars:

    ansible_network_os: juniper_junos
    ansible_connection: ansible.netcommon.network_cli
  
  roles:

    - juniper.junos

  tasks:

    - name: Retrieve Junos OS version
      junipernetworks.junos.junos_command:
        commands: show version
      regsiter: show_version

另外推荐使用junos_factsCollect facts from remote devices running Juniper Junos in Example

- name: Collect default set of facts and configuration
  junipernetworks.junos.junos_facts:
    gather_subset: config