Getting Started with Ansible: Configuring a web server via Ansible Playbook
Samarth Pant
Information Technology Analyst @ TCS | Contextual Master | OpenTofu | Terraform |
What is Ansible
Ansible is a suite of software tools that enables infrastructure as code. It is open-source and the suite includes software provisioning, configuration management, and application deployment functionality.
Ansible helps to manage multiple machines by selecting portions of Ansible's inventory stored in simple plain text files. The inventory is configurable, and target machine inventory can be sourced dynamically or from cloud-based sources in different formats.
Ansible Modules:
Modules are mostly standalone and can be written in a standard scripting language (such as Python, Perl, Ruby, Bash, etc.)
Ansible playbook:
Generally speaking, ansible configurations are deployed from something called an Ansible playbook. A playbook is a configuration file that defines a series of tasks to be executed on the managed nodes.
Inventory:
Inventory is like a database that consists of the IP addresses or DNS names of the Target/Managed Nodes on which the remote execution is to be performed.
Controller Node:
A control node is a Linux server that has Ansible installed on it and is used for managing remote hosts or nodes. These remote systems are known as Managed Hosts or Managed nodes.
Basic Setup
Once Ansible is installed(to install Ansible on RHEL or CentOS use "yum install ansible-core" command), update the inventory/hosts file( by default present in /etc/ansible/) with the IP Address of the Target Node(node on which we need to implement the Ansible Playbooks). Use the ssh method to authenticate the controller node to be able to connect to the Target Node.
Once the file is configured, use the below command to test the connectivity of Ansible Controller with the Target node:
领英推荐
ansible all -m ping
You should see something similar to the below output.
Creating a Basic Ansible Playbook :
We will be installing the Apache HTTP server on the target node via Ansible Playbook.
Once the index.html page is created, we will create an Ansible Playbook named package_install.yml and will place the below code in the file :
---
- name: Install Package
hosts: all
tasks:
- name: Install httpd package
yum:
name: httpd # installs the httpd package
state: present
- name: Enable HTTPD service
service:
name: httpd # enables the httpd service
state: started
- name: Copy the Index.html file
copy: # copies the index.html page from Controller node to Target
src: /etc/ansible/playbooks/index.html
dest: /var/www/html/index.html
owner: root
group: root
mode: 0644
Once the playbook file is ready, we will check for the syntax of the playbook using the below command:
ansible-playbook --syntax-check package_install.yml
Once the syntax is checked and is correct, we can proceed with running the playbook using the below command:
ansible-playbook package_install.yml
You can see from the terminal that our new task was a success. Let’s see if we can view the new page. Copy one of the IPs to your browser again.
Hence, we were successful in installing and configuring the Apache HTTP server on a Linux Machine via Ansible.