Today's Automation and Ansible.

Nishant Bhosale
5 min readMar 21, 2021

This blog is about Ansible and its features and uses.

Automation is a need of today's world and demanded in the industry.

Imagine a scenario where you have hundreds of servers to configure. For each server to configure it will take days to that with manual way. So what if instead of the manual technique some program will go to each system and configure the servers with one click. Yeah, here the Ansible comes in the picture. Using Ansible we can configure the servers within some minutes, instead of days.

What is Ansible?

Simple, agentless IT automation that anyone can use

Ansible is a universal language, unraveling the mystery of how work gets done. Turn tough tasks into repeatable playbooks. Roll out enterprise-wide protocols with the push of a button.

Simple: Ansible uses a simple syntax written in YAML called playbooks. YAML is a human-readable data serialization language. It is extraordinarily simple. So, no special coding skills are required and even people in your IT organization, who do not know what is Ansible can likely read a playbook and understand what is happening. Ansible always executes tasks in order.

Agentless: Finally, Ansible is completely agentless. There are no agents/software or additional firewall ports that you need to install on the client systems or hosts which you want to automate. You do not have to separately set up a management infrastructure which includes managing your entire systems, network, and storage. Ansible further reduces the effort required for your team to start automating right away.

Efficient: No extra software on your servers means more resources for your applications. Also, since Ansible modules work via JSON, Ansible is extensible with modules written in a programming language you already know. Ansible introduces modules as basic building blocks for your software. So, you can even customize it as per your needs. E.g. If you have an existing message sending module that sends messages in plain-text, and you want to send images too, you can add image sending features on top of it.

Ansible Terms:-

Controller Node: The controller machine is where the Ansible is installed and where it manages the managed nodes. Here we have inventory.

Managed Node: The managed node is the one that is controlled by the controller node. All the configuration will be done hereafter the controller node gives the command.

Inventory: It is the file in the controller node which have information about the managed node. It have IP, Password, and which protocol to use to go to the managed node.

Ad-hoc Commands:

Ad-hoc commands are the ansible commands which individually & directly executed on Command Shell. This Commands mostly used to perform any quick tasks. Ad-hoc commands are quick and easy, but they are not reusable. So why learn about ad-hoc commands first? Ad-hoc commands demonstrate the simplicity and power of Ansible. Ad-hoc commands are great for tasks you repeat rarely. An ad-hoc command looks like this:

ansible [pattern] -m [module] -a "[module options]"

Ad-hoc tasks can be used to reboot servers, copy files, manage packages and users, and much more. You can use any Ansible module in an ad-hoc task. Ad-hoc tasks, like playbooks, use a declarative model, calculating and executing the actions required to reach a specified final state. They achieve a form of idempotence by checking the current state before they begin and doing nothing unless the current state is different from the specified final state.

Playbook:

The playbook is the code to write for configuring the managed node, It is written in YAML language, It performs a set of tasks for specified hosts(managed node). We can write one and many tasks in the ansible-playbook.

Each module within an Ansible playbook performs a specific task. Each module contains metadata that determines when and where a task is executed, as well as which user executes it. There are thousands of other Ansible modules that perform all kinds of IT tasks, such as:

  • Cloud Management
  • User Management
  • Networking
  • Security
  • Configuration Management
  • Communication

Role: A pre-defined way for organizing playbooks and other files in order to facilitate sharing and reusing portions of provisioning.

Facts: Global variables containing information about the system, like network interfaces or operating systems.

Handlers: Used to trigger service status changes, like restarting or stopping a service.

Task: A block that defines a single procedure to be executed, e.g. Install a package.

How Ansible Works:-

Ansible works by connecting to nodes and pushing out small programs called ansible modules. Ansible then executes these modules over SSH by default and then removes them when finished.

The Ansible management node is the controlling node, which controls the entire execution of the Playbook. It’s the node from which you are running the installation, and the inventory file provides the list of the host where the modules need to be run. The management node makes ssh connection, and then it executes the modules on the host machines and installs the product. It removes the modules once they are installed. So that’s how ansible works.

Next, you may be interested in learning how to install and configure here is my blog hope that will help.😇

Simple Ad-hoc commands:-

To see the hosts from the inventory.

ansible all --list-hosts

Command to ping the hosts which are defined in inventory.

ansible all -m ping

Command to Install the package on all hosts

ansible -i hosts all -m yum -a 'name=<package_name> state=present'

Commad to Remove httpd package on all hosts

ansible -i hosts all -m yum -a 'name=<package_name> state=absent'

Command to Start Service on all hosts

ansible -i hosts all -m service -a “name=<service_name>state=started”

Command to Stop service on all hosts

ansible -i hosts all -m service -a “name=<service_name>state=stopped”

Command to Restart a service on all hosts

ansible -i hosts all -m service -a “name=<service_name> state=restarted”

Conclusion

In this blog, we learned about the need for automation. Ansible the best configuration management Tool. What is Ansible and How does it work. Terminologies of the Ansible, and some ad-hoc commands.

--

--