DevOps Tools - Python for DevOps Automation
What is Python Programming?
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python is often described as a "batteries included" language due to its comprehensive standard library.
Why Python is so popular in Devops?
Along with the growth of Python for developers in the machine learning and data science space, Python is also a growing language for devops / in the ops tooling side.
To understand why Python has been chosen you need to look at the environments and backgrounds of the people that have traditionally done ops works.
For many years *nix systems and their operators have had the mindset of automating their works flows, a good sysadmin is a lazy sysadmin. This has been supported through the ability to code in the shell, bash being the default on many systems for many years.
The ability to string together your shell commands into script files then run them time and time again was a very powerful thing. The difficulty was that trying to match what you get from modern languages is sometimes difficult and messy, so alternatives were sought. This is where Python stepped in.
Why Python?
- Its ubiquity and the fact it has been used for many years by OS providers gave admins the same warm fuzzy feeling as bash: they knew that they could write their scripts locally and that it should work anywhere, saving the need for individual scripts for all the different systems.
- Python is easy to read and learn. It’s also easy to copy, paste and run. Sometimes things just need to get done and Python allows you to jump into some complex things without the need to fully understand everything that’s happening. I’m not saying that’s a good thing, but if your site is down and the boss is screaming, sometimes the ability to just google, copy, paste and run, knowing you stand a good chance of achieving what you want, is just enough.
- No need for object oriented programming. No structured coding required either. You can go straight into grabbing what you want and doing the work. Much like shell scripting (PyBites addition - related Pycon 2018 talk: Solve Your Problem With Sloppy Python).
- Batteries are mostly included but if not, there is likely a module out there. If you need to poke at a network, no problem, try netaddr. Need to grab data from a website? Use BeautifulSoup. Make a simple dashboard/data visualization with Matplotlib or Bokeh. Python can do it all!
Big tools, same language
Beyond the day-to-day of ops work there are some big tools based on Python. Infrastructure management has Ansible and Saltstack, both written in Python. AWS's default command line tool is built in Python. Supervisor can be used to control services on systems. Or take pytest's testinfra which is specifically used for testing infrastructure deployments. Finally, we have openstack, the open source cloud platform. Many of the components and management of these tools use Python underneath!
The use of Python in many other areas, and the fact that devops is focused on breaking down silos and working across previously fixed boundaries, has also helped in increasing its use. If your data scientists and your ops guys are using it, the ability to talk and use code across disciplines is very useful.
Compared with other languages
While Python is certainly a leading choice, there is still the case for Ruby, where tools like Chef and puppet are used. What both languages (as well as JS) can suffer from though is the need to install additional packages. I feel Python is a lot better as you can get most things done with the standard library. However external modules will often allow you to do things in an easier way, requests being the perfect example.
This difficulty has moved some developers into using Golang, which has many of the benefits of Python, but in addition can produce a deployable component fast and capable of running on anything. This is certainly another language to look out for.
Select the language of your preference