===== ansible cisco =====
gerer du materiel reseau via ansible, exemple avec cisco
pour se connecter sur une equipement reseau sans avoir a saisir de mot de passe afin de favoriser l'automatisation il faut auprealable echanger les clés ssh , puis on fera usage de ansible via ssh pour lancer des commandes .
===== linux keys =====
generer une paire de clée ssh sur la station de commande linux (master node)
[ansnet@ansmnode ~]$ ssh-keygen -b 4096 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansnet/.ssh/id_rsa): /home/ansnet/.ssh/id_rsa_ansiblenet46
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ansnet/.ssh/id_rsa_ansiblenet46.
Your public key has been saved in /home/ansnet/.ssh/id_rsa_ansiblenet46.pub.
formater la clée dans un format compatible avec le copier/coller vers la cli cisco
[ansnet@ansmnode .ssh]$ fold -b -w 72 id_rsa_ansiblenet46.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC+NTIY3H4/yfQymt1kKfgNzEkfSKNkx8U4
Fxn65ZNmt5cx61MEXYK6Ok8HkRWCRAh6DCm/NzDzPLPqMZCj5VeMkIk1RMccafdCVxKfBnPd
....
8gjWECltaV10HCSKeUgZ2S9rT8KIUGlvMhp5Mg1jmRMIgOK+/iJOdMaKylx3zklR8lcJYzTt
UQ== ansnet@ansmnode.domain.fr
association de la clée privée avec le routeur cisco
[ansnet@ansmnode .ssh]$ cat config
host routeur
HostName routeur
IdentityFile ~/.ssh/id_rsa_ansiblenet46
User ansiblenet
avec une definition de l'adresse IP dans le fichier host
[ansnet@ansmnode .ssh]$ cat /etc/hosts | grep routeur
192.168.1.7 routeur
===== compte Cisco avec clé ssh =====
pour realiser nos commandes cisco depuis ansible, on créé un compte de privileges aproprié (ici 10)
routeur(config)#username ansnetco privilege 10 password 0 secretpass
puis on associe la clée publique formattée ci-dessous sous linux par simple copier/coller
routeur(config)#ip ssh pubkey-chain
routeur(conf-ssh-pubkey)#username ansnetco
routeur(conf-ssh-pubkey-user)#key-string
routeur(conf-ssh-pubkey-data)#ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC+NTIY3H4/yfQymt1kKfgNzEkfSKNkx8U4
routeur(conf-ssh-pubkey-data)#Fxn65ZNmt5cx61MEXYK6Ok8HkRWCRAh6DCm/NzDzPLPqMZCj5VeMkIk1RMccafdCVxKfBnPd
.....
routeur(conf-ssh-pubkey-data)#8gjWECltaV10HCSKeUgZ2S9rT8KIUGlvMhp5Mg1jmRMIgOK+/iJOdMaKylx3zklR8lcJYzTt
routeur(conf-ssh-pubkey-data)#UQ== ansnet@ansmnode.domain.fr
routeur(conf-ssh-pubkey-data)#exit
routeur(conf-ssh-pubkey-user)#exit
routeur(conf-ssh-pubkey)#exit
===== acces ssh cisco =====
Maintenant que tout est en place, il est possible de se connecter en ssh en utilisasant le clée ssh pour l'authentificaion .
[ansnet@ansmnode .ssh]$ ssh -c aes192-ctr ansnetco@routeur
routeur#show privilege
Current privilege level is 10
===== Playbook ansible cisco cli cmd =====
playbook ansible qui lance une commande cisco en cli affichant les stats de CPU QOS
routeur#show platform hardware fed switch active qos queue stats internal cpu policer
CPU Queue Statistics
============================================================================================
(default) (set) Queue Queue
QId PlcIdx Queue Name Enabled Rate Rate Drop(Bytes) Drop(Frames)
--------------------------------------------------------------------------------------------
0 11 DOT1X Auth Yes 1000 1000 0 0
1 1 L2 Control Yes 2000 2000 0 0
2 14 Forus traffic Yes 4000 4000 0 0
3 0 ICMP GEN Yes 750 750 0 0
4 2 Routing Control Yes 5500 5500 0 0
5 14 Forus Address resolution Yes 4000 4000 0 0
6 0 ICMP Redirect Yes 750 750 7021943669 59789154
7 16 Inter FED Traffic Yes 2000 2000 0 0
8 4 L2 LVX Cont Pack Yes 1000 1000 0 0
9 19 EWLC Control Yes 13000 13000 0 0
10 16 EWLC Data Yes 2000 2000 0 0
11 13 L2 LVX Data Pack Yes 1000 1000 0 0
12 0 BROADCAST Yes 750 750 31304954 155792
13 10 Openflow Yes 250 250 0 0
14 13 Sw forwarding Yes 1000 1000 2977910 41510
15 8 Topology Control Yes 13000 16000 0 0
...
c'est une commande complexe pour laquelle je n'ai pas trouvé d'OID snmp, ce qui aurai bcp plus simple ...
le playbook ansible ci-dessous realise l'execution de cette commande et stocke le resultat dans un fichier texte locale et noeud master ansible depuis lequel il s'execute
[ansnet@ansmnode .ssh]$ cat /home/ansnet/ansnet/playRecPolicy.yml
---
- name: Playbook to show qos queue stats internal cpu policer on Cisco 9K
connection: network_cli
hosts: routeur
remote_user: ansnetco
gather_facts: False
tasks:
- name: Show platform hardware qos queue stats internal cpu policer on Device
ios_command:
commands:
- show platform hardware fed switch active qos queue stats internal cpu policer
vars:
ansible_ssh_user: ansnetco
ansible_ssh_pass: "{{ ios_password }}"
ansible_network_os: "ios"
# ansible_ssh_private_key_file: '/home/ansnet/.ssh/id_rsa_ansiblenet46'
register: config
- name: Save output to output folder
copy:
content: "{{ config.stdout[0] }}"
dest: "/home/ansnet/ansnet/output/{{ inventory_hostname }}-cpu-policer.txt"
reste a parser le ficher txt pour en retirer les valeurs a grapher .
==== references URLs ====
* https://yetiops.net/posts/ansible-for-networking-part-3-cisco-ios/
* https://community.cisco.com/t5/security-knowledge-base/ise-cli-with-ansible/ta-p/4702386
* https://blog.gainskills.top/2018/09/07/ansible-access-network-via-ssh/
* https://www.ipspace.net/kb/Ansible/Parsing_Text_Printouts_Ansible.html
* https://blog.stephane-robert.info/post/ansible-vault/
* https://mindlesstux.com/2022/01/15/fix-for-ping-socket-operation-not-permitted/