Introduction to Ansible
Uses, Architecture, and Working
Ansible is an open-source [infrastructure as code] software provisioning, configuration management, and application deployment tool. It is a robust IT automation platform that enables users to automate repetitive tasks, deploy applications, and manage configurations across several servers.
In this blog, we will go through the fundamentals of Ansible, including as its usage, operation, architecture, and features.
Uses of Ansible
Ansible is a versatile tool that can be used for a variety of tasks, including:
1. Configuration management: Ansible can be used to manage configurations across a large number of servers, ensuring consistency and reducing the risk of human error.
2. Application deployment: Ansible can automate the deployment of applications, making it easy to deploy new applications or update existing ones.
3. Provisioning: Ansible can be used to provision new servers, making it easy to scale up infrastructure as needed.
4. Continuous delivery: Ansible can be used to automate the entire software delivery pipeline, from code commits to deployment.
Ansible Architecture
Ansible has a client-server architecture, but unlike other configuration management tools, it does not require the installation of any specific agents or daemons on managed nodes. Ansible instead uses a push-based architecture in which the Ansible control node sends orders to managed nodes over SSH and then disconnects when the operation is finished. Because of its architecture, Ansible is simple to set up and use.
Let’s take a deeper look at the Ansible architecture’s many components:
1. Control Node: The control node is where Ansible is installed and where Ansible playbooks are executed. It might be any machine, such as a laptop, desktop, or server.
2. Managed Node: The managed nodes are the servers on which Ansible executes the playbooks. The managed nodes can be physical or virtual servers.
3. Modules: Small pieces of code used by Ansible to perform specific tasks on managed nodes, such as creating a user account or installing a package. There are built-in modules and the option to create custom ones.
4. Inventory: The inventory is a list of managed nodes that Ansible can control. It contains information such as IP addresses, hostnames, and SSH port numbers.
5. Ad hoc commands: These are one-time commands that may be run on managed nodes without a playbook. This is useful for doing quick activities or checking connections.
6. Plugins: Ansible extensions that give extra functionality, such as inventory plugins, connection plugins, and callback plugins.
7. Playbooks: YAML files that include a set of tasks to be done on one or more managed nodes. A playbook is a collection of plays that define tasks to be performed on specified hosts.
Working of Ansible
Let’s have a look at how Ansible works. As already said, it is straightforward and simple. When an Ansible playbook is executed, it connects to the managed nodes via SSH and performs the actions specified in it. Ansible uses modules to conduct tasks on controlled nodes.
To define playbooks, Ansible uses a declarative language known as YAML. YAML is a simple, human-readable data serialization format. A typical Ansible playbook includes the following sections:
1. Hosts: This section defines the hosts on which the playbook will be executed.
2. Variables: This section defines variables that will be used in the playbook.
3. Tasks: This section defines the tasks that will be executed on the remote hosts.
4. Handlers: This section defines handlers that will be executed when a task completes successfully.
5. Roles: This section defines the roles that will be used in the playbook.
Here is an example of Ansible playbook:
--- - name: Update web servers hosts: webservers remote_user: root tasks: - name: Ensure apache is at the latest version ansible.builtin.yum: name: httpd state: latest - name: Write the apache config file ansible.builtin.template: src: /srv/httpd.j2 dest: /etc/httpd.conf - name: Update db servers hosts: databases remote_user: root tasks: - name: Ensure postgresql is at the latest version ansible.builtin.yum: name: postgresql state: latest - name: Ensure that postgresql is started ansible.builtin.service: name: postgresql state: started
In this playbook, we state that we want to run tasks on the webserver host. We then define a variable named http port, which is set to 80. The tasks section describes the tasks that will be done on the webserver host. In this example, we install the Apache web server, start it, enable it, and configure the firewall to allow traffic on port 80.
In summary, Ansible operates by connecting to manage nodes through SSH and performing actions described in playbooks via modules. Playbooks are written in YAML and include sections for hosts, variables, tasks, handlers, and roles.
Thank you for reading. We hope you find this article useful in learning the Ansible concepts. If you have any questions, please leave them below. Thank you once again for your time!