All of you working in the software industry must have heard of this term called “Ansible”, but why there’s so much hype for this term? How much you know about Ansible? Is it a tool or a framework or simply a practice? In this tutorial on Ansible, we will “Unsurprisingly” dive into the main concepts of Ansible, we will understand the need of Ansible, its workflow and many more.
Watch this Ansible Tutorial for Beginners video
Here we have the list of topics if you want to jump into a specific one:
- What is Ansible?
- Why we need Ansible?
- Important terms in Ansible
- Ansible Workflow
- Architecture of Ansible
- Benefits of using Ansible
- Operations by Ansible
- Setting up Ansible environment
- Use case of Ansible
First let’s understand What is Ansible?
What is Ansible?
Ansible is an open-source platform used for automation, helping with various operations such as configuration management, application deployment, task automation as well as IT Orchestration. It’s easy to setup, efficient, reliable and powerful. It’s free and runs on Linux, Mac or BSD. Apart from free version, they have an enterprise edition called as Ansible Tower. These hectic tasks like configuration management, task automation etc. were also being done through many traditional practices, so why do we need ansible for that?
We shall discuss this as we move along our DevOps tutorial.
Watch this interesting Video on What is Ansible?
Prepare yourself for the Top DevOps Interview Questions And Answers!
Why we need Ansible?
Before understanding Ansible as whole, we must understand the need of Ansible, what were the problems that we faced previously that we had to come with a solution called as Ansible?
Let’s consider a case where a system admin is responsible for handling the company’s infrastructure.
There are 9 servers out of which, 5 servers are acting as Web Servers and 4 servers as Database Servers. Further, you want to install Tomcat on web servers and MySQL on Database servers. If we investigate the traditional method where system admin will have to manage the servers manually, install the required software, will have to change the configurations along with administer the services on each server individually.
While there are small number of services, and small configurations requirements, it will be easy for the sysadmin to handle these amounts of server, even if there are like minimal increment in the services, sysadmin can provision few more servers to maintain the infrastructure.
As we know research and inventions never stop, technology keeps on enhancing, new features and services are introduced every day and week, the hosted applications become more complex with the increased amount of services. As the number of services starts to increase, the same task must be repeated multiple times as we provision more servers, it gets more difficult for sysadmins to setup, update and maintain all these servers manually.
In most of the cases, sysadmins won’t be able to setup each of the servers identically.
Also, such processes always ended up hampering the velocity of the work for developers as the development team was agile and are always releasing the software frequently, on the other hand, the sysadmins were spending extra time on system configurations and managing the infrastructure.
This is where ansible comes to the rescue!
How? That we shall discuss as we move to our next topic that is Ansible workflow.
But, before we proceed towards the working of Ansible, first let’s understand few important terminologies in ansible, so that you can understand the workflow in a better way.
Become Master of DevOps by going through this online DevOps training in London.
Important terms in Ansible
- Controller Machine: This is where your ansible gets installed, your controller machine helps in enabling the provisioning on the servers that you manage.
- Inventory: This is basically an initializing file which contains information about the servers that you are managing.
- Playbook: An organized unit of scripts which defines an automated work for the configuration management of your server.
- Task: Task block defines a single procedure to be executed on server, like installing the packages.
Now that we understand the important terminologies of Ansible, let’s move forward and understand the Workflow of Ansible.
As we discussed above, when the services increase, the sysadmin will provision more number of servers in order to do the configuration management. He doesn’t need to do it manually anymore, just install the Ansible on master node where he needs to write a code into the Ansible Playbook to describe the setup, installation process and the configuration required for these servers
The local machine connects to these server (nodes) through an inventory using Secured SSH connections.
Once these nodes are connected to the master server, then node servers are being analyzed and the playbook codes are pushed towards each of the servers so that these playbooks can configure the servers remotely which leads to a consistent environment.
Now let’s try to understand with its ARCHITECTURE.
Go through the Best DevOps Course in New York to get clear understanding of DevOps.
You can observe from the diagram above, the Ansible Orchestration Engine interacts with user who is writing the Ansible Playbook in order to execute the Ansible Orchestration Engine and interacting along with the services of public/private cloud and Configuration Management Database.
Ansible connects your nodes and pushes out the small “Ansible Modules” programs. Modules are being executed by the Ansible and then get removed when finished. These modules can reside on any machine, no servers or daemons or databases are required here. You can work with text editor of your choice or a terminal and a version control system to keep track of the changes made in your content.
Piece of code which expands the core functionality of Ansible. There are plenty of handy plugins and you can write your own too.
We already discussed this in terminologies, they are lists of hosts/nodes having their IP Addresses, servers, databases etc. which are needed to be managed.
Also, as discussed earlier, you write your code in Playbook, they are simple and written in YAML format which basically describes the tasks which are supposed to be executed through Ansible. You can launch tasks Synchronously as well as Asynchronously with playbooks.
Ansible API’s works as a transport for the Cloud Services, public or private.
Hosts are basically node systems in the Ansible Architecture getting automated by Ansible only, any type of machine like- Windows, RedHat, Linux, etc.
We can also use Ansible to automate different networks, it uses the easy, simple and yet powerful agentless automation framework for IT operations and development. It uses a type of data model (playbook or role) which is separated from the Ansible Automation Engine that spans the different network hardware quite easily.
A type of repository which acts as a data warehouse for the IT installations.
A network of remote servers on which you can store, manage and process your data, these servers are hosted on internet, storing the data remotely rather than local servers, just launch your resources and instances on cloud, connect them to your servers and you’ve the wisdom of operating your task remotely.
Now that we understand the workflow of Ansible along with its architecture, let’s sneak a peak over some of its benefits.
Learn more about DevOps in this DevOps training in Sydney to get ahead in your career!
Benefits of using Ansible
- Agentless: As long as connection can be ssh’d into and it has python, it can be configured with Ansible, no agent/software or additional firewall ports are required to install on your client or host systems for automation. Also, you don’t have to worry about setting up and managing the infrastructure.
- It’s Simple: As we’ve seen above that Ansible uses a very simple syntax written in YAML known as Playbooks, YAML (Yet Another Mark-up Language) which is human-readable data serialization language. You don’t need special coding skills required to code and understand the playbooks. It is very easy to install and executes task in order.
- Modular: It’s modular as you require only one program per script, in this way you can spread your programs across different servers.
- Efficient: As no extra software are required on your servers which means more space for your resources is there.
- Powerful & Flexible: Having powerful features which gives you a capability to model even the complex IT workflows in lesser time along with managing infrastructure, networks, operating systems and services that are already in use.
Moving forward, we also need to check once with all the operations that can be performed by ansible.
If you have any doubts or Queries related to DevOps, get it clarifies from DevOps Experts on DevOps Community.
Operations by Ansible
So far you must have figured out how reliable is Ansible, so below are the type of operations Ansible can perform:
It provides stability in the performance of your product by recording and updating the stats in detail which describes hardware and software of an enterprise/organization. Stats like versions and updates applied to the installed software packages, along with locations and network addresses of hardware devices.
For example, Company A wants to install the new version of an Nginx on all of their server machines, it won’t be feasible for them to update each and every machine manually. Just install your Nginx on one machine and deploy them across rest of the other machines using Ansible Playbooks.
Either you’re booting or starting servers or virtual machines or creating cloud instances from various templates, Ansible is there to help you with the smooth running of this process. Ansible makes sure to provision the required packages are installed on your application.
With Ansible, you can define your application as well as manage its deployment. Instead of performing the deployment steps one by one, just install Ansible on your machine and it will do the same task for you, that even in lesser time, just list those tasks in your Ansible Playbook and wait for the Ansible to execute these tasks in an order
With Ansible, you can easily configure your security details once in a Control machine and the same security details will be spread across all the other nodes.
You can also perform the orchestration of your application using Ansible. Ansible provides the Orchestration by aligning the business requests with the Application, data and infrastructure. Creates an application-aligned infrastructure which can be scaled up or down based on needs of each application. Now that we understand Ansible’s architecture and its workflow, let’s head towards setting up its environment.
Interested in becoming DevOps Expert? Click here to learn more in this DevOps Course in Toronto!
Setting up Ansible Environment
To setup Ansible on CentOS, first we need to setup the EPEL Repository.
EPEL (Extra Packages for Enterprise Linux) is a free repository project from Fedora team, it’s open source and provides high quality add-on software packages for Linux distributions which includes Red Hat, CentOS.
Since Ansible package is not available for default yum repositories for which we need to enable the EPEL repository on our machine by using this command:
sudo rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
After this, all your necessary packages will be downloaded to install the Ansible.
As our EPEL repo is added, so add the below command & install Ansible:
yum install ansible -y
It will complete your installation in no time.
In order to check the version of your ansible, use this command:
After Installation, you need to add servers on which you want to execute these management tasks using Ansible.
For that you need to create another CentOS VM which should act as your node machine.
First you have to setup password-less SSH authentication with your nodes on your control machine.
Then you have to generate SSH key on your Ansible Control Machine, just use the below command for that:
Once your public key is generated, check your IP address of your nodes which you have to specify in the Ansible Inventory later, use the ifconfig command for that and it will show your IP Address.
After your key is generated, you have to copy the public key of your ansible server to the nodes, use this command below:
ssh-copy-id -i root@<IP address of your node machine>
Now you can use any editor to write your inventory and name your servers as per your choice, like here we are taking an example as “intellipaat“.
If you want to perform a simple ping operation in order to test the connectivity using ansible, just type the command below:
ansible -m ping ‘intellipaat’
It must show your IP address and a SUCCESS message on the screen.
To setup Ansible on your Ubuntu Machine.
First, configure PPA on your machine, type the following command on your terminal:
$ sudo apt-get update $ sudo apt-get install software-properties-common $ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get install ansible
You will be able to manage the remote machines using Ansible, check your version using Ansible-version command.
As we know how to setup the Ansible Environment, now let’s sneak a peek over some of Ansible’s Ad-hoc commands.
Interested in getting an industry-recognized certification in DevOps? Enroll in Intellipaat’s DevOps Course in Bangalore now!
Ad- hoc commands are something want to type in order to do it really quick but don’t want to save for later. In case you need to reboot all your servers, then you will have to run the Adhoc commands from ‘/usr/bin/ansible’. These commands are one time usage and are not used for configuration management and deployment. Only Ansible Playbook can be used for Configuration Management and Deployment.
- Parallelism and Shell Commands
For example, using Ansible’s Command line tool to reboot a company’s server in 10 Parallel forks at a time, for that you’d need to set up SSHagent to establish a connection.
$ ssh-agent bash $ ssh-add ~/.ssh/id_rsa
In order to run reboot all of the company servers in a group names ‘test-servers’, in 10 parallel forks we will use:
$ Ansible test-servers -a "/sbin/reboot" -f 10
Ansible will be running these Ad-hoc commands from the current user account by default and in case you want to change its behaviour, just pass the username in Ad-hoc commands as shown:
$ Ansible test-servers -a "/sbin/reboot" -f 10 -u username
- File Transfer
Ad-hoc commands can also be used for file transfer for doing SCP (Secure Copy Protocol) numerous of files in parallel fashion on multiple machines.
To transfer files to multiple servers/machines use the command below:
$ Ansible test-servers -m copy -a "src = /etc/yum.conf dest = /tmp/yum.conf"
In order to create a new directory, use the command as follows:
$ Ansible test-servers -m file -a "dest = /path/user1/new mode = 777 owner = user1 group = user1 state = directory"
To delete the whole directory and files, we use the command as
$ Ansible test-servers -m file -a "dest = /path/user1/new state = absent"
- Managing Packages
Following are some of the ad-hoc command using yum which check if yum package is installed or not (but not for updating it)
$ Ansible test-servers -m yum -a "name = demo-tomcat-1 state = present"
To check if the package is installed or not:
$ Ansible test-servers -m yum -a "name = demo-tomcat-1 state = absent" $ Ansible test-servers -m yum -a "name = demo-tomcat-1 state = latest"
You can also use Ad-hoc commands for gather facts by implementing conditional statements in your playbook, find information of all your facts through this command:
$ Ansible all -m setup
Now let’s understand the structure of playbook and its basic syntax.
Now, you know the most important skill set of a DevOps Engineer. But, do you know, DevOps Engineers are among the highest paid professionals in the technology domain? so join DevOps training in Hyderabad!
Ansible Playbooks Commands
As we have discussed earlier that the Ansible code is written in Playbook in a YAML Format (Yet another Mark up Language).
YAML is a script type language, you can use any editor to write YAML files but notepad++ will be the easier one.
Let’s check the basic syntax of playbook with the below example, a YAML file will start with 3 Hyphens —
--- name: install & configure Intellipaat DB hosts: testServers become: yes vars: oracle_db_port_value : 1521 tasks: -name: Install the Oracle DB yum: <code to install the DB> -name: Ensure the installed service is enabled and running service: name: <your service name>
You can save the above file as first.yml, you need to follow the correct indentation while writing in YAML and be little careful about writing its Syntax.
It specifies the name of your Ansible book, like what this playbook will do, you can give it any relevant name.
It specifies the lists of hosts or host groups against which we will run our task, this tag is compulsory. Host tag communicates with Ansible and enables it to run the listed tasks; these tasks can run on the same machine as well as on remote machine. You can run the tasks on multiple machine thereby hosts tags can enable group of hosts’ entry as well.
Using this tag, you can easily define your variable which you are used in playbook; its usage is similar to variables in any of the programming language.
Playooks must contain tasks or list of task which are to be executed. These are basically list of actions that playbook needs to perform. Task field- contains the name of task. Every task links to a piece of code called module, module and arguments (in module) both should be executed.
Now that we have discussed about basics of Ansible, let’s check the use case of Ansible.
Get in touch with Intellipaat for a comprehensive DevOps Training and be a certified DevOps Engineer!
Ansible Use Case
Ansible has the potential to change the landscape of orchestration by seamlessly uniting it with configuration management, provisioning and deployment of applications, all of it, in this one easy to use platform.
This simple yet powerful platform enables you to solve your most challenging and complex problems and that is the reason why it comes out on top among its peers and is adored and supported by a large community.
Ansible’s popularity isn’t just limited to the said community but it is also being used by some of the major IT organizations such as Arista.
Arista Networks is a company that provides software driven cloud networking solutions for cloud architectures, that is, large data center storage and computing environments and helps them achieve better economy and agility.
Arista uses Ansible, bringing NetOps and DevOps together. Not just that, Arista helps you get the most out of your switch by leveraging the integration with Ansible and bringing Ansible’s strength, simplicity and agility to your network.
There are various benefits of the combined Ansible and Arista solutions such as:
- There is improved accuracy and speed in the combined solutions
- Integration of ansible with Arista eliminates the need to use third party EOS extensions
- Playbooks enable you to make dynamic and intelligent decisions
- Additional switch configuration is not required
- You get complete control over your configuration
- There is continuous EOS configuration compliance.
This is where you might ask how exactly Ansible is able to offer such significant benefits to this company? How exactly Arista is able to utilize the power of Ansible to draw out such significance benefits?
To answer your question, following is all about how Ansible is aiding Arista.
- Ansible uses CLI or eAPI to communicate with your Arista switch.
- The CLI connection securely manages your device using SSH.
- Ansible uses the existing TACACS users to provide built in authorization.
Look at this video tutorial on DevOps Tools!
Server systems are the backbones and foundation of applications. They too need to be version controlled, tested and automated like our applications. Now configuration management is nothing new, tools to perform configuration management have been around for quite some time now. Puppet, chef are some of the names that pop up in our minds when we talk about configuration management. Ansible is somewhat similar to these tools and yet it seems everyone is interested in Ansible these days. You got to know the reason behind that in this very tutorial along with every must-know thing about ansible to get you started with it. However, there is much more to ansible that you could still learn in depth by enrolling in DevOps Certification Course by Intellipaat.Previous Next