In IT automation, handling setups, rollouts, and daily tasks on many systems can be tough. As companies grow, they need a dependable, productive, and user-friendly automation tool. This is where Ansible steps in. Ansible is a robust, open-source tool that DevOps engineers love to use to automate jobs across multiple systems. In this article, we will look at what Ansible is and how it works.
What is Ansible?
Ansible is an open-source tool for IT infrastructure automation. It helps manage configurations, deploy applications, and automate tasks. It works without agents through SSH, so you don't need to install software on remote machines. Ansible uses simple playbooks based on YAML to define tasks and manage infrastructure. This makes it easy to automate repetitive tasks across many systems. Its modules work with different operating systems, cloud platforms, and services.
Key features and benefits of Ansible
Agentless architecture
Ansible stands out because it doesn't need agents. Other automation tools make you put agents on each system you want to control. Ansible just uses SSH or WinRM to connect. This makes things simpler. You don't have to deal with agents all over your network.
Easy-to-Read playbooks
Ansible uses YAML to write its playbooks. These are scripts that tell it what to do. YAML is easy for people to understand. This helps teams to create, check, and handle automation scripts. They are more straightforward than the setup files used by other tools.
Consistent results
Ansible is built to give the same result every time. You can run a task over and over, and it won't change things after the first time. This means your systems stay set up the same way, no matter how many times you run the playbook.
Scalability
Ansible can handle thousands of machines simultaneously, making it a good fit for small and big setups. Its simple design helps it grow without needing many extra resources.
Extensive module library
Ansible comes with a huge collection of modules that cover many tasks. These modules can be used to manage cloud providers like AWS, Azure, Google Cloud and Cherry Servers to set up network devices, databases, and more. This flexibility lets users automate pretty much any task in their IT setup.
Now, lets undstand the key componnents of ansible.
Run your deployments in a scalable and cost-effective open cloud infrastructure. Cherry Servers' secure virtual private servers offer automatic scaling, flexible pricing, and 24/7 technical support. Our pre-defined Ansible module helps you automate the installation and configuration.
Ansible components
Playbooks
Playbooks are YAML files that outline a series of tasks for managed nodes to execute. They describe the system's desired state and the steps to achieve it, helping implement complex IT processes.
Inventory
The inventory is a file or source that catalogs the managed nodes (hosts) and their groups. It can be static or dynamic and defines which systems Ansible will manage, allowing users to group hosts.
Modules
Modules are the building blocks of work in Ansible. They consist of scripts that carry out specific jobs on managed nodes, like installing packages or setting up services. Ansible comes with a wide range of built-in modules, and users can also create their own custom modules.
Tasks
Tasks represent individual actions within a playbook that runs modules. Each task is a single piece of work, such as kicking off a service or moving a file. The playbook determines the order in which tasks are executed.
Roles
Roles help organize and reuse Ansible code. They group tasks, variables, files, and templates into a structured format, making complex playbooks easier to manage by breaking them into smaller, reusable parts.
Handlers
Handlers are special tasks that run when another task triggers them. People often use them to take action, such as restarting a service when a configuration change happens.
Templates
Templates are files that contain variables. Ansible replaces these variables with real values when it runs a playbook. People use templates to create dynamic configuration files that fit the specific environment.
How Ansible works?
Ansible is a tool that makes IT tasks easier. It helps with setting up systems, deploying apps, and managing infrastructure. Here's how Ansible works step by step:
Set up the inventory
The inventory is a list of all the systems Ansible will manage. This can be servers, devices, or other types of systems. You can create the inventory as a simple text file or generate it from sources like cloud providers.
You list the IP addresses or hostnames of the systems you want to manage in the inventory. You can group these systems based on what they do (like web servers or database servers). This grouping lets you target specific sets of systems when you run your automation tasks.
Create playbooks
Playbooks are YAML files that spell out the automation tasks Ansible will run on the managed nodes. They outline the systems' desired state and the steps to get there.
Playbooks have plays, and each play aims at a group of hosts from the inventory. In a play, you list tasks for Ansible to carry out one by one on the targeted hosts. These tasks include setting up software, tweaking files, or kicking off services again.
Use Ansible modules
Modules serve as the building blocks of work in Ansible, in the form of scripts. These scripts perform specific jobs on the managed nodes, like installing packages or handling files.
When someone runs a playbook, Ansible connects to the managed nodes and applies the specified modules to complete the tasks. The modules run directly on the nodes, and Ansible receives the results.
Execute the playbook
Running the playbook carries out the tasks it defines on the chosen hosts. When you run a playbook, Ansible connects to the nodes in the inventory through SSH (for Linux/Unix systems) or WinRM (for Windows systems). Ansible doesn't need any software on the managed nodes, which makes it agentless. Tasks run in the order they appear in the playbook, so make sure the systems are set up just how you want them.
Gather facts
Facts are bits of info about the managed nodes, such as their IP addresses, OS types, and hardware specs. At the start of a playbook run, Ansible collects these facts.
Ansible gathers information about each node before it carries out tasks. This information can help make decisions in the playbook, like running tasks if the node has a certain operating system or hardware setup.
Perform tasks
Tasks are the specific actions in a playbook, such as installing programs, moving files, or starting up services.
Ansible runs each task one after another on the chosen nodes. Tasks are designed to be safe to run multiple times without causing unexpected changes. If a task has already been done successfully, Ansible will skip it, ensuring the system stays in the desired state.
Receive feedback and logs
Ansible gives real-time responses while running playbooks, including messages about success, errors, and detailed logs.
As Ansible carries out tasks on the managed nodes, it sends back updates on each task's status. This output helps you grasp what changes happened and spot any problems during the run.
Running Ansible commands
DevOps engineers use Ansible commands to do different jobs like running playbooks, handling inventories, etc., on remote servers. Here are some of the Ansible commands that are used often:
Ping all hosts
The ping module checks the connection between the Ansible control node and the managed hosts in Ansible.
This command shows if Ansible can connect to all the hosts in your inventory. It tries to run the ping module on each host. A successful connection gives you a pong response. A failed connection results in an error showing that the host is out of reach.
sudo ansible all -m ping
Gather facts for all hosts
The Ansible setup module gathers and displays detailed information about remote hosts (called facts). These facts include the OS, IP addresses, disk space, memory, CPU, and other system properties. The output is a JSON structure with all the collected facts for each host.
sudo ansible all -m setup
Check disk space (for filesystem usage)
You can use Ansible to check the disk usage on remote hosts by leveraging Linux commands like du (disk usage) or df (disk free). You can run the df command to check how much disk space filesystems use. People often use this to see the overall disk space available on mounted filesystems.
sudo ansible all -a "df -h"
Check the uptime of remote hosts
The uptime command tells you how long a system has been running, how many users are on it, and what the system load is. This information helps monitor how healthy and well systems are performing. You can set up Ansible to check multiple hosts at the same time.
sudo ansible all -a "uptime"
Change file permissions
The file module lets you change file permissions on remote hosts using ad-hoc commands. It also lets you change who owns files and other file details.
I have three files at path /home/osboxes/playbookDemo
sudo ls -l /home/osboxes/playbookDemo
To change the permissions of files in /home/osboxes/playbookDemo to 0777, run the below command.
sudo ansible all -m file -a "path=/home/osboxes/playbookDemo mode=0777"
Create a user on all hosts
The user module allows you to set up a user on all hosts using ad-hoc commands. This module helps you manage users on distant machines, letting you add, remove, and change user accounts. Here's an example of adding a new user named deploy to the remote hosts.
sudo ansible all -m user -a "name=deploy state=present"
Install a package using apt
You can run an ad-hoc command with the apt module to install a package on remote hosts using the apt package manager with Ansible. This works well for quick, single-time package setups or updates.
Below is an example of installing Nginx using the package manager of the remote system.
sudo ansible all -m apt -a “name=nginx state=present” -b
Copy a file to remote hosts
To copy a file from your Ansible control node to remote hosts, you can run an ad-hoc command using the copy module in Ansible. The copy module lets you move files, set ownership and permissions, and, if needed, create backups of existing files.
Below is an example of copying a file from /home/osboxes/lists.yaml to /home/osboxes/test/ on hosts.
sudo ansible all -m copy -a "src=/home/osboxes/lists.yaml dest=/home/osboxes/test/"
List the files at /home/osboxes/test and see if the file list.yaml got copied correctly.
ls -l /home/osboxes/test
These were a few widely used ansible ad-hoc commands.
Conclusion
Ansible has revolutionized IT automation by offering a straightforward, strong, and productive tool to manage infrastructure. Its declarative approach, agentless setup, and wide-ranging features make it a key asset for companies of all sizes. By grasping Ansible's main ideas and tried-and-true methods, you can harness its abilities to boost your IT work and make everything run more smoothly.
Found this guide helpful? check out the other Ansible tutorials on how does 'When' Condition work in Ansible and how to use Ansible with Terraform.