ansible - 如何在 Ansible 中运行 Cisco NX-OS Bash shell 命令?
问题描述
有没有办法在 Ansible 中运行 Cisco NX-OS Bash shell 命令而无需进入配置模式的任务?
我只想获得以下命令输出但一直失败。
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
我使用的是下面的剧本。
- 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。只是几乎不知道如何获得批量设备的输出)非常感谢任何帮助。谢谢!
解决方案
由于我将有一个类似的用例,并且将来可能还会有更多,因此我在 RHEL 7.9 环境中设置了一个简短的测试。
据我了解,Cisco Nexus 系列NX-OS和Bash是推荐的其他模块,它们来自社区 集合。之前需要安装它们
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 ...'
以及将集合路径添加到库路径。
vi ansible.cfg
...
[defaults]
library = /usr/share/ansible/plugins/modules:~/.ansible/plugins/modules:~/.ansible/collections/ansible_collections/
...
现在可以在远程设备上运行命令
# 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 }}"
或收集设备信息,例如配置。
# 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 }}"
如果只对内核正常运行时间感兴趣,请执行以下操作
commands: show version | i uptime
就足够了。
推荐阅读
- c++ - valgrind 和堆的问题
- r - 在 R 中正确投影栅格并检索其数据
- linux - dwm 架构 Linux。如何进行键绑定以运行终端程序
- html - 如何使div内的表格水平溢出?
- laravel - Laravel Livewire:设置模型属性不会刷新视图
- c - 为什么我不能返回 realloc 的结果?(当你看到代码时你会得到问题......)
- reactjs - 如何在 Recharts 条形图中的条之间添加间隙(垂直,堆叠)
- class - get 方法没有所需参数的问题
- c - 如何测试我自己的 kbhit() 替代方案的源代码?
- java - 如何在hibernate中使用多个表的主键作为第三个表的主键