Identifying IOS and NXOS

485 views
Skip to first unread message

rajthecomputerguy

unread,
Jul 1, 2021, 6:25:43 AM7/1/21
to Ansible Project
Hi Team,

I have 100 devices in host file, both IOS and NXOS mixed. I am trying pull OS details and hostname as report.   Since IOS and NXOS devices are mixed in inventory,  How can IOS_facts and NXOS_facts work in single playbook when there is no group in inventory.

I have only one group in inventory(both IOS and NXOS)

[cisco_devices]
rrtr1_ios
n9k_nexus



Thanks,
Raj

rajthecomputerguy

unread,
Jul 3, 2021, 4:42:41 AM7/3/21
to Ansible Project
Any help would be appreciated 

J C

unread,
Jul 4, 2021, 7:19:50 PM7/4/21
to ansible...@googlegroups.com
Well I assume you must have ansible_network_os variable for the hosts.  So you would just need to make 2 tasks, one for ios and one for nexus.

Then create a when statement

when: ansible_network_os is "ios"

Same for the nexus task just change it too nexus

You put the when statement at the task level.






--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4dbd6f3d-6fb0-4271-8f4d-da2de05c4e86n%40googlegroups.com.

rajthecomputerguy

unread,
Jul 4, 2021, 9:27:10 PM7/4/21
to Ansible Project
I have only one group in inventory file which has IOS and NXOS devices mixed,   I can only set   ansible_network_os variable as 'IOS' 

rajthecomputerguy

unread,
Jul 6, 2021, 6:42:26 AM7/6/21
to Ansible Project
Is there any solution for this? 

nilashis...@gmail.com

unread,
Jul 29, 2021, 11:44:04 AM7/29/21
to Ansible Project
Hello,

I don't think this is possible to achieve if you can _only_ set `ansible_network_os` to `ios`. The *_facts (or any network module with `connection: network_cli`) is dependent on the correct cliconf and terminal plugins being loaded which is identified by `ansible_network_os` value.

Thanks.

Nilashish Chakraborty

unread,
Jul 29, 2021, 1:41:22 PM7/29/21
to ansible...@googlegroups.com
Since your target devices are restricted to IOS and NX-OS which share similarities in CLI structure, you can possibly do something like the following. However, this is not recommended at all and is not guaranteed to work. The correct approach is to fix your inventory and group the target hosts properly.

---
- hosts: cisco_devices
  gather_facts: false
  tasks:
    - name: Determine Network OS
      ansible.netcommon.cli_command:
        command: show version
      register: result
   
    - set_fact:
        ansible_network_os: nxos
      when: '"NXOS" in result.stdout'
   
    - meta: reset_connection

    - name: Gather IOS facts
      cisco.ios.ios_facts:
        gather_subset: ["hardware"]
      when: ansible_network_os == "ios"
   
    - name: Gather NX-OS facts
      cisco.nxos.nxos_facts:
        gather_subset: ["hardware"]
      when: ansible_network_os == "nxos"


Thanks.

rajthecomputerguy

unread,
Jul 30, 2021, 2:28:35 AM7/30/21
to Ansible Project
Thank you .. Awesome !
Reply all
Reply to author
Forward
0 new messages