Network Automation

Learning to Use Booleans

1. What are Booleans?

It is a data type that will return the value of True or Fale and that is it.

2.1 The and Operation

You can write a if statement based on the Boolean values. Example below if the router is up AND config is applied then print a resulting statement, and if both Boolean values are not TRUE then print another statement.

2.2 The or Operation

Now this example is based on whether one OR the other Boolean statement is true, so you can have one or the other such as if WAN1 link is up (True) but WAN2 link is down (False) then print a statement.

2.3 The not Operation

This NOT keyword does the opposite of what is already set on the script. So if it is True and you apply ‘not’ then it will make it false. In the example below the ‘not’ keyword checks ‘WAN1’, and ‘WAN1’ is set to true but with NOT, this changes it to False. Now because WAN1 is now set to False and WAN2 is True, the conditions do not match(because of the AND), therfore the ‘if’ statement will return “both Transports are goosed”. In order to print “We have one Transport working” we would need WAN1 configured as False then when we call the ‘if’ and ‘not’ keyword if will flip to True and then it matches WAn2 of True. In other words the conditions have to match. I.e True an and True or False and False.

Another example of the NOT keyword: Below example is set primary as false and backup as true. The NOT keyword is used in the if statement so primary changes to True. Next it will check ‘backup’ which is True so if primary is true AND backup is True then print the statement”Switching to backup link”.

Another example with Boolean data set:

3. Boolean Comparisons

Another example:

3.2 Inequality (!=)

3.3 Greater Than (>) and Less Than (<)

5. Using Booleans in Conditional Statements

5.1 if, elif, and else Statements

Int his example, the ‘elif’ statement is basically saying if it is 50% or less but less than 75% then display the message “Memory usilisation is Moderate”. In the script the ‘memory’ is 80% therefore it skips this condition.

6. Combining Multiple Conditions

You can combine multiple Boolean statements together such as ‘and’, ‘or’ and ‘not’.