You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible执行copy run scp命令无限挂起,如何解决?

解决Nexus设备通过Ansible复制运行配置到远程服务器的挂起问题

针对你用cisco.nxos.nxos_command执行SCP复制配置时任务无限挂起的问题,可尝试以下方案:

1. 修正Prompt匹配规则

Nexus设备执行SCP时的密码提示可能不是单纯的"password:",比如首字母大写的"Password:",或者带设备前缀的格式(如switch1# Password:)。手动登录设备执行一次copy run scp://...命令,记录实际弹出的提示文本,然后调整prompt参数:

- name: Copy config to remote server
  cisco.nxos.nxos_command:
    commands:
    - command: copy run scp://{{ username }}@{{ remote_server }}{{ remote_location }}/{{ hostname }}_config.txt vrf management
      prompt:
      - "Password:"  # 替换为实际抓包到的提示文本
      answer:
      - "{{ password }}"

2. 增加命令执行完成的等待标志

模块可能无法识别命令是否执行完毕,通过wait_for参数指定复制成功后的特征文本,比如Nexus常用的"Successfully copied"或者设备提示符:

- name: Copy config to remote server
  cisco.nxos.nxos_command:
    commands:
    - command: copy run scp://{{ username }}@{{ remote_server }}{{ remote_location }}/{{ hostname }}_config.txt vrf management
      prompt:
      - "Password:"
      answer:
      - "{{ password }}"
      wait_for:
      - result contains "Successfully copied"  # 匹配成功提示

3. 采用SSH密钥认证消除交互

给Nexus设备配置SCP服务器的公钥,让设备无需密码即可推送文件:

  • 在Nexus设备上执行ssh key <server_ip> vrf management获取服务器公钥并信任
  • 或者直接将SCP服务器的公钥添加到Nexus的~/.ssh/authorized_keys(如果设备支持)
    之后简化任务,去掉prompt和answer:
- name: Copy config to remote server
  cisco.nxos.nxos_command:
    commands:
    - copy run scp://{{ username }}@{{ remote_server }}{{ remote_location }}/{{ hostname }}_config.txt vrf management

4. 改用离线导出后上传的方案(最稳定)

先通过nxos_config模块导出运行配置到控制节点,再用copy模块上传到远程服务器:

- name: Fetch running config from Nexus
  cisco.nxos.nxos_config:
  register: nexus_config

- name: Save config to local file
  copy:
    content: "{{ nexus_config.running_config }}"
    dest: "/tmp/{{ hostname }}_config.txt"

- name: Upload config to remote server
  copy:
    src: "/tmp/{{ hostname }}_config.txt"
    dest: "{{ remote_location }}/{{ hostname }}_config.txt"
  delegate_to: "{{ remote_server }}"

内容的提问来源于stack exchange,提问作者stacksstack1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 20:35:30