As the size and complexity of IT infrastructure increases, it becomes increasingly important to automate system administration tasks. Ansible is a powerful tool that can be used to automate system administration tasks in CentOS 7. In this blog, we will discuss how to automate system administration tasks in CentOS 7 with Ansible.
What is Ansible?
Ansible is an open-source automation tool that allows you to manage and automate IT infrastructure. It is a simple and powerful tool that uses a simple language called YAML to describe automation tasks. Ansible uses SSH to communicate with remote servers and does not require any additional software to be installed on remote servers.
Setting up Ansible
Before we start automating tasks with Ansible, we need to install Ansible on our system. Ansible is available for installation on most Linux distributions, including CentOS 7.
To install Ansible on CentOS 7, we can use the following command:
sudo yum install ansible
Once Ansible is installed, we need to create an inventory file that lists the hosts we want to manage with Ansible. The inventory file is a simple text file that lists the IP addresses or hostnames of the servers we want to manage. We can create an inventory file by running the following command:
sudo vi /etc/ansible/hosts
In this file, we can list the IP addresses or hostnames of the servers we want to manage. For example, if we want to manage a server with the IP address 192.168.1.100, we can add the following line to the inventory file:
[web]
192.168.1.100
This will create a group called “web” that contains the server with the IP address 192.168.1.100.
Automating tasks with Ansible
Once we have set up Ansible and created an inventory file, we can start automating tasks with Ansible. Ansible uses a simple language called YAML to describe automation tasks. YAML stands for “YAML Ain’t Markup Language” and is a human-readable data serialization format.
To automate tasks with Ansible, we need to create a playbook. A playbook is a YAML file that describes a set of tasks that we want to automate. We can create a playbook by running the following command:
sudo vi /etc/ansible/playbook.yml
In this file, we can define the tasks we want to automate. For example, if we want to install the Apache web server on the server with the IP address 192.168.1.100, we can add the following lines to the playbook:
- name: Install Apache web server
hosts: web
become: true
tasks:
- name: Install Apache
yum:
name: httpd
state: present
- name: Start Apache
service:
name: httpd
state: started
In this playbook, we define a task called “Install Apache web server” that will run on the hosts in the “web” group. We use the “become” parameter to run the tasks as a privileged user (i.e., root). We define two tasks: one to install the Apache web server using the “yum” module, and one to start the Apache web server using the “service” module.
Once we have created the playbook, we can run it using the following command:
sudo ansible-playbook /etc/ansible/playbook.yml
This will run the playbook on all the hosts in the inventory file. Ansible will connect to each host using SSH, copy the playbook to the host, and execute the tasks defined in the playbook.
Conclusion
Automating system administration tasks in CentOS 7 with Ansible is a straightforward and efficient way to manage IT infrastructure. Ansible’s simple language and SSH-based communication make it easy to learn and use. By using Ansible, we can save time and reduce errors by automating repetitive tasks and ensuring consistency across our infrastructure.