title: Ansible Tutorial 3: Variables, Modules & Network Fact Gathering tags: - ansible - tutorial - variables - network facts - getting started
Ansible Tutorial 3: Variables, Modules & Network Fact Gathering
This tutorial covers how to use variables, advanced modules, and gather facts from network devices with Ansible.
Ansible Variables
Variables allow you to customize playbooks and reuse code. You can define them in playbooks, inventories, group_vars/
, or host_vars/
.
Example:
Cisco IOS Modules
Use modules like cisco.ios.ios_command
, cisco.ios.ios_config
, and cisco.ios.ios_facts
for Cisco devices.
Example:
- name: Get version from Cisco router
hosts: routers
gather_facts: no
connection: network_cli
tasks:
- name: Show version
cisco.ios.ios_command:
commands:
- show version
register: version_output
- debug:
var: version_output.stdout_lines
Using cli_command
ansible.netcommon.cli_command
is a generic module for sending commands to network devices (multi-vendor).
Example:
- name: Use cli_command on Cisco
hosts: routers
gather_facts: no
connection: network_cli
tasks:
- name: Show interfaces
ansible.netcommon.cli_command:
command: show interfaces
register: interfaces
- debug:
var: interfaces.stdout_lines
Using Enable Mode (become)
Some commands require privileged (enable) mode. Use become: yes
and set ansible_become_password
.
Example:
- name: Use enable mode
hosts: routers
gather_facts: no
connection: network_cli
become: yes
vars:
ansible_become_password: your_enable_password
tasks:
- name: Show running config
cisco.ios.ios_command:
commands:
- show running-config
register: config
- debug:
var: config.stdout_lines
Passing Credentials
You can pass credentials via variables, group_vars/
, host_vars/
, or environment variables.
Example using environment variables:
Gathering Network Facts
Use modules like cisco.ios.ios_facts
to gather detailed information from devices.
Example:
- name: Gather facts from Cisco IOS
hosts: routers
gather_facts: no
connection: network_cli
tasks:
- name: Get facts
cisco.ios.ios_facts:
register: facts
- debug:
var: facts
Congratulations! You now have a solid foundation in Ansible for network automation. Explore more advanced topics and real-world playbooks as your next step.