Practical Network Automation Leverage the power of Python and Ansible to optimize your network Abhishek Ratan BIRMINGHAM - MUMBAI Practical Network Automation Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: November 2017 Production reference: 1141117 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78829-946-6 www.packtpub.com Credits Author Copy Editor Abhishek Ratan Safis Editing Reviewer Project Coordinator Pradeeban Kathiravelu Judie Jose Commissioning Editor Proofreader Gebin George Safis Editing Acquisition Editor Indexer Prateek Bharadwaj Pratik Shirodkar Content Development Editor Graphics Abhishek Jadhav Tanya Dutta Technical Editor Production Coordinator Swathy Mohan Melwyn Dsa About the Author Abhishek Ratan has around 15 years of technical experience in networking, automation, and various ITIL processes, and has worked in various roles in different organizations. As a network engineer, security engineer, automation engineer, TAC engineer, tech lead, and content writer, he has gained a wealth of experience during the 15 years of his career. Abhishek also has a deep interest in strategy game playing, and if he is not working on technical stuff, he is busy spending time on his strategy games. He is currently working as a Sr Automation Engineer at ServiceNow, learning, and expanding his automation skills in the ServiceNow platform. His earlier experience includes working for companies such as Microsoft, Symantec, and Navisite,which has given him exposure to various environments. I would like to give special thanks to the entire editorial team that has helped me throughout this book, from correcting my typo mistakes to making me realize where the flow of content needs to be improved. I would also like to thank my family for being supportive and giving me ample time and support to make this book see the light of the day. About the Reviewer Pradeeban Kathiravelu is an open source evangelist. He is a PhD researcher at INESC-ID Lisboa/Instituto Superior Técnico, Universidade de Lisboa, Portugal, and Université Catholique de Louvain, Belgium. He is a fellow of Erasmus Mundus Joint Degree in distributed computing (EMJD-DC), researching a software-defined approach to quality of service and data quality in multi-tenant clouds. He holds a masters of science degree, Erasmus Mundus European Master in Distributed Computing (EMDC) from Instituto Superior Técnico, Portugal, and KTH Royal Institute of Technology, Sweden. He also holds a first class bachelor of science of engineering (Hons) degree, majoring in computer science and engineering from the University of Moratuwa, Sri Lanka. His research interests include software-defined networking (SDN), distributed systems, cloud computing, web services, big data in biomedical informatics, and data mining. He is very interested in free and open source software development and has been an active participant of the Google Summer of Code (GSoC) program since 2009, as a student and mentor. He has authored Python Network Programming Cookbook - Second Edition and has also reviewed two Packt books on OpenDaylight. I would like to thank Prof. Luís Veiga, my MSc and PhD advisor for his continuous guidance and encouragement throughout my time at Instituto Superior Técnico. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1788299469. If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! Table of Contents Preface 1 Chapter 1: Fundamental Concepts 6 Network automation 6 DevOps 7 Software-defined networking 8 OpenFlow 9 Program concepts 10 Variables 11 Data types 11 Decision makers 13 Loops 15 Arrays 16 Functions 17 Best practices 18 Readability of a program 18 Support information 20 Indentation 20 Sample best practice example 21 Language choices (Python/PowerShell) 22 Writing your first program 24 PowerShell IDE 25 Python IDE 27 Representational State Transfer (REST) framework 29 Summary 33 Chapter 2: Python for Network Engineers 34 Python interpreter and data types 34 Conditions and loops 39 Nested and multiple conditions 41 Loops 43 For next loop 43 While loop 45 Writing Python scripts 46 Functions 48 Passing arguments from the command line 54 Python modules and packages 55