Automating Network Devices with cli_command
Push config to switches and routers.
Automating Network Devices with cli_command is a free Ansible Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Servers Are Not the Only Targets
Ansible can manage routers and switches too, not just Linux hosts. Network automation pushes config to devices over SSH or an API. 🌐
Why Network Devices Are Different
Switches usually lack Python, so Ansible cannot copy modules onto them. The network_cli connection runs commands remotely over the device CLI instead.
vars:
ansible_connection: ansible.netcommon.network_cli
ansible_network_os: cisco.ios.iosMeet cli_command
The cli_command module sends a raw command to a network device and returns its output, like typing at the device prompt yourself.
- name: Show version
ansible.netcommon.cli_command:
command: show versionCapture the Output
Use register to save what the device returns. The result lands in stdout, ready for you to inspect or parse later.
- name: Read running config
ansible.netcommon.cli_command:
command: show running-config
register: cfgSend Configuration Commands
To change config, the device must be in config mode. cli_config handles entering and leaving that mode for you safely.
- name: Set hostname
ansible.netcommon.cli_config:
config: hostname core-sw01Answer Interactive Prompts
Some commands ask for confirmation. cli_command can reply automatically using prompt and answer pairs so the play never hangs.
- ansible.netcommon.cli_command:
command: reload
prompt:
- Proceed with reload
answer:
- 'y'Vendor-Neutral by Design
The same cli_command task works across Cisco, Juniper or Arista. Only ansible_network_os changes, so your style stays consistent.
Use Platform Modules When You Can
For structured, idempotent changes, prefer modules like cisco.ios.ios_config. Reach for cli_command when no specific module fits.
- cisco.ios.ios_config:
lines:
- description uplink to core
parents: interface GigabitEthernet0/1Credentials Over SSH
Network devices still authenticate over SSH. Set ansible_user and a vaulted password or key so plays can log in cleanly.
vars:
ansible_user: netadmin
ansible_password: "{{ vault_net_pass }}"Save the Running Config
Changes live in the running config until saved. Run a write command so settings survive a reboot of the device. 💾
- ansible.netcommon.cli_command:
command: write memoryWhere These Modules Come From
cli_command ships in the ansible.netcommon collection. Install it from Galaxy before your network playbooks can use it.
ansible-galaxy collection install ansible.netcommonQuick Check
Let us check how you talk to a switch.
Recap
The cli_command module, via the network_cli connection, lets Ansible run and save commands on switches and routers over SSH. ✅
Frequently asked questions
Is the “Automating Network Devices with cli_command” lesson free?
Yes — the full text of “Automating Network Devices with cli_command” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Automating Network Devices with cli_command”?
Push config to switches and routers. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Automating Network Devices with cli_command” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Automating Network Devices with cli_command
- delegate_to & run_once for Orchestration
- Coordinate Web, App & DB Tiers
- Load Balancer Drain & Re-Enable