Ansible bruker ios_facts modulen for å hente informasjon fra nettverksenheter.
Det finnes flere moduler som henter inn informasjon fra nettverksenheter.
Som for eksempel:
ansible -i inventory --ask-vault-pass -m ios_facts switcher -l IOS_XE_switch_4
Vault password:
IOS_XE_switch_4 | SUCCESS => {
"ansible_facts": {
"ansible_net_api": "cliconf",
"ansible_net_gather_network_resources": [],
"ansible_net_gather_subset": [
"default"
],
"ansible_net_hostname": "IOS-XE-switch-4",
"ansible_net_image": "bootflash:packages.conf",
"ansible_net_iostype": "IOS-XE",
"ansible_net_model": "C9KV-Q200-8P",
"ansible_net_operatingmode": "controller",
"ansible_net_python_version": "3.10.6",
"ansible_net_serialnum": "CML12345Q20",
"ansible_net_stacked_serialnums": [
"CML12345Q20"
],
"ansible_net_system": "ios",
"ansible_net_version": "17.12.01prd9",
"ansible_network_resources": {}
},
"changed": false
}
ansible -i inventory --ask-vault-pass -m ios_facts switcher -l IOS_XE_switch_4 -a "gather_subset=min gather_network_resources=l3_interfaces"
Vault password:
IOS_XE_switch_4 | SUCCESS => {
"ansible_facts": {
"ansible_net_api": "cliconf",
"ansible_net_gather_network_resources": [
"l3_interfaces"
],
"ansible_net_gather_subset": [
"default"
],
"ansible_net_hostname": "IOS-XE-switch-4",
"ansible_net_image": "bootflash:packages.conf",
"ansible_net_iostype": "IOS-XE",
"ansible_net_model": "C9KV-Q200-8P",
"ansible_net_operatingmode": "controller",
"ansible_net_python_version": "3.10.6",
"ansible_net_serialnum": "CML12345Q20",
"ansible_net_stacked_serialnums": [
"CML12345Q20"
],
"ansible_net_system": "ios",
"ansible_net_version": "17.12.01prd9",
"ansible_network_resources": {
"l3_interfaces": [
{
"ipv4": [
{
"dhcp": {
"enable": true
}
}
],
"name": "GigabitEthernet0/0"
},
{
"name": "GigabitEthernet1/0/1"
},
{
"name": "GigabitEthernet1/0/2"
},
{
"name": "GigabitEthernet1/0/3"
},
{
"name": "GigabitEthernet1/0/4"
},
{
"name": "GigabitEthernet1/0/5"
},
{
"name": "GigabitEthernet1/0/6"
},
{
"name": "GigabitEthernet1/0/7"
},
{
"name": "GigabitEthernet1/0/8"
},
{
"name": "Vlan1"
},
{
"ipv4": [
{
"address": "10.170.0.234/24"
}
],
"name": "Vlan170"
}
]
}
},
"changed": false
}
}
---
- name: Hente informasjon fra switcher
hosts: switcher
connection: network_cli
gather_facts: no
tasks:
- name: Hent informasjon fra switcher
ios_facts:
gather_subset: min
gather_network_resources: l3_interfaces
- name: Skriv ut informasjon
debug:
msg:
- "{{ ansible_net_hostname }}"
- "{{ ansible_network_resources['l3_interfaces'][0] }}"
- "{{ ansible_network_resources['l3_interfaces'][1] }}"
- "{{ ansible_network_resources['l3_interfaces'][2] }}"
ansible-playbook playbook_ios_facts.yaml --ask-vault-password -l IOS_XE_switch_4
Vault password:
PLAY [Hente informasjon fra switcher] **************************************************************************************************************************************************************************************************
TASK [Hent informasjon fra switcher] ***************************************************************************************************************************************************************************************************
ok: [IOS_XE_switch_4]
TASK [Skriv ut informasjon] ************************************************************************************************************************************************************************************************************
ok: [IOS_XE_switch_4] => {
"msg": [
"IOS-XE-switch-4",
{
"ipv4": [
{
"dhcp": {
"enable": true
}
}
],
"name": "GigabitEthernet0/0"
},
{
"name": "GigabitEthernet1/0/1"
},
{
"name": "GigabitEthernet1/0/2"
}
]
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
IOS_XE_switch_4 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0