ansiblejuniper

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


My playbook:

- 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

hosts:

[sw]
EX4200-2
EX4200-1

vars:

ansible_network_os: juniper_junos

Ansible config

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

The result of running the playbook

[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
}

Looks like there was a problem with SCP and SFTP. But it is normal for me to execute SCP and SFTP manually.

How can I fix this mistake?


Solution

  • The message

    MODULE FAILURE
    error: unknown command: /bin/sh
    

    indicates that there is no shell on Remote Node(s), missing rights, etc.

    As you already pointed out, to use the modules from Ansible Collection Junipernetworks.Junos, according Junos OS Platform Options it will be neccessary to configure the connection setup properly.

    ---
    - 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
    

    Furthermore it is recommended to use junos_facts to Collect facts from remote devices running Juniper Junos in Example

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