Learning paths › Automation › Readable code, branching and loops
Readable code, branching and loops
Branching and looping as a script grows: if and elif deciding from what the device reported, while with break, continue and a loop else, iteration without managing an index, a dictionary walked as keys, values or items, and ranges counted up or down — then values looked up by a name that means something, dictionaries merged and emptied, and finally working code wrapped in a function, with a Netmiko login turned into one reusable call whose arguments are named. Explanation and checks; seven written exercises.
Lessons
- Split one long statement across lines
- Run code only when a condition holds
- Decide what to do from what the device reported
- Choose between several outcomes with elif
- Repeat until a condition stops being true
- Loop forever, and break out on an event
- Skip one iteration without ending the loop
- Walk a list by index
- Run cleanup only when the loop finished normally
- Iterate without managing an index
- Loop over keys, values, or both
- Filter records as you iterate them
- Generate a sequence of numbers
- Count downwards with a negative step
- Look values up by a name that means something
- Read, add and update entries by key
- Merge two dictionaries and handle collisions
- Delete a key safely and empty a dictionary
- Walk keys, values and items as views
- Wrap working code in a function
- Pass values in as arguments
- Order matters: positional arguments
- Turn your Netmiko login into a reusable function
- Name your arguments so calls read clearly
More in Automation
- First scripts that configure devices
- NAPALM: structured data across vendors
- Concurrency, and the Python types device output returns
- Parsing show output and testing conditions
- Build the workstation and the toolchain
- Files, CSV, JSON and pickle
- Paramiko: SSH to devices from Python
- Netmiko at fleet scale
- Tuples, sets and dictionaries for device records
- Exceptions and your first classes
- Plan and build the automation lab
- Files, formatting and regular expressions
- Addresses, functions, modules and classes
- CSV inventories, logging and scheduling
- Netmiko, SCP and structured parsing
- Agents, async and model-driven workflows
- NETCONF, YANG and RESTCONF
- NX-API, pyATS and Genie
- A small GenAI app, end to end
- macOS, Linux and the virtual lab
- Agile delivery and Python fundamentals
- REST APIs, authentication and requests
- Flask, Docker and Jenkins
- Managing a router with NETCONF and RESTCONF
- Ansible: inventory, ad-hoc commands and first playbooks
- Roles and zero-touch provisioning for Linux switches
- Installing the controller and applying a project
- Setting up Python 3
- The Ansible lab and the controller
- Vault, variables and config changes
- Parsing output and knowing which variable wins
- Jinja filters and PAN-OS automation
- Loops, callbacks, strategy and run control
- Roles, imports and writing your own module
- Running playbooks from AWX