0

有没有办法在 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。只是几乎不知道如何获得批量设备的输出)非常感谢任何帮助。谢谢!

4

1 回答 1

0

由于我将有一个类似的用例,并且将来可能还会有更多,因此我在 RHEL 7.9 环境中设置了一个简短的测试。

据我了解,Cisco Nexus 系列NX-OSBash是推荐的其他模块,它们来自社区 集合。之前需要安装它们

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

就足够了。

于 2021-09-20T15:06:10.597 回答