skip to Main Content

Looking for assistance with an odd error I am troubleshooting with a playbook.

I have a working SSH session to a switch, but having difficulty with transferring files via SCP on Ansible. I can start a SCP session directly from the same server with no issues and can transfer a text file (the same one references below) but it does not seem to work in Ansible.

I enabled verbose logging via Ansible and this is what I am seeing in the logfile generated.

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/usr/lib/python3/dist-packages/ansible/utils/jsonrpc.py", line 46, in handle_request
        result = rpc_method(*args, **kwargs)
      File "/root/.ansible/collections/ansible_collections/ansible/netcommon/plugins/connection/network_cli.py", line 1282, in copy_file
        self.ssh_type_conn.put_file(source, destination, proto=proto)
      File "/root/.ansible/collections/ansible_collections/ansible/netcommon/plugins/connection/libssh.py", line 498, in put_file
        raise AnsibleError(
    ansible.errors.AnsibleError: Error transferring file to flash:test.txt: Initializing SCP session of remote file [flash:test.txt] for w>
    
    2022-10-06 11:58:35,671 p=535932 u=root n=ansible | fatal: [%remoteSwitch%]: FAILED! => {
        "changed": false,
        "destination": "flash:test.txt",
        "msg": "Exception received: Error transferring file to flash:test.txt: Initializing SCP session of remote file [flash:test.txt] fo>
    }

Afraid Google is not helping me too much with this one. If it helps, this is on Ubuntu 22.04, with Ansible 2.10.8.

Play attempting to be ran is:

- hosts: %remoteSwitch%
  vars:
    - firmware_image_name: "test.txt"
  tasks:
    - name: Copying image to the switch... This can take time, please wait...
      net_put:
        src: "/etc/ansible/firmware_images/C2960X/{{  firmware_image_name  }}"
        dest: "flash:{{  firmware_image_name  }}"
      vars:
        ansible_command_timeout: 20
        protocol: scp

2

Answers


  1. It would be helpful to know what type of connection it is and what platform.

    I see from the file that it is a Cisco IOS device. Do you have the following settings?

    ansible_connection: ansible.netcommon.network_cli
    ansible_network_os: cisco.ios.ios
    

    The following documentation mentions the need for paramiko. Will it work if you change the ssh_type to paramiko?

    https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/net_put_module.html

    ssh_type can be set as follows:

    configuration:

    INI entry:
    [persistent_connection]
    ssh_type = paramiko

    Environment variable: ANSIBLE_NETWORK_CLI_SSH_TYPE

    Variable: ansible_network_cli_ssh_type

    Variable: ansible_network_cli_ssh_type

    Login or Signup to reply.
  2. I ran into this same error. I was able to fix it by switching back to Paramiko SSH. This can be accomplished by either pip uninstall ansible-pylibssh (note, this very likely has other side-effects).

    Alternatively, you can force Paramiko usage at the Ansible play level:

    ---
    - name: Test putting a file onto Cisco IOS/IOS-XE device
      hosts: cisco1
      # ansible-pylibssh errors out here (force paramiko usage)
      vars:
        ansible_network_cli_ssh_type: paramiko
      tasks:
        - name: Copy file
          ansible.netcommon.net_put:
            src: my_file1.txt
            dest : flash:/my_file1.txt
            protocol: scp
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search