Hands-On Enterprise Automation with Python.

Book description

Invent your own Python scripts to automate your infrastructure

About This Book
  • Make the most of Python libraries and modules to automate your infrastructure
  • Leverage Python programming to automate server configurations and administration tasks
  • Efficiently develop your Python skill set
Who This Book Is For

Hands-On Enterprise Automation with Python is for system administrators and DevOps engineers who are looking for an alternative to major automation frameworks such as Puppet and Chef. Basic programming knowledge with Python and Linux shell scripting is necessary.

What You Will Learn
  • Understand common automation modules used in Python
  • Develop Python scripts to manage network devices
  • Automate common Linux administration tasks with Ansible and Fabric
  • Managing Linux processes
  • Administrate VMware, OpenStack, and AWS instances with Python
  • Security automation and sharing code on GitHub
In Detail

Hands-On Enterprise Automation with Python starts by covering the set up of a Python environment to perform automation tasks, as well as the modules, libraries, and tools you will be using.

We'll explore examples of network automation tasks using simple Python programs and Ansible. Next, we will walk you through automating administration tasks with Python Fabric, where you will learn to perform server configuration and administration, along with system administration tasks such as user management, database management, and process management. As you progress through this book, you'll automate several testing services with Python scripts and perform automation tasks on virtual machines and cloud infrastructure with Python. In the concluding chapters, you will cover Python-based offensive security tools and learn how to automate your security tasks.

By the end of this book, you will have mastered the skills of automating several system administration tasks with Python.

Style and approach

This book will follow a practical approach to help you script and automate tasks using Python.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Enterprise Automation with Python
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Setting Up Our Python Environment
    1. An introduction to Python
      1. Python versions
        1. Why are there two active versions?
        2. Should you only learn Python 3?
        3. Does this mean I can't write code that runs on both Python 2 and Python 3?
      2. Python installation
    2. Installing the PyCharm IDE
      1. Setting up a Python project inside PyCharm
    3. Exploring some nifty PyCharm features
      1. Code debugging
      2. Code refactoring
      3. Installing packages from the GUI
    4. Summary
  7. Common Libraries Used in Automation
    1. Understanding Python packages
      1. Package search paths
    2. Common Python libraries
      1. Network Python Libraries
      2. System and cloud Python libraries
    3. Accessing module source code
      1. Visualizing Python code
    4. Summary
  8. Setting Up the Network Lab Environment
    1. Technical requirements
    2. When and why to automate the network
      1. Why do we need automation?
    3. Screen scraping versus API automation
    4. Why use Python for network automation?
    5. The future of network automation
    6. Network lab setup
    7. Getting ready – installing EVE-NG
      1. Installation on VMware Workstation
      2. Installation over VMware ESXi
      3. Installation over Red Hat KVM
      4. Accessing EVE-NG
      5. Installing EVE-NG client pack
      6. Loading network images into EVE-NG
    8. Building an enterprise network topology
      1. Adding new nodes
      2. Connecting nodes together
    9. Summary
  9. Using Python to Manage Network Devices
    1. Technical requirements
      1. Python and SSH
      2. Paramiko module
        1. Module installation
        2. SSH to the network device
      3. Netmiko module
        1. Vendor support
        2. Installation and verification
        3. Using netmiko for SSH
        4. Configuring devices using netmiko
        5. Exception handling in netmiko
        6. Device auto detect
    2. Using the telnet protocol in Python
      1. Push configuration using telnetlib
    3. Handling IP addresses and networks with netaddr
      1. Netaddr installation
      2. Exploring netaddr methods
    4. Sample use cases
      1. Backup device configuration
        1. Building the python script
      2. Creating your own access terminal
      3. Reading data from an Excel sheet
      4. More use cases
    5. Summary
  10. Extracting Useful Data from Network Devices
    1. Technical requirements
    2. Understanding parsers
    3. Introduction to regular expressions
      1. Creating a regular expression in Python
    4. Configuration auditing using CiscoConfParse
      1. CiscoConfParse library
      2. Supported vendors
      3. CiscoConfParse installation
      4. Working with CiscoConfParse
    5. Visualizing returned data with matplotLib
      1. Matplotlib installation
      2. Hands-on with matplotlib
      3. Visualizing SNMP using matplotlib
    6. Summary
  11. Configuration Generator with Python and Jinja2
    1. What is YAML?
      1. YAML file formatting
        1. Text editor tips
    2. Building a golden configuration with Jinja2
      1. Reading templates from the filesystem
      2. Using Jinja2 loops and conditions
    3. Summary
  12. Parallel Execution of Python Script
    1. How a computer executes your Python script
    2. Python multiprocessing library
      1. Getting started with multiprocessing
      2. Intercommunication between processes
    3. Summary
  13. Preparing a Lab Environment
    1. Getting the Linux operating system
      1. Downloading CentOS
      2. Downloading Ubuntu
    2. Creating an automation machine on a hypervisor
      1. Creating a Linux machine over VMware ESXi
      2. Creating a Linux machine over KVM
    3. Getting started with Cobbler
      1. Understanding how Cobbler works
      2. Installing Cobbler on an automation server
      3. Provisioning servers through Cobbler
    4. Summary
  14. Using the Subprocess Module
    1. The popen() subprocess
    2. Reading stdin, stdout, and stderr
    3. The subprocess call suite
    4. Summary
  15. Running System Administration Tasks with Fabric
    1. Technical requirements
    2. What is Fabric?
      1. Installation
      2. Fabric operations
        1. Using run operation
        2. Using get operation
        3. Using put operation
        4. Using sudo operation
        5. Using prompt operation
        6. Using reboot operation
    3. Executing your first Fabric file
      1. More about the fab tool
      2. Discover system health using Fabric
    4. Other useful features in Fabric
      1. Fabric roles
      2. Fabric context managers
    5. Summary
  16. Generating System Reports and System Monitoring
    1. Collecting data from Linux
      1. Sending generated data through email
      2. Using the time and date modules
      3. Running the script on a regular basis
    2. Managing users in Ansible
      1. Linux systems
      2. Microsoft Windows
    3. Summary
  17. Interacting with the Database
    1. Installing MySQL on an automation server
      1. Securing the installation
      2. Verifying the database installation
    2. Accessing the MySQL database from Python
      1. Querying the database
      2. Inserting records into the database
    3. Summary
  18. Ansible for System Administration
    1. Ansible terminology
    2. Installing Ansible on Linux
      1. On RHEL and CentOS
      2. Ubuntu
    3. Using Ansible in ad hoc mode
      1. How Ansible actually works
    4. Creating your first playbook
    5. Understanding Ansible conditions, handlers, and loops
      1. Designing conditions
      2. Creating loops in ansible
      3. Trigger tasks with handlers
    6. Working with Ansible facts
    7. Working with the Ansible template
    8. Summary
  19. Creating and Managing VMware Virtual Machines
    1. Setting up the environment
    2. Generating a VMX file using Jinja2
      1. Building the VMX template
      2. Handling Microsoft Excel data
      3. Generating VMX files
    3. VMware Python clients
      1. Installing PyVmomi
      2. First steps with pyvmomi
      3. Changing the virtual machine state
      4. There's more
    4. Using Ansible playbook to manage instances
    5. Summary
  20. Interacting with the OpenStack API
    1. Understanding RESTful web services
    2. Setting up the environment
      1. Installing rdo-OpenStack package
        1. On RHEL 7.4
        2. On CentOS 7.4
      2. Generating answer file
      3. Editing answer file
      4. Run the packstack
      5. Access the OpenStack GUI
    3. Sending requests to the OpenStack keystone
    4. Creating instances from Python
      1. Creating the image
      2. Assigning a flavor
      3. Creating the network and subnet
      4. Launching the instance
    5. Managing OpenStack instances from Ansible
      1. Shade and Ansible installation
      2. Building the Ansible playbook
        1. Running the playbook
    6. Summary
  21. Automating AWS with Boto3
    1. AWS Python modules
      1. Boto3 installation
    2. Managing AWS instances
      1. Instance termination
    3. Automating AWS S3 services
      1. Creating buckets
      2. Uploading a file to a bucket
      3. Deleting a bucket
    4. Summary
  22. Using the Scapy Framework
    1. Understanding Scapy
    2. Installing Scapy
      1. Unix-based systems
        1. Installing in Debian and Ubuntu
        2. Installing in Red Hat/CentOS
      2. Windows and macOS X Support
    3. Generating packets and network streams using Scapy
    4. Capturing and replaying packets
      1. Injecting data inside packets
      2. Packet sniffing
      3. Writing the packets to pcap
    5. Summary
  23. Building a Network Scanner Using Python
    1. Understanding the network scanner
    2. Building a network scanner with Python
      1. Enhancing the code
      2. Scanning the services
    3. Sharing your code on GitHub
      1. Creating an account on GitHub
      2. Creating and pushing your code
    4. Summary
  24. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Enterprise Automation with Python.
  • Author(s): Bassem Aly
  • Release date: June 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788998512