Search code examples
ansibleansible-inventorycisco-ios

Ansible Cisco Config Terminal


I have a problem with Cisco Routers. I can't open a config terminal because it's saying always invalid input. I done everything what are docs saying. Inventory file:

all:
  vars:
      ansible_connection: network_cli
      ansible_network_os: ios          
      ansible_user: user
      ansible_ssh_pass: pass
      ansible_become: yes      
      ansible_become_method: enable
      ansible_become_password: pass
      ansible_python_interpreter: python
      accept_hostkey: yes 
      ansible_host_key_checking: false
  hosts: 
    testcisco:
      ansible_host: ip

Playbook:

---
- hosts: all
  ignore_errors: yes
  gather_facts: no
  tasks:
    - name: Config
      cisco.ios.ios_command:
        commands: "configure terminal"
      register: output
    - debug:
        msg: "{{ output }}"

Output:

fatal: [testcisco]: FAILED! => {"changed": false, "msg": "configure terminal\r\nconfigure terminal\r\n   ^\r\n% Invalid input detected at '^' marker.\r\n\r\n------#"}

No matter what i type config terminal, conf t etc. It's always saying invalid input


Solution

  • For some reason, Ansible is deciding to run two instances of conf t, which is strange. However, you don't nececarily need to be going into global config mode inside of Ansible, see this documentation: https://docs.ansible.com/ansible/latest/collections/cisco/ios/ios_config_module.html

    and specifically this part

    - name: configure top level configuration
      cisco.ios.ios_config:
        lines: hostname {{ inventory_hostname }}
    
    - name: configure interface settings
      cisco.ios.ios_config:
        lines:
        - description test interface
        - ip address 172.31.1.1 255.255.255.0
        parents: interface Ethernet1

    You can see you can do most/all configuration changes you need inside of the playbook itself.