How to create Ansible Module | Hands on Demo and Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome back to the channel in the past few sessions we have learnt a lot about modules and the ways we can use modules for our automation but what if we want to create our own ansible modules so can we do that let's see if that's a possibility but before moving forward please listen to this important message during these testing times i request you to please stay at home stay safe if you have to step out please wear a mask follow social distancing norms wash your hands and if you feel sick stay at home and follow proper medication by following these rules we can all be on the winning side so when we wrote a playbook previously we made use of the existing modules and parameters to execute the operation that is what we used to make our automation better so let's understand or let's try and understand this playbook and let's clarify these terms so the first one is hyphen name setup nginx server on my server list so this is our name string the second point host colon my servers so this is the host group that makes it a pattern as well so this is the host group and within all the first group all the hosts that are present on those machines that are available as a part of that host group the third one is become cool and true as i said it is used for elevating privileges you can use become as true or yes and the fourth one task next we actually are defining the task using this task parameter and after that we have hyphen name and then we have the ansible module that is command here the command module helps you to execute commands on the shell prompt or as well as on the windows powershell but we use windows command that's a separate thing but that is basically the operation that the command module does for us but along with that it takes the task keyword that is basically our arguments the next thing that we have done is we have given the parameter as well that is creates and we have given the path that is the path string so all these if you see we are having a collection of statements that are able to execute a certain operation so this ansible module that we have here will be used to install applications execute commands and operations on the host which are a part of your host group so that's what we are seeing here so this is the playbook that we are writing we are using the command module and when we try to execute this from our ansible master onto the host group that we have here so it gets executed on system a system b system c system d so that is how it gets propagated but our main focus for today is let's suppose if i have a custom requirement to create a specific module then how to do that for that you need to come to ansible module development getting started so this module will provide you all the basic requirements and the prerequisites that you need to create your own ansible module so first thing for us is to set up an environment and for that i need a ubuntu machine and we'll install the dependencies that we have here in the documentation so let's go ahead and create amazon instance for ubuntu so this is the amazon ec2 console you have launch instances here just click on that you have the ubuntu server yeah i don't have any problem that you have to do it with ubuntu i'm just following the documentation you can do it in any other operating system as well so don't think that it can only be done using ubuntu you can do it with red hat and amazon linux 2 as well so there is no harm in doing anything in any other operating systems just make sure that you follow the protocols and the documentation for you to achieve the same results that we are achieving here so we have the amazon server 20.04 lts version and 64 bit x86 architecture that's what i'm going to select select that and we'll quickly create an instance so let's wait for it to get started let's copy this public ipv address and we'll try and connect to that particular instance remember the username for ubuntu machines will be ubuntu not ec2 user so don't get confused if it does not work just give the username as ubuntu and just save it that's it so this is the one that we are using right now so just add and continue so you might be thinking that what we are trying to achieve here so the first thing that i want to tell you is that for everything that you want to achieve or you want to write let's suppose you're writing a code on python so for that you need python installed isn't it if you want to run it so just like that if you want to run ansible commands unit ansible to run it isn't it but for you to have your development environment set up you need the ansible core code itself and on that itself you are going to modify some of the files and you're going to add some of the files for your own customization and that's how it works so if you have your own ansible code or the root level code that you have then you can make changes to that and once you run that particular environment then you will be able to make use of the custom scripts that you have or the modules that you want to create we'll just see what is the present working directory here so home slash ubuntu and we'll come here and we'll clone the repository that's the first step and let's clone the repository for ansible so you will get the ansible code okay so now once your ansible code is cloned you can just do a ls here you will see the folder and sybil and within which you have all the ansible code that you need to run your own version of ansible the own version of henceable means if you have to customize something if you want your own code to be present in one of the command modules or anything that you want to customize you can do it here it will not affect the overall environment that you have so now what it is telling is change the directory into the repository root create a virtual environment so what i am going to do i am going to do cd ansible and here it tells me that i can do it like this so my main aim is that i want to create so i'm giving it my venv so if you have to create virtual environment in python 3 you can give the command python 3 hyphen m that's the module name the module name is benv and you can give the name of the virtual environment that you want to create and then just press enter okay so if this doesn't work then you have to install this kit let's install the virtual environment now once it is done just try to create this so if it did not give you any error then just do a ls here inside the ansible folder you will see a folder name that is my v and b will do a ls iphone yeah see this is the one that got created so you can do a yveenv and you can if you want to activate it what you need to do is you can just go inside when and you will have this activate you can do dot slash activate it will activate the virtual environment for you but let's come back to the documentation part so what it is selling next is you have to activate the virtual environment and you have to install the requirements.txt that we have here so for that what you need to do is it is telling dot slash dot so what it is saying is you can do dot v and v slash bin slash activate so for us it is my v and v right that's it so once it is activated you will see a placeholder here for that particular environment which tells you that your virtual environment is active right now and within this environment you can install any packages and that will be local to that particular virtual environment it will not affect your overall python installation and now what we have to do is we have to install the development requirements so this is the requirements.txt and it has a lot of files and it has a lot of modules so you can check that out there's a file you can do a cat here requirements.txt and you will see these are the modules that have to be installed here there's also that so in order to install this what you can do you can just type install iphone r requirements.txt and then just hit enter but before that i want to show you something like you can do pip freeze and see there are nothing installed here so this virtual environment does not have any packages installed so just make sure that you check it ahead of installing it one second now that that has been installed what you can do you can just do a papers again you see all the packages that were inside that requirements.txt have been installed now the next thing is to run the environment setup for each dev shell process you can just do this as well so hacking this folder is there hacking we have to activate this as well we just do environment setup okay so the path python path and everything has been set so that ansible can make use of it now your development setup or prerequisites are done the next thing that you have to do is write your code and whenever you want to come back and you want to start working on this once again you can just copy this same thing let's suppose i want to move out i have i want to restart this machine then i have to come back and start working again on that particular environment isn't it so what i can do i can just let's suppose i disconnected from this machine okay remove it close it and this is the machine that i was currently working on right isn't it so i'll just double click on that and if you see currently i don't have that prompt for virtual environment what i can do i can just do a cd cd ansible and within which i can actually type my vnv activate and hacking environment activate that's it so now this is also done and my virtual ac virtual machine oh sorry my virtual environment also has been activated so i'm good to go now so every time i come back and i restart the machine or anything that goes bad like if i want to restart the things once again i can just come back and execute the same command and i'll be good to go so here you have information like you can create your info modules and fact modules which gives you information about what the actual module is doing and you can have a documentation as well based on these info and fact modules but our main focus for today is if i have to create a module for myself so in order to create a new module navigate to the correct directory from your new module so within ansible there is a modules folder within which if i have to create a module i have to add my file there so lib ansible modules will go there so if i have to see here so we have lib inside which i have ansible inside which i'll have modules isn't it do a ls here and if you see most of the models that you are going to use in ansible will be present here itself see we have command module here if you open this and if you read the command module you will understand that it is just a python script it is telling you what to pass as an argument in the playbook and it will execute it based on the arguments that are provided there so similar to that we have to create our own module as well so to create your own new module you can just type whim my underscore test module as it is given there and you have to create a new file here ansible already has a predefined format and documentation so based on that you have to write your own modules as well so for that what ansible tells us is that you can paste the content below on your new module file and it includes the required ansible format on documentation a simple argument spec for declaring the module options and a sample example code so this code that you see here can be straight away used and we have to just modify this code to make a particular ansible module and that is as per the ansible standard so we'll not modify them we'll just tweak them for our requirement just copy this hit i just paste it here okay just see here now from the top so you have your shebang you have the copyright details the licensing module does you can write your own things here like short descriptions like this is my test module you can modify this there is no problem with that you can provide the information like what does it do what parameter it takes what argument it takes what type of argument it takes whether it should be a string or boolean you can provide everything here and you can provide an example code as well for users to use so this is the place where the actual execution happens so within this you have module arguments so here if you see it takes two arguments name and new you can tweak that based on your requirement so this just test module so when you pass a particular name here it is just going to return a string for you so that is just a basic purpose of this particular module this is just a test module so we'll just see how we can run this and then once we have executed this properly we will see how we can write our own custom module so you have this file now you can just save it by clicking on escape roll and double queue and if you do ls here you'll find your test module also here as well where did it go in ghaji yeah so here my test dot py what is the next thing that it tells us to do so if i have to execute my module locally what i can do is i can create a temp args chart json so there is a temp folder here so temp i can what i can do is i can create a args.json here logs.json and this will be my input file so i can just copy this let me just paste it here so these are the two things that my test module actually takes name and new as i told you the name actually is a string that is hello and new is a boolean that is true so just save it for now you can run the module and test it and the next thing that it is selling is you have to activate it and run the development environment these two has already been done and the next thing that we have to do is we can just run this module so what is the command that we are showing here so this is just a test command so python iphone m and simple.modules.mytest so my test is the module name and then it takes the argument from here so once you run this it will give you the basic formatting of what a particular result should look like okay so as i told you if you have made the new equals true then it will keep the status as or changed equal to true original message will be hello message is goodbye so that is the particular so that is the purpose of this particular module that once you pass a string here it will give you a message and the invocation will be module logs name hello and new equal to 2. so this is the basic input that have been provided this is just a simple execution so let's suppose i want to write a playbook for this so that option has already been given so i can go back to my ubuntu my home folder and i can create a playbook here test mode okay now i can just copy this and this playbook is very simple you have to provide a name and host is localhost it means that you are testing this particular module locally you're not going to install it on any other machine or any vm and we have two tasks here so first one is run the new module so here we are passing our own created module that is my underscore test that we created so that name of that particular module will be same as the python file name that we have provided there so my underscore test and then it takes name as an argument that is hello and new equals true and we are registering the output that we are collecting from this particular module in register that is our variable is test out and then we are dumping the test output to this particular debug message so that is as simple as it can get just save this so now we have the playbook so how to run a playbook we can just type ansible hyphen playbook so what you can do is ansible playbook dot slash test not yaml so now if you don't specify any other module and the host does not have any other modules so it will take the host by default and if you see here we have the dump test output and there's the debug message that we are getting so changed equal to true failed equal to false message equal to goodbye and original message equal to hello so now what we have achieved is we have successfully run the development setup we have written a test code and we have successfully tested it on the local environment i have to write an ansible module which can take a string and encode it and give me the result and that same module should be able to decode if i provide a encoded string to it you understand the requirement so the same module it should take the string and it should encode it and also it should be able to process a encoded string or a byte string into a normal string that we have so it should perform both the base64 encoding and base64 decoding so such as the requirement for us so what i'm going to create is i'm going to create a base64 mod that will be my ansible module and the type of arguments it will take is the name that will be the string type will be either encode or decode and neo equal to true or false based on the states change that i want to do and the playbook will be exactly like this i'll register the output whether it is encoded output or the decoded output and i will dump it to my debug message so this is the particular requirement that i have right now and we are going to write some basic python commands so if you are not aware of python don't worry this is very simple anyone who has some basic understanding will be able to write this or you can just copy the same code that i have given here and i will be giving this on the git repository as well you can copy the same thing and you can try and experiment on that and try to learn things in a simple way so that when you try to achieve something big you will not have that initial hurdles so try and make use of the initial time that you have and try to learn things that you are able to understand and grasp don't worry if you are not aware of python it's not a mandatory thing here it is better to learn it but if you don't know it don't worry you can just learn this and you can see how simple it is and if you get the confidence now you can as well go ahead and learn python and execute your own operations as well so first of all we have to create our own module and we'll go to that particular path that we had the module path and we'll write our own python code i'm going to create a new file so my new file will be base 64 mod b64 mod dot py and just hit enter so this is the placeholder for me to write the code so my benchmark that i am going to set is basically so let's create a this 64 mod dot py and what i'm going to do is i'm going to copy the same content that i need and i'll just modify my code here so go ahead and copy this just paste it here okay so now this is done so i don't want to modify this terry jones uh 2018 copyright and everything and let's just add my name at the bottom by pythagorean 2021 and this is fine we don't have to mess up with the import statements the documentation is also fine and this is not going to be my test it will be base64 mod and this will be my test module uh okay so this is my base 64 module encode and decode so the first thing that you do go is you have to understand that it starts from main and it goes to main it is going to run module isn't it so run module is this particular function the module arguments that you see here is basically the arguments that you pass so the next thing that i wanted to give was type is entered so type equal to dictionary and the string that is basically a string and required equilateral so that's a mandatory value for us now come down you don't need to change this not a problem there's the original reference dictionary so it is just creating the module object here there's the check mode analysis and after that this is the place where you're going to edit your code so it's written here as well manipulate or modify the state as needed where your module will do what it wants to do so this is the place where you're going to have your own code so first what i'm going to do is i'm going to check if module dot params i'll just copy this i don't want to do any spelling mistakes so the first thing that i want to check is it should have either encode or it should have either decode otherwise it should throw an error function it should be either n code so i'll what i'll do is i'll give a flag here status flag so if it is encode then what it will do is it will set the lag to true or else if it is decode it will set the flag decode status equal to 2 and just copy these things and just keep it here as variable declaration decode and set the flags to false so this will be the default lag status okay let's save it so now our input validation is partially done i don't want to do much here you can as well do the input validation for your strings that you provide so now the next thing what we have to do is we have to raise an alert here so for that you see we have a message here like module exudation similar to that we have failed json here so with this we can actually raise error messages you can just copy this or you can just copy this as well let's delete this so what i have to check is if not encode status and not decode status if both of them are false then what i'm going to do is i'm going to throw an error exception so that will be bad input and it should be encode or decode okay so this is the message that i have here so it will fail the json the next thing is once i have the input validation done i have to change string to base64 and return it so now what i have to do is so this is my encode part so if encode status so at a time only one operation can be there because there is only one type so just make sure that you don't get confused here so message will be the message is equal to module we are going to take the parameter params of what is the parameter name isn't it so name is the parameter and message underscore byte will be message dot and code of ascii so i'm going to just format it to the ascii characters and then i'll convert this to base64 encode so for that what i need to do if i need to use encoding i have to just import this import base64 here so it will be able to use that encoding functionality so post that what i can do is base 64 underscore byte underscore string bytes underscore string is equal to strf i just want to convert it to str the base64 dot base b64 encode so that is a particular method within that particular base64 package message underscore byte so i'll take this and i'll pass it here and i'll use the base64 encode that is present in base64 that we encode that we just imported right now and i will store it in a string format here after that i have to show it to the user isn't it so there is a result json which i can make use of so this is the result json here see there's the resultation so that is what i am going to use here so result of original message original message is equal to message which is basically our module parameters of name and then copy this once again just paste it three times so encoded message will be input and message will be 64 byte string and then message we have to give some message to the user so that will be basically your string your data your data is encoded to base64 enjoy enjoy okay so your users can enjoy now so now what we can do is we can use the same logic here to decode the string just use the same thing here and we'll just change a few things change base64 to normal string and return it so here it will check for decode status so module status module parameters is name [Music] the next thing that gets changed here is uh message base 64. we will change this to a new name so this will be my base64 underscore name message and this one will be base64 underscore bytes message so this will be base64 message dot encode of ascii and then base 64 byte string strf base 64 dot decode of base 64 bytes so we are taking this byte here and we are providing it here and we will just convert this the original message here will be base64 message decoded decoder message will be base 64 byte string and message will be decoded to normal string okay just copy this and we'll just just paste it here so we have the decode status we have the encode status okay we have the validation [Music] we have the variable declaration of the status flag declaration we have given it the type it is equal to dict of str yeah that's fine just save this now i'll create a playbook in our home folder have created this so what exactly it tells is that it will take the host as localhost we'll take the name as hello that is the string that we want to encode so type equals to encode here status change equal to true and the test results will be dumped into that particular debug message so this is what i need so let's create another playbook here so this will be my playbook 64. 64. yammer just hit i ctrl v paste it and this is the playbook that we have so the playbook is this so it execute localhost so it executes on the localhost and it uses base64 mod that we have recently created it takes hello as a string and it encodes it let's go ahead and run this okay so ansible hyphen playbook dot slash playbook 64. yaml just hit enter and it should basically encode your string so this is the original message there's the encoded message the message that i wanted to give to the user was now data is encoded to base64 let's copy this let's copy this string that is basically your so that is basically your encoded string is ended so i'll just modify this playbook i'll just modify here and i'll show it to you let's just modify the playbook here i will provide now decode record and the string that i have is this one so this is the encoded string and i want to decode it so the type will be decode now and will run the playbook now so we'll see whether it is able to decode or not okay yeah so the original message was this it is able to decode it to hello so that is one of the very simple requirements that i had let's suppose i give something else let's see what happens let's suppose i gave it like let's save it and let's run it what happened when there was a condition that we had put for input validation it has actually helped us to identify that that was a bad input it should be either encode or decode the benefit of using input validation is that whenever there is a wrong input it basically helps you identify what exactly the error was and based on our input validation we were able to supply a very good understandable message so that our users or someone who is going to use our module will be able to understand if there is any error so you have to do the same thing you have to follow protocols you have to follow the measures and you have to follow the standards and ensure that your code is going to be used by someone else so you make sure that your code is understandable enough and there are proper error handling mechanisms in place and all the messages that you write for your error handling should be meaningful and others should not face any problem while using your modules so that's it for today's session on ansible demo that is basically how to create your own ansible modules i hope it was easy to understand and i hope you will also experiment this by creating your own environment your test environment and installing the ansible sdk and you will let me know how did it go for you and if you faced any problems then please put them in the comment section below i'll try and answer them and if you wish to support me then the links to insta mojo paypal and patreon are given in the link in the descriptions and you can join the membership on the channel as well if you wish to support the channel so wishing you all the good health and success until then it's pytholic signing off [Music] you
Info
Channel: Pythoholic
Views: 3,320
Rating: undefined out of 5
Keywords: Pythoholic, ansible tutorial, ansible playbook, ansible modules, ansible module development tutorial, ansible training, ansible playbook tutorial, learn ansible, ansible devops, ansible shell module, infrastructure as code, ansible modules custom, infrastructure as code explained, infrastructure as code for dummies, ansible beginners tutorial, devops tutorial aws, red hat ansible tutorial for beginners, ansible training for beginners, introduction to ansible playbook
Id: 7pzJ6ri4RTM
Channel Id: undefined
Length: 36min 3sec (2163 seconds)
Published: Thu May 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.