APNIC 56 Network Automation tutorial
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Access VyOS via ansible

This module is to access your router and ensure Ansible can connect to it.

Step 1 - SSH your router and ensure you can log in. You can find access details here. In case you have trouble accessing it, please contact the lab instructor.


Step 2 - Install software-properties-common, add ansible repository and install Ansible

sudo apt install software-properties-common -y

sudo apt-add-repository ppa:ansible/ansible

sudo apt install ansible -y

Step 3 - Configure Ansible

vi /etc/ansible/ansible.cfg

add following content on the file.

[defaults]
host_key_checking = False

Step 4 - Create an inventory file with the details

vim inventory
[routers]
R01 ansible_host=R01
R02 ansible_host=R02
R03 ansible_host=R03 

[routers:vars]
ansible_connection=ansible.netcommon.network_cli ansible_network_os=vyos.vyos.vyos ansible_user=anurag

Next, install vyos collection

ansible-galaxy collection install vyos.vyos

Next, install python3-pip

sudo apt install -y python3-pip

Add the module - ansible-pylibssh

pip install ansible-pylibssh

Step 4 - Ensure Ansible is able to connect to router, execute a basic command on remote to ping 8.8.8.8

ansible -i inventory -m vyos.vyos.vyos_command -a 'commands="ping 8.8.8.8 count 5"' R01

The response of above ping module should a reply in ping (like example below):

ansible -i inventory -m vyos.vyos.vyos_command -a 'commands="ping 8.8.8.8 count 5"' R01
R01 | SUCCESS => {
    "changed": false,
    "stdout": [
        "PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.\n64 bytes from 8.8.8.8: icmp_seq=1 ttl=119 time=4.93 ms\n64 bytes from 8.8.8.8: icmp_seq=2 ttl=119 time=4.72 ms\n64 bytes from 8.8.8.8: icmp_seq=3 ttl=119 time=4.73 ms\n64 bytes from 8.8.8.8: icmp_seq=4 ttl=119 time=4.75 ms\n64 bytes from 8.8.8.8: icmp_seq=5 ttl=119 time=4.76 ms\n\n--- 8.8.8.8 ping statistics ---\n5 packets transmitted, 5 received, 0% packet loss, time 4007ms\nrtt min/avg/max/mdev = 4.719/4.777/4.926/0.075 ms"
    ],
    "stdout_lines": [
        [
            "PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.",
            "64 bytes from 8.8.8.8: icmp_seq=1 ttl=119 time=4.93 ms",
            "64 bytes from 8.8.8.8: icmp_seq=2 ttl=119 time=4.72 ms",
            "64 bytes from 8.8.8.8: icmp_seq=3 ttl=119 time=4.73 ms",
            "64 bytes from 8.8.8.8: icmp_seq=4 ttl=119 time=4.75 ms",
            "64 bytes from 8.8.8.8: icmp_seq=5 ttl=119 time=4.76 ms",
            "",
            "--- 8.8.8.8 ping statistics ---",
            "5 packets transmitted, 5 received, 0% packet loss, time 4007ms",
            "rtt min/avg/max/mdev = 4.719/4.777/4.926/0.075 ms"
        ]
    ]
}

Once successful, proceed with next module of running basic tasks for VyOS setup via Ansible. Instructions here.