【问题标题】:Ansible ad-hoc commands don't work with Cisco devicesAnsible ad-hoc 命令不适用于 Cisco 设备
【发布时间】:2017-09-09 03:11:19
【问题描述】:

我新安装了带有 Ansible 的 Ubuntu 服务器。 我尝试在我的网络中使用 Ansible,但它从一开始就失败了

10.102.249.3是路由器

zab@UbuntuSrv:/etc/ansible$ ansible 10.102.249.3 -a "conf t" --ask-pass -vvv       
SSH password: 
<10.102.249.3> ESTABLISH CONNECTION FOR USER: zab
<10.102.249.3> REMOTE_MODULE command conf t
<10.102.249.3> EXEC ['sshpass', '-d6', 'ssh', '-C', '-tt', '-q', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/zab/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'StrictHostKeyChecking=no', '-o', 'Port=22', '-o', 'GSSAPIAuthentication=no', '-o', 'PubkeyAuthentication=no', '-o', 'ConnectTimeout=10', '10.102.249.3', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210 && echo $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210'"]
<10.102.249.3> PUT /tmp/tmpZUkRET TO Line has invalid autocommand "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210 && echo $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210'"/command
10.102.249.3 | FAILED => failed to transfer file to Line has invalid autocommand "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210 && echo $HOME/.ansible/tmp/ansible-tmp-1412930091.8-230458979934210'"/command:

Connection to 10.102.249.3 closed by remote host.
Connection closed

zab@UbuntuSrv:/etc/ansible$ ansible 10.102.249.3 -m ping  --ask-pass -vvv         
SSH password: 
<10.102.249.3> ESTABLISH CONNECTION FOR USER: zab
<10.102.249.3> REMOTE_MODULE ping
<10.102.249.3> EXEC ['sshpass', '-d6', 'ssh', '-C', '-tt', '-q', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/zab/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'StrictHostKeyChecking=no', '-o', 'Port=22', '-o', 'GSSAPIAuthentication=no', '-o', 'PubkeyAuthentication=no', '-o', 'ConnectTimeout=10', '10.102.249.3', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532 && echo $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532'"]
<10.102.249.3> PUT /tmp/tmpOPuOWh TO Line has invalid autocommand "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532 && echo $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532'"/ping
10.102.249.3 | FAILED => failed to transfer file to Line has invalid autocommand "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532 && echo $HOME/.ansible/tmp/ansible-tmp-1412930136.7-170302836431532'"/ping:

Connection to 10.102.249.3 closed by remote host.
Connection closed

更新: 我的剧本有什么问题? 我得到ERROR: raw is not a legal parameter at this level in an Ansible Playbook

---
- hosts: testsw
  remote_user: zab
  tasks: 
  - name: copy tftp run
    raw: copy tftp://10.1.78.153/test running-config

Update2:谢谢,现在可以播放了。但它无法连接到交换机(cisco 3750、cisco 2960)。我可以从此服务器制作 ssh。 Ansible 连接到第二个交换机,我们可以看到横幅。我记得 Ansible 将 python 脚本推送到远程主机。但现在我在剧本中使用原始模块。是这样的吗?

ansible-playbook test.yml --ask-pass
SSH password: 

PLAY [testsw] ***************************************************************** 

GATHERING FACTS *************************************************************** 
fatal: [10.0.100.61] => failed to transfer file to Line has invalid autocommand "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1413965089.88-16456712970308 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1413965089.88-16456712970308 && echo $HOME/.ansible/tmp/ansible-tmp-1413965089.88-16456712970308'"/setup:

Connection to 10.0.100.61 closed by remote host.
Connection closed

fatal: [10.0.100.60] => failed to transfer file to Line has invalid autocommand "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1413965089.85-196216747271106 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1413965089.85-196216747271106 && echo $HOME/.ansible/tmp/ansible-tmp-1413965089.85-196216747271106'"/setup:

C Authorized access only!
 Disconnect IMMEDIATELY if you are not an authorized network administrator!
channel_by_id: 2: bad id: channel free
client_input_channel_req: channel 2: unknown channel
Connection to 10.0.100.60 closed by remote host.
Connection closed


TASK: [copy tftp run] ********************************************************* 
FATAL: no hosts matched or all hosts have already failed -- aborting


PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/zab/test.retry

10.0.100.60                : ok=0    changed=0    unreachable=1    failed=0   
10.0.100.61                : ok=0    changed=0    unreachable=1    failed=0   

【问题讨论】:

  • 你的缩进错误。固定。

标签: ansible cisco


【解决方案1】:

对于 Update2:您需要禁用收集 Cisco 设备的事实,因为这不受支持。

---
- hosts: testsw
  remote_user: zab
  gather_facts: false
  tasks: 
  - name: copy tftp run
    raw: copy tftp://10.1.78.153/test running-config

原始模块只是在写入时通过 ssh 发送字符串,这通常是我使用 Ansible + Cisco 设备的方式。

为了使 openssh 与 Cisco 设备配合使用,我通常设置我的本地 .ssh/config,如下所示:

Host *
  StrictHostKeyChecking no
  UserKnownHostsFile=/dev/null
  ServerAliveInterval 120
  ServerAliveCountMax 2
  ControlPath ~/.ssh/master-%r@%h:%p
  ControlMaster auto
  ControlPersist 60s

【讨论】:

  • 您好,请问您是否曾经设法使用此配置来获取多行原始命令以与 Cisco 或 Juniper 设备一起使用?
【解决方案2】:

Ansible 现在在核心模块中支持 cisco ios 设备。你可以看看这里:https://docs.ansible.com/ansible/ios_config_module.html

这是一个简单的例子。

  1. site.yml
- 名称:在 cisco ios 设备上播放显示命令并更改运行配置 主持人: - 思科 收集事实:没有 连接:本地 角色: - 思科
  1. tasks/main.yml
--- - 名称:定义提供者 设置事实: 提供者: 主机:“{{inventory_hostname}}” 用户名:“{{用户名}}” 密码:“{{密码}}” - 名称:使用 ios_command 运行显示命令 ios_命令: 提供者:“{{提供者}}” 命令: - 显示版本 - 显示ip接口简介 - 显示库存 - 显示 ntp 关联 注册:show_result - 调试:var=show_result.stdout_lines - 名称:使用 ios_config 更改 ntp 服务器 ios_config: 提供者:“{{提供者}}” 授权:是 行: - ntp 服务器 {{ntpsrv01}} - ntp 服务器 {{ntpsrv02}} 替换:块 匹配:线 前: - 没有 ntp 服务器 {{ntpsrv03}} - 没有 ntp 服务器 {{ntpsrv04}} 力:真 - 名称:使用 ios_config 更改后运行显示 ntp 关联 ios_命令: 提供者:“{{提供者}}” 命令: - 显示 ntp 关联 注册:after_change_ntp - 调试:var=after_change_ntp.stdout_lines - 名称:使用 ios_template 更改系统日志服务器 ios_模板: 主机:“{{inventory_hostname}}” 用户名:“{{用户名}}” 密码:“{{密码}}” 源代码:config.j2 力:真 - 名称:更改后运行“显示运行|包括日志记录主机” ios_命令: 提供者:“{{提供者}}” 命令: - 显示运行|包括日志主机 注册:syslogsrv_list - 调试:var=syslogsrv_list.stdout_lines - 名称:将运行配置写入启动配置 ios_命令: 提供者:“{{提供者}}” 命令: - 写 寄存器:write_output - 调试:var=write_output.stdout_lines

【讨论】:

    【解决方案3】:

    由于您发送的是conf t,我想您是在尝试与 Cisco 路由器通信。 这是行不通的,因为 Ansible 会推送在远程主机上执行的 python 脚本。

    做某事的唯一方法是使用raw 模块,如下所示:

    ansible cat -m raw -a 'show clock' --ask-pass -c paramiko
    SSH password: 
    cat | success | rc=0 >>
    
    11:11:51.676 METDST Fri Oct 10 2014
    

    我也必须使用 paramiko 进行连接。 甚至不要考虑以这种方式发送配置:对模块的每次调用都会重新打开一个 ssh 连接,因此您不能使用连续的 ansible 命令发送配置。

    您可以做的最好的事情是copy tftp://.... startup-config,首先将配置文件发送到 tftp 服务器。

    然而,这很容易用 ansible 编排。

    祝你好运。

    【讨论】:

    • 请查看 Update2。如果您可以分享您的配置,那就太好了。
    • 你必须使用 paramiko(见上面的-c paramiko
    • 现在我遇到了设备身份验证问题。它以前有效(没有剧本),现在当我制作ansible all -m raw -a 'show clock' --ask-pass -c paramiko -vvv 我得到kh.tb03 | FAILED =&gt; FAILED: Authentication failed. 我仔细检查了密码,我复制粘贴了它。 cisco 上的调试表示选择了 ACS 方法,但我没有看到 ACS 中的任何内容失败或通过了 attemptps。从服务器到设备的 SSH 工作正常。有什么想法吗?
    猜你喜欢
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多