Transcript for:
Python for DevOps Engineers - Introduction and Setup

hello everyone my name is abishek and welcome back to my Channel today is a very first episode of python for devops engineers and in this video we will learn about some really interesting topics such as why python is used by devops engineers then we will understand the differences between shell scripting and python scripting after that we will learn some realtime use cases of python by Dev Engineers finally I'll teach you how to install Python and run your very first Python program whether you're on Windows Linux or Mac OS I'll share some useful tips so that you will be able to run your first Python program in just a couple of minutes so please watch this video till the end okay now before going into today's video and deep diving into that particular topic let me tell you you that this is the GitHub repository that I am going to use throughout this series that is right from day one to day 19 whatever notes that I want to share with you people the programs that I'm going to write interview questions everything will be uploaded to this GitHub repository not just during this series let's say after 6 months 3 months I want to update few things so that it is useful for you people I will update in the same GitHub repository so just start this GitHub repository so that you will get updates as I make some changes to this repository along with that there is also a readme file and in the readme file you have the complete course content that is the complete course syllabus right from day one to day 19 what we are going to cover in which order we are going to cover everything is available right so please start this repository so that you get the information okay now go going back and understanding the topic for today that is why python right I'm pretty sure most of you have this question right that abishek as a devops engineer why should I learn python so this question also comes from Shell scripting versus python right probably this also leads to this question now I understand that many people have this concern that is because you have already learned shell or you know you are confused should you start with shell scripting or should you start with python scripting which is more important than the other if I have to only learn one thing should I learn shell or python all of these questions I will answer in a very simple way so instead of just giving you one liner let me explain from the uh Basics right so why shell scripting is is very commonly used by devops Engineers so usually as a devops engineer you deal with Linux systems okay that is most of the machines that we use or you know the most of the machines that we use to deploy the software the applications right it is on the Linux machines because Linux is much safer when compared to Windows series uh Windows servers and they are very less vulnerability prone as well so that's why people prefer Linux and most of the applications are deployed on the Linux servers and because we are concerned with the deployment because we are you know the team who provides the infrastructure so on a day-to-day basis we deal with a lot of Linux machines and unlike Windows machines or you know your uh Mac OS machines which have a rich user interface right that means today I'm explaining this video on my Mac OS machine and I am writing things here I'm drawing things here I have open the browser and I'm showing this content to you right this is all possible because this has a very rich user interface the laptop that I'm using or the operating system that I've installed on the server whereas if you talk about the Linux systems right let's say you have created an ec2 instance or you have created a virtual machine on the Azure platform by default you know by default they do not come with a graphical user inter interace they don't come up with a GUI so that's why to interact with this Linux machines it can be to creating a file or opening browser and reading the content that is if you want to access any website and read the information on the website or creating some uh folders right learning about the free disk space on the uh Linux system or or uh the memory that is left on the Linux system or the uh you know the number of CPUs that are available on the Linux systems so any day-to-day task you want to perform on this Linux system right so you need to learn shell commands right why you need to learn shell commands because to interact with this uh Linux systems what you do is you SSH to those missiones SSH is a protocol through which you can connect from your personal machine to this Linux system it can be easy to instance VM on Azure or Google Cloud anything so you SSH to those instance and run the shell commands now what is the difference between shell commands and shell scripting it's very simple if I have to put this in a very very blunt way that is if I have to put this in a very very simple way if you create a file okay and provide the extension of the file as Dosh instead of providing the extension of that txt usually for the text files we have txt instead of that if you provide the extension of this file as Dosh that is you create this file with name test.sh or ABC Dosh or something and inside this file if you just write bunch of shell commands in order that is line number one line number two three four and five if you write five shell commands and you know using this command called do/ test.sh if you execute this file all the commands will be executed in the serial order that means let's say someone tells you that abishek log to a Linux machine SS to a machine then create a file in the machine then create a folder in it then try to get the free displace try to get the free memory try to get the free uh CPUs that are available in the machine now instead of running five commands and reading the output one after the other you can put all that Five Shell commands in a file and you can just execute the file using this command and it will print the output on the terminal right so this is about shell scripting that's it so the primary purpose of using shell scripting watch it very carefully when I'm talking I'm saying that this is the primary purpose I'm not saying this is the only purpose but the primary purpose of shell scripting by devops Engineers is to interact with Linux systems and to get some information from this Linux systems or you know to manage these Linux systems or you know to uh get some system related information from this Linux systems you use shell scripting or shell commands now if you are able to do all of these things then why would you need python right so for that I'll give you two fundamental reasons okay one reason second reason is very important but the first reason is that I told you that as a devops engineer most of the things that we interact with is Linux systems but I told you most of the systems I did not tell you all of the systems that we interact so there can be some instances that is window as well okay so if you are writing scripts on Linux machines only or if you're writing shell scripting let's say the task is uh the task that is given to you is to get some information both from Linux systems and windows machines then your shell scripting would not work so you know shell is only restricted to Linux whereas python can work on both Linux and windows now you might say that if you are a devops engineer you might say that but abishek I have anible for that right why would I need both uh Linux and sorry shell scripting and python I can simply learn anel which works on both of these things you know the foundation of anible is also python that is anible is written in Python now it is because many people don't know python so they can simply learn a templating language I will call anible as just a templating language because it a yaml scripting right so using anle you can run things both on Linux and python that's why I said this is only one reason and this is not the most important reason the most important reason is that when you want to perform some complex tasks okay when you want to write some complex programs you want to interact with apis I'll tell you use cases don't worry why you interact with apis or why you write complex programs I'll explain in a minute but when you want to write complex programs when you want to you know interact with the apis when you want to perform more of uh data manipulation okay so in such use cases python wins over shell scripting okay so if you ask me these things cannot be done using shell scripting you can definitely do it okay these days you can achieve things with any programming language or scripting language but python is designed to perform these activities in a very simpler way okay so two reasons that I've explained one reason is shell scripting is only designed for Linux systems but python can work both on Linux systems and windows machines but this is not the only reason or this is not the important reason because as a devops engineer you can avoid python using anible if you want to run some system related task or you know if you want to fetch some information both from Windows and Linux instead of python you can also use an but the main reason here is that when you want to write some complex programs that is from the scripting point of view or when you want to interact with the apis or when you want to perform data manipulation in such cases you will use Python now let me explain this right so here abishek you said apis you said complex task you said data manipulation but I don't understand what you're talking about don't worry let's say that there is a very simple task that is you see this GitHub repository right so as a devops engineer let's say you have some 20 30 repositories and now your task is to talk to this GitHub repository and list out all the issues that are created on this GitHub repository and who has created this issue okay for example there is this issue now who has created this issue so this is your task so you are asked to to talk to the API of a GitHub repository right API of GitHub and talk to the API that gives you issues on this GitHub repository and you have to get the author name from it got it so you have to make a API call to GitHub GitHub api. github.com isues something and using this API call you need to get the list of issues so what happens when you make an API call GitHub will give you a Json payload okay so it will give you a Json payload that is it will give you a very big uh syntax and from that you want to fetch the author name now you can do this using shell scripting as well as python scripting but the modules of python are rich okay and and python is designed to handle these kind of scenarios in a very simple way so if you are using shell scripting you would use Cur okay curl is a shell command to talk to the API of GitHub and then you would use probably JQ to pass the uh Json object that GitHub has given to you and you would get the required information right you can finally get the author details using shell scripting as well but if you're using python because python has a Json module you can serialize this object and it can be a complex Json object it can be thousand lenses 10,000 lines or you know it can be a nested Json object using python you can easily iterate through the Json object basically you serialize this you convert this into a dictionary you will understand going ahead I'm just explaining on a very high level right we have all of these topics covered don't worry and you will iterate to this dictionary and get information in like couple of lines in Python that is very very simple you can do it through shell scripting as well but shell scripting does not have Rich modules as python has right so I'll again explain you this in uh just one line so that you will understand this in a better way so basically the task that is given to you is instead of going through each and every repository that is if you want to get the issues in this repository what you need to do you have to log to GitHub account after that you have to go to this repository then you have to go to issues then you have to manually open each and every issue and see who has created this issue right let's say these are the steps that you have now instead of doing this manually you can automate this as a devops engineer because you might have 20 repositories 30 repositories so your goal would be to write a script right now let's say I'm not saying shell script or python script but your goal is to write a script that script should talk to the API of GitHub because whenever you are automating things you will either talk to API or CLI right so you will talk to the API of GitHub to fetch the issues so GitHub will give you back this information so when you ask about the issues what GitHub will do is it will give the entire list of issues in the Json format so now you have received the Json object from this Json object you have to fetch in your script what you need to do is you have to fetch the author details perfect so step one step two step three and step four you can do this using shell scripting as well as python scripting right now if you do this using shell scripting the problem is that for shell script first to interact with this uh API you would use a curl command and then GitHub would return you a Json so to pass the Json of course in Shell scripting you have a module called JQ but you know these modules are not as robust or they are not as rich as the python modules so in Python what you would do is again in Python also you have some modules like you would use the request module and then you would use the Json module or what you can also do is you can serialize this content in Python and using serialization you can convert that into a uh data type called uh dictionaries and from there you can easily fetch the content okay so if you are doing any complex activities like this you should choose python whereas if you are doing some system related activities that is if you want to fetch information of bunch of systems or you know if you want to to uh do some activities on that system create files folders or you know uh get uh information such as uh free disc on that system or mount a volume onto that system or you want to uh detach a volume you want to uh perform any of this monitoring related task then go with shell scripting right advanced concepts Advanced topics go with python scripting right I hope this is clear and I have explained this in the this particular file as well that is Shell versus python where what I have explained here when use shell scripting if you want to perform any system administration task or you know if you want to perform any text processing that is uh you know you get uh list of files you get log files right you have a very big log file and from the log file you want to get uh number of error messages from the log file you want to get uh number of uh info messages warning messages in such cases you can simply use shell scripting or you want to create any envirment variables or you want to get information from any envirment variables these are very simple tasks so go with shell scripting when use Python when you want to write any complex Logics or when you have crossplatform compatibility that is you might have uh Linux machines Windows machines or when you want to talk to the apis example I just gave you or you know you want to do some error handling that is when you are writing uh the Python scripts you have an option for handling the errors we will talk about it later and finally some Advanced data processing which again kind of uh things that I've explained if you want a Json object and if you want to address or uh in other fields where you want to talk to the csvs or you want to uh talk uh use large amount of data sets you want to train it in such cases also python is much preferred over shell script in but it is not from devops engineer point of view from devops engineer point of view bluntly saying simple task go with shell scripting advanc or complex task go with python scripting and what comes in the advanc OR python uh Advanced topics or what comes in the complex scripting talking to the apis right or performing operations on the nested Json objects talking to third party applications right whether you want to write uh server applications that is Lambda functions on AWS where you want to interact with another AWS resources when you want to interact with another AWS resources that itself means like let's say using Lambda function you want to talk to S3 bucket basically you're talking to S3 API right so more or less the API related activities we will use Python scripting right I hope this point is very very clear to each and every one of you and if you still have any questions do let me know in the comment section I'll definitely reply now after this what is the next thing that we are going to learn is how to install Python and run your very first Python program you might think that this is very very simple abishek I can simply install python but many times what I have noticed is there are a lot of students who want to do this or you know uh people don't have their personal laptops they're using your office laptops and they cannot install python or when they're using AWS account or Azure account sometimes their account is suspended blocked due to some payment issues so there is a very very simple technique that I'm going to tell you that is go to the GitHub repository you can go to this GitHub repository or any GitHub repository you know if you are following this series you will definitely Fork this GitHub repository right because you want to use the files in it or let's say you have created your own GitHub repository anything is fine there is something that is offered by GitHub which is called as Cod spaces right so just click on this place button and what you will do is GitHub will give you an instance so this is again a very uh simple instance that you get from uh Amazon or you get from Azure whatever it is right it's kind of a similar instance but here what Cod spaces does is it abstracts everything from you that is it does not provide any information it will just ask you tell me about the instance that you want that is how much resources you want it will only ask you about CPU and RAM because I have already created it is not asking me for that information because I've already created the Cod space right and in this environment once you tell that go with 2 CPU 2 GB Ram that is more than enough for Python and this instance will be provided for you for 60 hours Now 60 hours is a huge time because as I close this particular thing that means GitHub is not counting the usage of mine right and let's say your 60 hours is done you can use another GitHub account or you know if you don't have any issues with the machine that you are using that is on your Windows machine or Linux machine you don't have any problem then you can use that itself but going back to Cod spaces so it will give you 60 hours of free resource you can use this instance for 60 hours per month and next month you can again use for 60 hours and if you have multiple GitHub accounts then you can use 60 plus 60 plus 60 whatever it is right and the advantage here is that you don't have to bother about the installation at all right you will get a terminal and just let me increase the font okay just type python here hyen hyen version and you will notice that python is is already installed on this machine right so if you just go to the day one folder and uh just run python space day one I need to switch to the folder right day one followed by 02 hyen Hall world. py you will notice that the program is already executed so my python is installed and the program is also executed and how much time did it take just a couple of minutes so use this if you have any issues with your account being suspended your account being blocked or any particular thing now let's say for some reason you don't want to use code spaces then what you need to do so just take a new tab and search for python downloads don't go with any other website just go with the official python website that is download uh python so this is the page that you would see and inside this page there is a button called downloads click on that and depending upon your operating system just choose it let's say windows so click on the Windows button and here you have different versions of python go with the latest version there is no problem or go with python 3.10 as well in fact you can install any version because the scripts that I'm going to explain will work on any python version and now you have to be very careful about your system type most of of the windows machines use 64bit processor but you can go to the about section and see what exactly your machine is so mostly it is 64bit machines which we use and click on this particular thing so you will download that thing right and extract the zip click on the next next next button your python is installed and python is also set to path so that what you can do is if you are on a Windows machine open git bash if you don't get have git bash open command prompt but I will highly recommend you to use git bash or mobile ex right let's say you don't want to use mobile exterm puty or any other thing just install git bash okay so git bash will come up with a term will come with a terminal you can use that mostly issue is with people who have Windows because on Linux and Mac OS you can just install using the command line as well if you have Mac Mac OS you can just run the command called Brew space install space python or Python 3 and your installation will be successful there will not be any issues but with Windows people have some concerns that is uh python is not set to path or you know I'm not finding the right distribution all of these things so go with these steps if you're finding it difficult for some reason the path variable is not set or for some reason you know you are not able to install on your machine you have off his laptop restrictions in such cases you can use code spaces code spaces is just a couple of minutes you will able to run it and mostly for this course I will use code spaces and code spaces is a visual studio thing right so here uh this comes with a visual studio code so I will explain the entire thing here if you're using your personal laptop then install Visual Studio code on that particular laptop right it's very simple again just go to your browser search for visual studio code and in next classes I will explain you what plugins to install what extensions to install in the visual studio code but for today just have your python set up as I've shown in the video and execute your very first Python program so this is the assignment for today nothing more than this it's very very simple so I hope you'll all be able to complete this and you can revise the complete material that is available in the day one folder thank you so much see you all in the next video take care bye-bye