Our first configuration playbook against two Cisco routers reported changed on every run, even when nothing on the routers had changed since the run before. The playbook wasn’t broken. IOS simply never shows one of the lines we asked for in its running configuration, so Ansible kept pushing it. Most of what goes wrong when you run Ansible against Cisco IOS is like that. The commands are short, and the surprises come from SSH and from how IOS stores its own configuration.
This guide runs commands on Cisco IOS routers with Ansible from a Linux control node. It covers an ad hoc command, a playbook of show commands, configuration changes with ios_config, and an idempotent version of the same change. It also tests the result with wait_for, saves and backs up the configuration, and walks through six errors we hit on the way, each with its fix. The runs are from November 2026 (Ansible 14.6.0 with ansible-core 2.21.6, the cisco.ios 11.6.0 collection and paramiko 4.0.0 on Ubuntu 24.04, driving Cisco 7200 routers on IOS 15.2(4)S3 in GNS3).
How Ansible runs commands on a Cisco router
Ansible needs no agent on a Cisco router, and nothing Python runs on the device. The modules execute on the control node, which opens an SSH session to the router with the ansible.netcommon.network_cli connection plugin, types commands at the CLI and reads the replies the way you would. The ansible_network_os setting tells Ansible which platform’s prompts, error strings and paging behavior to expect, so cisco.ios.ios is what makes it understand R1# and % Invalid input.
Three modules from the cisco.ios collection did all the work in this lab. They differ mostly in when they report a change, and that matters more than it sounds, because changed is how you tell a run that did something from a run that found everything already in place. A scheduled playbook that reports changes on a quiet night is either drifting or badly written, and you want to know which:
| Module | What it sends | Reports changed |
|---|---|---|
cisco.ios.ios_command | EXEC commands such as show, ping and copy | Never |
cisco.ios.ios_config | Configuration lines, after comparing them with the running configuration | When a line it sends is missing from the running configuration |
cisco.ios.ios_interfaces | Interface settings described as data (description, enabled, speed) | When the device’s state differs from the data |
Running a command is the easy half. ios_command sends whatever you give it and hands back the output as stdout and stdout_lines, which you can print, register or test. The harder half is configuration, where Ansible has to decide whether a change is needed at all, and that decision is where the surprises in this guide come from.
The lab: one control node and two routers
The control node is an Ubuntu 24.04 machine on the routers’ management network. R1 and R2 each have their GigabitEthernet0/0 port on that network, and the playbooks address and test the GigabitEthernet1/0 link between them. A plain switch joins the management ports, so nothing in the path filters SSH, and every error later in this guide comes from Ansible or the routers themselves:

Ansible logs in exactly the way you would, so each router needs the same pieces a person needs to configure SSH access on a Cisco device. If any one of them is missing, the playbook fails before it sends a single command:
- An address on a port the control node can reach
- A hostname other than Router and a domain name, which IOS needs before it will generate an RSA key
- The RSA key pair itself, which SSH needs before it starts
- A local user, here at privilege 15
- VTY lines that accept SSH and check the local user database
We typed these on each router’s console, because Ansible can’t reach a router that has no SSH yet. That first bootstrap is always done by hand or by whatever provisions the device, and everything after it can come from playbooks. These are R1’s commands in global configuration mode, and R2 is identical apart from its address, 192.168.122.12.
hostname R1
ip domain name lab.example
username ansible privilege 15 secret AnsLab2026
interface GigabitEthernet0/0
description MGMT to Ansible control node
ip address 192.168.122.11 255.255.255.0
no shutdown
line vty 0 4
transport input ssh
login local
crypto key generate rsa modulus 2048
ip ssh version 2
Privilege 15 puts the ansible user straight into privileged EXEC mode, so the playbooks need no enable step. If your account lands at the > prompt instead, Ansible’s IOS platform guide covers the become settings that send enable for you. Ping both routers from the control node before going further, because every later error is easier to read once you know the network path works.
Install Ansible on the control node
Ansible goes into a Python virtual environment here, which keeps its version and the SSH library pinned to this project. Ubuntu’s Python comes without ensurepip, and without it python3 -m venv stops with “ensurepip is not available”, so install the package that provides it first:
sudo apt update
sudo apt install -y python3-venv
Create a project directory with its own environment and activate it. Every later command in this guide runs from that directory, because Ansible looks for ansible.cfg in the current directory first:
mkdir -p ~/ansible-lab && cd ~/ansible-lab
python3 -m venv .venv
source .venv/bin/activate
Now install Ansible together with paramiko, the SSH library network_cli falls back to. The version cap matters for older routers. Paramiko 5 removed every SHA-1 key exchange, and IOS 15 routers like these offer nothing else, so the current paramiko cannot connect to them at all (the error is in the troubleshooting section below):
pip install ansible "paramiko<5" # pin: older IOS offers only SHA-1 key exchange #https://www.paramiko.org/changelog.html
The ansible package pulls in ansible-core and the network collections, and pip settles on the last 4.x paramiko. These are the versions Ubuntu 24.04’s Python 3.12 gets. A newer Python resolves to the next Ansible major release, which we haven’t run against these routers:
Successfully installed MarkupSafe-3.0.4 PyYAML-6.0.4 ansible-14.6.0 ansible-core-2.21.6 bcrypt-5.0.0 cffi-2.1.1 cryptography-50.0.2 invoke-3.0.3 jinja2-3.1.6 packaging-26.4 paramiko-4.0.0 pycparser-3.12 pynacl-1.6.2 resolvelib-1.2.1
Newer devices may not need the cap at all. Cisco’s IOS XE 17 SSH algorithm list shows curve25519, ECDH and SHA-2 Diffie-Hellman key exchange in the default set, with several of them added between releases 17.8 and 17.11, so on IOS XE 17.11 or later the current paramiko should connect. Test one device without the cap before you drop it everywhere. Then confirm the two collections the playbooks use came with the package:
ansible-galaxy collection list | grep -E "cisco.ios |ansible.netcommon "
The output should list both. If cisco.ios is missing, you installed ansible-core on its own, which ships without any network collections, so either install the full ansible package as above or add the collection with ansible-galaxy collection install cisco.ios:
ansible.netcommon 8.7.1
cisco.ios 11.6.0
On a distribution where you would rather install Ansible from packages, the Ansible install guide for Ubuntu covers that route. You then still need paramiko or ansible-pylibssh next to it for network devices.
Write the inventory and ansible.cfg
Two small files tell Ansible where the routers are and how to talk to them. The first is the project’s ansible.cfg, which Ansible reads from the current directory.
vim ansible.cfg
Point it at the inventory and write the command timeout down, so you know where to change it later (30 seconds is also the default). Host key checking stays at its default, which matters later in the troubleshooting section:
[defaults]
inventory = inventory.yml
[persistent_connection]
command_timeout = 30
The inventory carries the connection settings as group variables, so every router in routers inherits them and a third router needs only its own four lines.
vim inventory.yml
Each host gets its management address plus three variables the playbooks use for the point-to-point link:
routers:
hosts:
R1:
ansible_host: 192.168.122.11
link_ip: 10.0.12.1
peer: R2
peer_ip: 10.0.12.2
R2:
ansible_host: 192.168.122.12
link_ip: 10.0.12.2
peer: R1
peer_ip: 10.0.12.1
vars:
ansible_connection: ansible.netcommon.network_cli
ansible_network_os: cisco.ios.ios
ansible_network_cli_ssh_type: paramiko
ansible_user: ansible
ansible_password: "{{ lookup('ansible.builtin.env', 'IOS_PASSWORD') }}"
Four of those variables do the real work:
ansible_connection: ansible.netcommon.network_climakes Ansible drive the CLI over SSH instead of copying Python to the device.ansible_network_os: cisco.ios.iosloads the IOS prompt and error handling. Without it,network_clidoesn’t know what kind of device it is talking to.ansible_network_cli_ssh_type: paramikoforces paramiko. The default,auto, prefers ansible-pylibssh when it is installed. The network_cli documentation marks the paramiko option as deprecated, with removal in a release after 1 February 2028, so plan to move to libssh once your devices offer modern ciphers.ansible_passwordreads the password from an environment variable, so it never sits in a file. For anything beyond a lab, keep it in an encrypted file with Ansible Vault.
In bash, set the variable without echoing it to the screen or saving it in your history:
read -rsp "IOS password: " IOS_PASSWORD && export IOS_PASSWORD
The Ansible inventory guide goes further into groups, host_vars files and dynamic inventories. Two routers with everything inline is enough to start.
Run one command ad hoc
An ad hoc command is the quickest proof that SSH, credentials and the platform setting all work, because it runs a single module with no playbook around it. Ask both routers for their software version:
ansible routers -m cisco.ios.ios_command -a "commands='show version | include IOS Software'"
Each router answers with one line. changed is false because ios_command never claims to change anything, even when the command you send does, so a copy or clear sent this way still reports ok in the play recap:
R1 | SUCCESS => {
"changed": false,
"stdout": [
"Cisco IOS Software, 7200 Software (C7200-ADVENTERPRISEK9-M), Version 15.2(4)S3, RELEASE SOFTWARE (fc1)"
],
"stdout_lines": [
[
"Cisco IOS Software, 7200 Software (C7200-ADVENTERPRISEK9-M), Version 15.2(4)S3, RELEASE SOFTWARE (fc1)"
]
]
}
R2 | SUCCESS => {
"changed": false,
"stdout": [
"Cisco IOS Software, 7200 Software (C7200-ADVENTERPRISEK9-M), Version 15.2(4)S3, RELEASE SOFTWARE (fc1)"
],
"stdout_lines": [
[
"Cisco IOS Software, 7200 Software (C7200-ADVENTERPRISEK9-M), Version 15.2(4)S3, RELEASE SOFTWARE (fc1)"
]
]
}
The IOS output filter ran on the router, so only the matching line came back. That’s worth using on anything long, since the whole reply travels back over SSH and lands in Ansible’s result. The ad hoc commands guide covers the general syntax if -m and -a are new to you.
Run show commands from a playbook
A playbook makes the same work repeatable and lets you send several commands in one task, which you’ll want as soon as you check more than one thing per router. Create show.yml.
vim show.yml
The first task sends two commands and registers the result. The second prints stdout_lines, which holds one list of lines per command:
- name: Collect state from the routers
hosts: routers
gather_facts: false
tasks:
- name: Run show commands
cisco.ios.ios_command:
commands:
- show version | include uptime
- show ip interface brief | exclude unassigned
register: show_out
- name: Print the output
ansible.builtin.debug:
var: show_out.stdout_lines
gather_facts: false skips a fact-collection step these plays never use, which saves a round of commands on every router. Run the playbook:
ansible-playbook show.yml
Both routers report a fresh boot and a single addressed interface, the management port. GigabitEthernet1/0 is missing from the table because the exclude unassigned filter hides every port without an address, which is how you keep the output short on a switch with 48 ports:
PLAY [Collect state from the routers] ******************************************
TASK [Run show commands] *******************************************************
ok: [R1]
ok: [R2]
TASK [Print the output] ********************************************************
ok: [R1] => {
"show_out.stdout_lines": [
[
"R1 uptime is 1 minute"
],
[
"Interface IP-Address OK? Method Status Protocol",
"GigabitEthernet0/0 192.168.122.11 YES manual up up"
]
]
}
ok: [R2] => {
"show_out.stdout_lines": [
[
"R2 uptime is 1 minute"
],
[
"Interface IP-Address OK? Method Status Protocol",
"GigabitEthernet0/0 192.168.122.12 YES manual up up"
]
]
}
PLAY RECAP *********************************************************************
R1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
R2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Ansible works on both routers at once and prints each result as it arrives, so on a bigger inventory the order of hosts changes from run to run. The indexes are what you build on: show_out.stdout_lines[1] is the interface table, which a later task can loop over or test. The playbook tutorial covers registers, loops and conditionals in more depth.
Push configuration with ios_config
ios_config takes configuration lines and an optional parents list for the section they belong to. Before sending anything, it reads the running configuration and pushes only the lines that are missing. Create link.yml to address the R1 to R2 link.
vim link.yml
The {{ peer }} and {{ link_ip }} values come from each host’s inventory entry, so one task configures both ends:
- name: Address the R1 to R2 link
hosts: routers
gather_facts: false
tasks:
- name: Configure GigabitEthernet1/0
cisco.ios.ios_config:
parents: interface GigabitEthernet1/0
lines:
- description Link to {{ peer }}
- ip address {{ link_ip }} 255.255.255.252
- no shutdown
Do a dry run first, so you see the change before any router does. --check computes the change without sending it, because ios_config only needs the running configuration to work out what is missing, and -v prints the exact commands Ansible would type:
ansible-playbook link.yml --check -v
Look at the commands list for each router. It’s the parent line followed by all three child lines, because none of them exist yet:
TASK [Configure GigabitEthernet1/0] ********************************************
[WARNING]: To ensure idempotency and correct diff the input configuration lines should be similar to how they appear if present in the running configuration on device
changed: [R2] => {"banners": {}, "changed": true, "commands": ["interface GigabitEthernet1/0", "description Link to R1", "ip address 10.0.12.2 255.255.255.252", "no shutdown"], "updates": ["interface GigabitEthernet1/0", "description Link to R1", "ip address 10.0.12.2 255.255.255.252", "no shutdown"]}
changed: [R1] => {"banners": {}, "changed": true, "commands": ["interface GigabitEthernet1/0", "description Link to R2", "ip address 10.0.12.1 255.255.255.252", "no shutdown"], "updates": ["interface GigabitEthernet1/0", "description Link to R2", "ip address 10.0.12.1 255.255.255.252", "no shutdown"]}
The warning appears whenever ios_config has lines to send, even in check mode, and here it turns out to be a real hint. Apply the change by running the same command without --check. The output is the one above with the changes actually sent, and the link comes up on both routers.
Why ios_config reports changed on every run
Run ansible-playbook link.yml -v a second time, with nothing touched in between. A configuration playbook should now report changed=0, because the state it describes already exists. Instead, both routers report a change again:
changed: [R2] => {"banners": {}, "changed": true, "commands": ["interface GigabitEthernet1/0", "no shutdown"], "updates": ["interface GigabitEthernet1/0", "no shutdown"]}
changed: [R1] => {"banners": {}, "changed": true, "commands": ["interface GigabitEthernet1/0", "no shutdown"], "updates": ["interface GigabitEthernet1/0", "no shutdown"]}
The description and address are gone from the list, so the comparison worked for them. no shutdown is still there, and it will be on every run from now on. IOS writes shutdown under an interface that is administratively down and writes nothing at all under one that is up, so the line no shutdown never appears in the running configuration for ios_config to find. The ios_config documentation states the rule: lines must match the running configuration exactly to be idempotent. That’s also why abbreviations like shut or int gi1/0 break it.
A playbook that always reports a change is worse than noise. It hides the run where something really did drift, and it pushes configuration into production on every scheduled run. The clean fix is a resource module, which compares structured state instead of text, so you describe the port as up and it works out whether IOS needs telling. Create link-fixed.yml.
vim link-fixed.yml
ios_interfaces owns the description and the up or down state, and ios_config keeps the address line, which IOS does print exactly as written:
- name: Address the R1 to R2 link
hosts: routers
gather_facts: false
tasks:
- name: Set the description and bring the port up
cisco.ios.ios_interfaces:
config:
- name: GigabitEthernet1/0
description: Link to {{ peer }}
enabled: true
state: merged
- name: Address the port
cisco.ios.ios_config:
parents: interface GigabitEthernet1/0
lines:
- ip address {{ link_ip }} 255.255.255.252
Run it with -v against the same routers. They already carry the configuration from link.yml, so this is the run that shows whether the new playbook recognises a finished job.
ansible-playbook link-fixed.yml -v
The before data shows how the module sees the device. GigabitEthernet1/0 already has the right description and enabled: true, so its commands list is empty and nothing is sent:
TASK [Set the description and bring the port up] *******************************
ok: [R1] => {"before": [{"duplex": "auto", "enabled": false, "name": "Ethernet0/0"}, {"description": "MGMT to Ansible control node", "duplex": "full", "enabled": true, "name": "GigabitEthernet0/0", "speed": "1000"}, {"description": "Link to R2", "enabled": true, "name": "GigabitEthernet1/0"}, {"enabled": false, "name": "GigabitEthernet2/0"}], "changed": false, "commands": []}
ok: [R2] => {"before": [{"duplex": "auto", "enabled": false, "name": "Ethernet0/0"}, {"description": "MGMT to Ansible control node", "duplex": "full", "enabled": true, "name": "GigabitEthernet0/0", "speed": "1000"}, {"description": "Link to R1", "enabled": true, "name": "GigabitEthernet1/0"}, {"enabled": false, "name": "GigabitEthernet2/0"}], "changed": false, "commands": []}
TASK [Address the port] ********************************************************
ok: [R1] => {"changed": false}
ok: [R2] => {"changed": false}
Every rerun ends with changed=0 on both routers now, which is what lets you schedule the playbook and trust it. The rule of thumb we follow: use ios_command to read, a resource module when one exists for the feature, and ios_config for everything else, writing each line exactly as show running-config prints it.
Test the link with wait_for
A configuration task proves only that the commands were accepted. To prove the link works, have each router ping its peer and fail the task if the ping doesn’t fully succeed. Create verify.yml.
vim verify.yml
wait_for checks the output of the first command (result[0]) for the 100 percent line, and retries three times, two seconds apart, before giving up. The module defaults are nine retries one second apart. Two-second gaps suit a link that has only just come up, because a router often drops the first ping to a new neighbor while ARP resolves its MAC address, so you want the check to tolerate one slow start without hiding a real failure:
- name: Test the link from both ends
hosts: routers
gather_facts: false
tasks:
- name: Ping the peer
cisco.ios.ios_command:
commands: ping {{ peer_ip }} repeat 5
wait_for: result[0] contains 'Success rate is 100'
retries: 3
interval: 2
register: ping_out
- name: Show the result line
ansible.builtin.debug:
msg: "{{ ping_out.stdout_lines[0][-1] }}"
Run it once the link playbook has finished. If an interface is still coming up, the retries give it a few seconds before the task fails, which is the whole point of wait_for.
ansible-playbook verify.yml
Both ends report 5 of 5 replies, and the debug task pulls just the summary line out of the ping output. Round trips of 8 to 16 ms are normal for emulated routers sharing one host’s CPU, so treat them as proof of reachability and not as a performance figure:
TASK [Ping the peer] ***********************************************************
ok: [R2]
ok: [R1]
TASK [Show the result line] ****************************************************
ok: [R2] => {
"msg": "Success rate is 100 percent (5/5), round-trip min/avg/max = 8/11/16 ms"
}
ok: [R1] => {
"msg": "Success rate is 100 percent (5/5), round-trip min/avg/max = 8/10/12 ms"
}
Had the address been wrong on one side, that router’s task would have failed with “One or more conditional statements have not been satisfied” and listed the condition in failed_conditions, which stops the play before it touches anything else. Put a check like this after every change you make.
Save and back up the configuration
Nothing so far survives a reload, because ios_config changes only the running configuration. Its default for save_when is never. You can save in two ways:
save_when: modifiedon anios_configtask, which saves only when running and startup differ- An explicit
copy running-config startup-configsent throughios_command
The first is tidier, but it failed for us on routers whose NVRAM had been written by a different IOS image (see “command timeout triggered” below), because IOS asks an extra question that save_when doesn’t answer. So the explicit save comes first, and after one successful save the extra question goes away. Create save.yml.
vim save.yml
The command is passed as a dictionary with prompt and answer lists, so Ansible can answer IOS’s questions. The prompts are regular expressions, which is why the brackets and question marks are escaped:
- name: Save the running config
hosts: routers
gather_facts: false
tasks:
- name: Copy running-config to startup-config
cisco.ios.ios_command:
commands:
- command: copy running-config startup-config
prompt:
- 'Destination filename \[startup-config\]\?'
- 'Overwrite the previous NVRAM configuration\?\[confirm\]'
answer:
- "\r"
- "\r"
register: save_out
- name: Show what IOS printed
ansible.builtin.debug:
var: save_out.stdout_lines
Run ansible-playbook save.yml. On these routers the first save printed a second question, about NVRAM written by a different version of the system image, and the playbook answered both. Here it came from the emulated routers’ fresh NVRAM. On real hardware you are most likely to meet it after an IOS upgrade, which is exactly when an automated save that hangs would hurt most:
ok: [R2] => {
"save_out.stdout_lines": [
[
"Destination filename [startup-config]? ",
"Warning: Attempting to overwrite an NVRAM configuration previously written",
"by a different version of the system image.",
"Overwrite the previous NVRAM configuration?[confirm]",
"Building configuration...",
"[OK]"
]
]
}
A second run on R1 got only the filename prompt and [OK], and the same playbook handled both cases. From then on, save_when: modified also worked: a task that added ip ssh time-out 60 reported changed on R1, and show startup-config showed the line afterwards.
Saving to NVRAM protects you from a reload, but not from a mistake in the configuration itself. For that, keep a copy on the control node. Create backup.yml.
vim backup.yml
With backup: true and no lines, ios_config changes nothing on the router and writes its running configuration to a file named after the host:
- name: Back up running configs
hosts: routers
gather_facts: false
tasks:
- name: Save the running config to the control node
cisco.ios.ios_config:
backup: true
backup_options:
dir_path: ./backups
filename: "{{ inventory_hostname }}.cfg"
After ansible-playbook backup.yml, which reports changed for each router because it wrote a file, check what landed on disk:
ls backups
grep -A3 "interface GigabitEthernet1/0" backups/R1.cfg
One file per router, holding the link the earlier playbooks built:
R1.cfg
R2.cfg
interface GigabitEthernet1/0
description Link to R2
ip address 10.0.12.1 255.255.255.252
negotiation auto
Commit that directory to Git after each run and you get a dated history of every router’s configuration, with git diff showing exactly what changed and when. The file holds the user’s password hash, so it belongs in a private repository. When a backup needs to travel between devices instead, copy it with SCP rather than TFTP.
Practice Ansible for network automation
This drill mixes inventory settings, ios_command and ios_config behavior and idempotency, the same ground the playbooks above covered. Each answer comes with the reasoning:
CCNA practice: Domain 5: AI, and Network Operations and Management
10 questions drawn fresh from 66 in the bank, with the answer and explanation after each one.
Start 10 questionsConfiguration management with Ansible is one of the six topics in the AI and Network Operations and Management domain of the Cisco CCNA 200-301 v2.0 exam, which replaces v1.1 on 3 February 2027. The Domain 5 practice test covers the others too, and the SNMP and syslog guide handles the two monitoring topics with real router output.
Fix the errors you will hit
Each error below came up in this lab, either by accident or because we provoked it to capture the message. They’re grouped roughly in the order you meet them: installing, connecting, then running tasks.
paramiko is not installed: No module named ‘paramiko’
A plain pip install ansible leaves you with no SSH library for network_cli, because ansible-core uses OpenSSH for Linux hosts and doesn’t depend on paramiko. The first task against a router fails at once:
R1 | FAILED! => {
"changed": false,
"msg": "paramiko is not installed: No module named 'paramiko'"
}
Install paramiko in the same environment as Ansible, with the cap from the install step if you have IOS 15 devices. The other option is ansible-pylibssh, which also needs ansible_network_cli_ssh_type set to libssh or auto in the inventory, but read the next entry before choosing it for old routers.
Incompatible ssh peer (no acceptable kex algorithm)
With paramiko 5 installed, every connection to an IOS 15 router dies during the SSH handshake:
R1 | FAILED! => {
"changed": false,
"msg": "Incompatible ssh peer (no acceptable kex algorithm)"
}
The plain OpenSSH client on the control node shows what the router offers, which is the quickest way to diagnose this on any device:
ssh [email protected]
All three methods on offer use SHA-1:
Unable to negotiate with 192.168.122.11 port 22: no matching key exchange method found. Their offer: diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
The paramiko 5.0.0 changelog lists those exact three methods as removed, along with SHA-1 RSA signatures, which is why paramiko<5 fixes it. We also tried ansible-pylibssh with its ansible_libssh_key_exchange_algorithms variable set to diffie-hellman-group14-sha1. That got past key exchange and then failed on ciphers, because this router offers only CBC ciphers and libssh’s client defaults include none of them. The lasting fix is on the device: an image whose SSH server offers SHA-2 key exchange and CTR or GCM ciphers, which IOS XE 17 does by default.
host key mismatch for 192.168.122.12
This one appeared after we rebuilt the lab. The routers came back with the same addresses and freshly generated RSA keys, so the keys stored on the control node no longer matched what the routers presented, and you get the same result when a router is replaced or its keys are regenerated:
R2 | FAILED! => {
"changed": false,
"msg": "host key mismatch for 192.168.122.12"
}
With host key checking at its default, Ansible records each router’s key in ~/.ssh/known_hosts the first time it connects, without asking, and refuses a key that changes afterwards. That is exactly what you want when the change is unexpected. When you know why it changed (a replaced router, regenerated keys), remove the old entries:
ssh-keygen -R 192.168.122.11
ssh-keygen -R 192.168.122.12
Each command confirms the entry it removed and keeps a backup of the file:
# Host 192.168.122.11 found: line 1
/home/jmutai/.ssh/known_hosts updated.
Original contents retained as /home/jmutai/.ssh/known_hosts.old
The next run records the new keys and connects normally. Don’t fix this by deleting known_hosts wholesale, because that also throws away the keys that would warn you about a real impostor on the management network.
command timeout triggered, timeout value is 30 secs
We hit this on a task with save_when: modified against a router that had not yet been saved since the lab was built. The configuration change itself went through, but the task failed after half a minute:
fatal: [R2]: FAILED! => {"changed": false, "msg": "command timeout triggered, timeout value is 30 secs.\nSee the timeout setting options in the Network Debug and Troubleshooting Guide."}
The cause showed on the router console. Because NVRAM held a configuration written by a different version of the system image, the save asks “Overwrite the previous NVRAM configuration?[confirm]” after the filename prompt. The save that ios_config sends answers only the filename prompt, and the session sits there until the timeout fires. Raising command_timeout in ansible.cfg would only make it wait longer. Save once with the save.yml playbook above (or by hand on the console), and save_when works from then on. The same timeout also means a command that is genuinely slow, which is the case where raising command_timeout is the right answer.
Failed to authenticate: Authentication failed
A wrong password in IOS_PASSWORD produces the same message twice over:
R1 | FAILED! => {
"changed": false,
"msg": "Failed to authenticate: Failed to authenticate: Authentication failed."
}
An empty variable fails exactly the same way as a wrong one, so don’t assume the password itself is the problem. Check these in order:
- The variable is exported in the shell you run Ansible from (
echo ${#IOS_PASSWORD}prints its length without showing it) - The username exists on the router with the privilege you expect
- The VTY lines use
login localor the AAA method list you think they do
Working from the control node side first is quicker, because each of those checks takes seconds and needs no console access to the router.
% Invalid input detected at ‘^’ marker
Sending a configuration command through ios_command fails, because the module runs everything at the privileged EXEC prompt:
ansible R1 -m cisco.ios.ios_command -a "commands='interface GigabitEthernet2/0'"
IOS rejects the line just as it would if you typed it at R1#, and Ansible passes the raw reply through:
R1 | FAILED! => {
"changed": false,
"msg": "b\"interface GigabitEthernet2/0\\r\\ninterface GigabitEthernet2/0\\r\\n ^\\r\\n% Invalid input detected at '^' marker.\\r\\n\\r\\nR1#\""
}
The ios_command documentation says plainly that it does not support configuration mode. Move the line into ios_config with parents, or into a resource module, and keep ios_command for reading state and for EXEC actions such as ping and copy. That split, together with lines written exactly as IOS prints them, prevents most of the surprises in this guide.