Thursday, April 20, 2017

SSH into Cisco routers and switches with Ansible - step by step guide


Host File Example


Location: /etc/ansible/hosts
Copy and paste the following to your ansible host file

# A collection of Cisco devices                          

##IP Address
[cisco]
sw1 host=1.1.1.1

##Python Version
[all:vars]
ansible_python_interpreter=/usr/bin/python2.7
ansible_connection=local

[local]
localhost

##Credentials to login to Cisco devices
[cisco:vars]
device_type=cisco_ios
username=test
password=test

Playbook Example

Location: /etc/ansible/test.yml
Copy and paste the following to your ansible yml test file

---
- name: Show Run                
  hosts: cisco
  vars:
    creds:
      host: "{{ host }}"
      username: "{{ username }}"
      password: "{{ password }}"
      auth_pass: "{{ password }}"
      authorize: yes
  tasks:
    - ios_command:
        provider: "{{ creds }}"
        commands: show run  

      register: show_run

    - debug:
        msg: "{{ show_run.stdout[0] }}"

Once you have host file and playbook setup completed, run the following command
ansible-playbook /etc/ansible/test.yml -vvv -c ssh

No comments:

Post a Comment