Ansible Project: Network Security Audit 2 – VTY Configuration

This test is designed to see if the VTY configuration is on the device. If there are any extra commands that are not part of the confirmed configuration, they will be removed. This is specifically for configuration items in the confirmed configuration. Anything outside this configuration scope will not have any changes made.

The desired outcome will be for only the VTY configuration below to be applied to each device. Any other VTY commands are to be removed.
As a note, only VTY line 0 4 are used as my GNS3 lab has older switches that do not have anymore. I decided to use a standard and not have a variation.

All the commands in this post build upon my previous Ansible posts.
The playbook is available in my Github.
And the project for this can be found here.

For this set of testing, I have added another router that contains a switching module. The switching module is not important for this playbook, but will be used in the future.

Configure VTY Lines

Before any configuration I need to know what is already configured on the device. If Ansible is able to SSH to the device it is safe to assume that the VTY lines have some form of configuration already applied.

The first thing to do is declare what configuration is to be applied to the devices. The command “line vty 0 4” is not required as this will be in the playbook as a parent command. Without the parent command each command is entered into the global configuration resulting in errors for all the VTY configuration.

The next thing to do is to get the running config. I have used the command show run | b vty, although newer and all of my devices do support the section command.

Next task is to apply only the configuration that is not in the show running output. Nothing is being removed right now.

This tasks logic is;
– For each item in the vty_config variable
– Check if it is in the show run | b line vty output
– If it is missing, apply it

I have included a diagram and explanation with the logic as I don’t think it’s very readable.
1. Start when condition
2. Item in the vty_config list
3. Is not in
4. pre_vty_output list
5. Apply item as a configuration line

Remove Unwanted VTY Configuration

This next task is to remove any unwanted VTY configuration. The original VTY config must remain so we do not lose connectivity to the device. Any line that is not defined in the vty_config variable is to be removed.
There is a caveat to this, as I am running a “show run | begin” command to see the VTY configuration in the output there are extra lines such as “!” and “end”.
To filter these out I have used a second variable list.

Using “show run | section” would eliminate this, but old Cisco devices do not have the section command.
This can result in a big list of strings that are to be ignored. It’s ok for my lab.

The task is designed to compare the confirmed configuration against what is already on the device. If there is configuration applied to the VTY lines that is not in either vty_config or vty_config_ignore variable then it will be removed.

This tasks logic is;
– For each item in the show run | b line vty output
– Check if it is in the either vty_config or vty_config_ignore variables
– If it is not in either, remove the line

For this I have used “| trim” to remove the white space from the output from the show command. The Cisco output has a space at the start of each line except for “line vty”.
The white space was causing problems when comparing items of the show output to the config variables.
| trim removes white spaces from the start and end of the list item.

Printing Output When Changed

The last task is to print the new output, but only when there has been a configuration change.

Testing

Test 1 – Add “Width 200”

This test will add in the “width 200” command to the VTY lines on a single router only. The current configuration is missing the width line.

The output of this task. Only the single width line has been added. The new running config is displayed at the end. If this playbook were to be rerun then everything would be skipped except the checking of the initial configuration.

Test 2 – Remove “Width 200”

This task is the opposite to task 1. The configuration line “width 200″ will be removed”

The output has removed the line “width 200” and printed the new running configuration output. If this task were to be rerun then there would be no changes, and all tasks skipped except the initial show output for the VTY lines.

Test 3 – Add/Remove “Width 200” – Multiple Devices

For this test I have added in a second router to test the play against. I have added the IP of R4 (172.16.1.125) to the inventory under [lab_core].

This is basically a rerun of test1, but with a second router. The output is as expected, just more of it.

Leave a Comment

Your email address will not be published. Required fields are marked *