首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Ansible中运行Cisco NX-OS Bash shell命令?

如何在Ansible中运行Cisco NX-OS Bash shell命令?
EN

Stack Overflow用户
提问于 2021-06-25 10:52:46
回答 1查看 108关注 0票数 0

有没有办法在Ansible中运行Cisco NX-OS Bash shell命令,而无需任务进入配置模式?

我只想得到下面的命令输出,但总是失败。

代码语言:javascript
复制
    bash-4.3# smartctl -a /dev/sda | egrep 'Model|Firmware|Hours'
Device Model:     Micron_M600_MTFDDAT064MBF
Firmware Version: MC04
  9 Power_On_Hours          0x0032   100   100   000    Old_age   Always       -       17014

我使用的是下面的攻略。

代码语言:javascript
复制
 - name: running the bash commands
    ios_command:
      commands:
        - conf t
        - feature bash
        - run bash sudo su
        - smartctl -a /dev/sda | egrep 'Model|Firmware|Hours'
    register: uptime

  - name: output the result
    debug:
      msg: uptime

  - name: run the last command
    ios_command:
      commands: smartctl -a /dev/sda | egrep 'Model|Firmware|Hours'
    register: uptime

  - name: write to the file
    ansible.builtin.template:
      src: ./templates/9k_uptime.j2
      dest: ./9k_uptime/9k_uptime.txt
      newline_sequence: '\r\n'

(**我不精通Ansible。几乎不知道如何获得批量设备的输出)任何帮助都是非常感谢的。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-09-20 15:06:11

因为我将有一个类似的用例,将来可能还会有更多的用例,所以我已经在RHEL7.9环境中设置了一个简短的测试。

据我所知,对于Cisco Nexus系列,推荐的其他模块包括NX-OSBash,这些模块来自Community Collections。必须先安装它们,然后才能

代码语言:javascript
复制
ansible-galaxy collection install cisco.nxos # --ignore-certs 
Process install dependency map
Starting collection install process
Installing ... to '/home/${USER}/.ansible/collections/ansible_collections/community/cisco ...'

以及将集合路径添加到库路径。

代码语言:javascript
复制
vi ansible.cfg
...
[defaults]
library = /usr/share/ansible/plugins/modules:~/.ansible/plugins/modules:~/.ansible/collections/ansible_collections/
...

现在可以在远程设备上运行命令

代码语言:javascript
复制
  # Usage of command module
  # Doc: https://docs.ansible.com/ansible/latest/collections/cisco/nxos/nxos_command_module.html
  
  - name: Run command on remote device
    cisco.nxos.nxos_command:
      commands: show version
    register: results

  - name: Show results
    debug:
      msg: "{{ results.stdout_lines }}"

或收集设备信息,例如配置。

代码语言:javascript
复制
  # Gather device information
  # Doc: https://docs.ansible.com/ansible/latest/collections/cisco/nxos/nxos_facts_module.html
  
  - name: Gather only the config and default facts
    cisco.nxos.nxos_facts:
      gather_subset:
      - config

  - name: Show facts
    debug:
      msg: "{{ ansible_facts }}"

如果只对内核正常运行时间感兴趣,请执行以下操作

代码语言:javascript
复制
commands: show version | i uptime

就足够了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68124829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档