有没有办法在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。几乎不知道如何获得批量设备的输出)任何帮助都是非常感谢的。谢谢!
发布于 2021-09-20 15:06:11
因为我将有一个类似的用例,将来可能还会有更多的用例,所以我已经在RHEL7.9环境中设置了一个简短的测试。
据我所知,对于Cisco Nexus系列,推荐的其他模块包括NX-OS和Bash,这些模块来自Community Collections。必须先安装它们,然后才能
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就足够了。
https://stackoverflow.com/questions/68124829
复制相似问题