Mega-lab - Part 3: Using Ansible to gather information from hosts and save to files

2 minute read

Updated:

In this post, we will set up an Ansible playbook to collect some informationfrom hosts and save the outputs to different files depending on the hostname.The following steps show how to setup and run an Ansible playbook:

  • Creating the inventory file
  • Modifying theansible.cfg
  • Writing the playbook

1. The ansible folder structure

We will need to create the ansible folder according to the following structure:

.├── ansible.cfg├── gather_commands.yml├── inventory│   └── hosts.yml└── output    ├── R1.txt    └── R2.txt

Theansible.cfg is as follows:

[defaults]inventory = hosts.ymlhost_key_checking = FalseANSIBLE_SSH_ARGS = -oKexAlgorithms=+diffie-hellman-group1-sha1 -oHostKeyAlgathering = explicit#interpreter_python = .venv/bin/python[paramiko_connection]#host_key_auto_add = Truelook_for_keys = Falsehost_key_checking = False[persistent_connection]connect_timeout = 30#connect_retry_timeout = 15command_timeout = 30

2. Create the hosts inventory file

all:children:CORE1:children:Switch1:hosts:R1:ansible_host:10.15.1.1R2:ansible_host:10.15.1.2vars:ansible_user:ciscoansible_password:ciscoansible_connection:network_cliansible_network_os:ios

We can check the connection to a groups of routers defined in the inventoryfile.

ansible all-i inventory-m ping

Output:

R1 | SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/home/doanh/.pyenv/shims/python3.7"},"changed":false,"ping":"pong"}R2 | SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/home/doanh/.pyenv/shims/python3.7"},"changed":false,"ping":"pong"}

We can use the Ansible ad-hoc CLI tool to run a command on a remote host.

ansible-i inventory-m setup-u cisco R1

Herecisco is the user on the routerR1.

The setup module, which is part of Ansible core, enables users to view facts gathered on remote hosts. By default, Ansible collects these facts when we use the Ansible tool to manage hosts.

Suppose that we want to run a command on dozens of routers. Instead of opening a terminal session to each router and scraping the output, we can use the Ansible ad-hoc CLI with group definition in an inventory file.

ansible-i inventory-mcommand-a"df -k"-u cisco Switch1

3. Create the Ansible playbook

---- name: Gather commands  hosts: Switch1  vars:    commands:      - show version      - show ip int br      - show memory statistics      - show arp      - show ip route  tasks:    - name: Gather commands      ios_command:        commands: "{{commands}}"      register: output    - name: save output to a file      copy:        content: "{{output.stdout|zip(commands)|flatten|join('\n')}}"        dest: "output/{{inventory_hostname}}.txt"

4. Running the Ansible playbook

We run the Ansible playbook to gather the following information:

  • show version
  • show ip int br
  • show memory statistics
  • show arp
  • show ip route

The command to run is as follow:

ansible-playbook-i inventory gather_commands.yml

Output:

PLAY[Gather commands]************************************************************************************************************TASK[Gather commands]************************************************************************************************************ok:[R1]ok:[R2]TASK[save output to a file]******************************************************************************************************changed:[R2]changed:[R1]PLAY RECAP************************************************************************************************************************R1                         :ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0   R2                         :ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0

The outputs file will be saved in theoutput folder:R1.txt andR2.txt.

Categories:

Updated:

Twitter Facebook LinkedIn

Comments

You May Also Enjoy

Introduction to SOLID Principles

6 minute read

Hey friends! Today I’m talking about SOLID - five principles that will level up your object-oriented programming game.

Cisco SD-WAN: Configuring and Attaching Templates to WAN Edge devices for BGP configuration

13 minute read

I recently joined a study group at RouterGods, focusing on Cisco SD-WANsolutions. Let me start first by saying thank you to my RouterGods friend Judson Bishopfor organizing th...

Nornir 3.0 app: Fastcli for auto-config of interfaces, OSPF, BGP, EIGRP, RIP

6 minute read

I am having a passion for Network Automation and DevNet. Since I want to lab every day in GNS3 or EVE-NG, I am committed to automating all of the repetitive tasks such as inter...

vManage REST APIs - Cisco SD-WAN 20.3.1 and building the SDWANCLI

8 minute read

In this post, we will explore the vManage REST APIs, and use python requests module to interact with vManage for extracting some useful information, including list of devices i...