Ansible is a simple, powerful, and agentless tool that simplifies the process of IT automation and expedites DevOps efforts. Ansible works to help you automate and configure your infrastructure to save time and increase productivity. It’s straightforward, secure, and powerful, making it an easy tool to learn and implement within your organization. Today, we’ll discuss what Ansible is, what it does, key terms, and how to get started.
We’ll cover:
Get hands-on with Ansible
In this course, you’ll learn the ins and outs of Ansible to help you manage and automate your infrastructure and code deployment.
Ansible is an open-source orchestration and automation tool used for software provisioning, configuration management, and application deployment. Ansible was first developed in 2012 by Michael DeHaan, the creator of Cobbler and Func. The company funding Ansible was acquired by RedHat, Inc. in 2015. RedHat was acquired by IBM in 2019, so now, Ansible lives under the IBM umbrella.
Ansible runs on Windows and Unix-like operating systems, providing infrastructure as code. It has its own declarative programming language for management and system configuration. It can connect with cloud environments like Amazon AWS and Microsoft Azure to help you manage and automate your infrastructure and code deployment.
Ansible is simple to install, connects easily to clients, and contains many features. It’s push-based and connects to clients via SSH, so it doesn’t require an agent on the client.
By pushing modules to clients, the modules on the client execute locally and the outputs are pushed back to the Ansible server. It uses SSH-Keys to simplify the process of connecting the clients. Hostnames, IP addresses, and SSH ports are stored in inventory files. Once an inventory file is created and populated, Ansible can use it.
Ansible is a preferred tool in DevOps organizations because it streamlines automation and flexibility. Some of the key benefits include:
Ansible is a powerful tool that has a wide variety of applications and uses, particularly for connecting it to Docker environments or cloud services like AWS. Some of the main use cases of Ansible include:
Now that we’ve discussed the benefits and common use cases of Ansible, let’s get familiarized with some of Ansible’s key terms.
Learn about Ansible without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature real-world examples to make your learning fun and efficient.
Now it’s time to learn about some fundamentals of the tool. We’ll walk you through getting started with Ansible, Ansible playbooks, and Ansible ad-hoc commands.
Remember that with Ansible, you have an Ansible server and hosts. The Ansible server is the machine where Ansible is installed, and the hosts are the machines handled by Ansible via the Ansible server. The Ansible server can handle multiple hosts.
Ansible server requirements:
Host requirements:
Note: The way you set up your Ansible environment is dependent upon your device’s requirements. Your next steps would be to download Ansible, then configure and automate your experience.
Playbooks are the foundation of configuration management and orchestrated automation with Ansible. They are where you create instructions to determine the tasks you want to execute within your different hosts.
Playbooks are reusable and repeatable, making it easy to perform a task more than once. Playbooks are written in YAML and have a small amount of syntax. Each playbook is composed of one or more plays.
Each play defines two things at a minimum:
A play begins by defining the hosts
line, which is a list of one or more host patterns or groups divided by colons. The hosts
line is followed by a tasks
list. Ansible will execute the tasks on the designated hosts in order, one at a time.
For example, take a look at the playbook below:
---- hosts: webserverstasks:- name: deploy code to webserversdeployment:path: {{ filepath }}state: present- hosts: dbservertasks:- name: update database schemaupdatedbschema:host: {{ dbhost }}state: present- hosts: webserverstasks:- name: check app status pagedeployment:statuspathurl: {{ url }}state: present
In this playbook example, we executed three plays: we deployed code to the web servers, updated the database schema, and checked the app status page.
Ad-hoc commands are another important part of the Ansible environment. They are used to automate a single task on one or more hosts.
They offer a fast and simple way to automate, but they’re not meant for reusability. Ad-hoc commands are useful for occasional tasks. For example, if you wanted to restart a service on multiple hosts, you can easily achieve this with a single ad-hoc command.
Use cases for ad-hoc commands:
Different ad-hoc command types and what they do:
file
: Add and/or remove directoriesping
: Test connectivitystat
: Retrieve facts about directoriescopy
: Copy filesreplace
: Update filesdebug
: Debug variables and expressionslookup
: A plugin to access data from outside sourcesHere’s an example of the stat
command, which retrieves facts about directories:
ansible localhost -m stat -a "path=/ansible"
Congratulations on taking your first steps with Ansible! This popular tool is perfect for modern DevOps environments and is easy to learn for existing developers. You’re now ready to dive deeper into Ansible and learn more about topics such as:
To help you develop your Ansible skills fast, check out Educative’s course Ansible: Zero to Production Ready. In this self-paced, curated course, you’ll learn how to set up a Docker environment, connect to the cloud, manage infrastructure within the cloud, and automate configuration and state management processes with Ansible.
By the end of the course, you’ll earn a valuable certificate and learn a new skill to put on your resume that will open many doors for you in areas like DevOps and cloud computing.
Happy learning!
Free Resources