Network Automation

Understanding the Principle of Containment

1. The in and not in Operators

You can use the ‘in’ or ‘not in’ to check a specific parameters.

1.1 Using in with Lists

Example below I create a list called ‘devices’ and then I create a variable ‘check’ which has ‘Router1’ in the variable.
I then create a ‘if’ statement to check if ‘check’ (Router1) is in the list called ‘devices’ and if it is then print Router1 is in the network else Router1 is not in the network.

Another example below:

1.2 Using not in with Lists

This time you can have the ‘not in’ statement. The below example is basically checking if ‘Router69’ is not in the list called ‘devices’ then print a statement to say Router69 is not in the network.

2. Using in and not in with Dictionaries

2.1 Checking Keys in a Dictionary

The example below is the same as the above example 1.1 using lists. but this time we check the key in a disctionary.

2.2 Checking Values in a Dictionary

To check the value in a dictionary, you use the ‘.values()’ hihglighted in PINK.

3. Using in and not in with Sets

3.1 Checking Membership in a Set

The example below is showing as LIST instead of SET. It should be a set beginning with { and end with }.

3.1 Checking Membership not in a Set

4. Practical Use Cases in Network Automation

4.1 Verifying Commands Before Execution

Example below has a list of commands in the list, if the command ‘show cdp nei’ is not on the list then print it is not allowed to be executed.

Another example: