How to make your site work stably and not crash. Part 3: Automation of routine tasks

Ansible

We wrote about the cost of losing access to a site or application on a VPS. We told you how to organize real-time server monitoring with the help of free Zabbix distribution:

The next important aspect is the automation of routine tasks. Administrators spend hours performing the same actions, such as updating software and backing up data. According to surveys, more than 50% of VPS admins say that automation has reduced administration time by 30-40%. Using Ansible or Puppet tools not only saves time, but also reduces the likelihood of error.

 

Why do I need Ansible?

Ansible

Instead of manually configuring servers, Ansible allows you to automate the process with scripts called playbooks. This saves time, reduces errors, and ensures consistency of customizations.

Key features:

  • Agentlessness. Ansible does not require additional programs (agents) to be installed on target machines. All communication is done via SSH.
  • Simplicity. Settings are written in YAML, which is easy to read and requires no programming skills.
  • Scalability. Ansible can manage a single server as well as thousands of machines.
  • Versatility. Ansible supports many tasks, from configuring web servers to managing network equipment.

How is Ansible better than other tools?

  • Easy to get started.
  • Does not require permanent agents.
  • Suitable for any operating system (Linux, Windows).
  • Large community and support.

Let's tell you how to configure Ansible.

Installing and configuring Anisable to automate your routine

First, let's install it. To do this, run the following commands on the management server:

sudo apt update

sudo apt install ansible

Ansible зачем нужен

Create an inventory file (for example, hosts.ini) that lists your servers:

[myservers]

server1 ansible_host=192.168.1.1 ansible_user=root

server2 ansible_host=192.168.1.2 ansible_user=root

Create a playbook. The playbook describes the actions that Ansible will perform on your servers. For example, to install the nginx package, create the install_nginx.yml file:

- hosts: myservers

  tasks:

    - name: Установить nginx

      apt:

        name: nginx

        state: present

Let's launch the playbook. Run the command:
ansible-playbook -i hosts.ini install_nginx.yml

Ansible плейбук

This output shows that Ansible has successfully executed the install_nginx.yml playbook, installing and running Nginx on the servers specified in the hosts.ini inventory file.

Configuring SSH access to servers

To work with Ansible, you need to configure SSH access to the servers. Make sure you can connect to the servers via SSH without entering a password. This can be done using SSH keys:

ssh-keygen -t rsa

ssh-copy-id user@192.168.1.10

ssh-copy-id user@192.168.1.11

After executing the commands, you will be able to connect to the servers without entering a password.

Verify connectivity

Now you can verify that Ansible can connect to your servers. Run the following command:

ansible -i hosts.ini webservers -m ping

If everything is configured correctly, you will see a response from each server:

Ansible настройка

Create a simple playbook

Now you can create the first playbook that will perform automated tasks. For example, let's create an install_nginx.yml file that installs Nginx on your web servers:

- hosts: webservers

  become: yes

  tasks:

    - name: Установить Nginx

      apt:

        name: nginx

        state: present

Let's launch playbook

Start playbook using the command:

ansible-playbook -i hosts.ini install_nginx.yml

Ansible запустить плейбук

This will install Nginx on all the specified servers.

Ansible is now configured and ready to use. It can be used to automate many tasks, which saves time and reduces the chance of errors. Server configuration, software upgrades, data backups can all be done automatically, allowing administrators to focus on more important aspects of infrastructure management.

Software and security updates

Regular software and security updates are equally important. Systems that are not updated on time are vulnerable to attack. Studies show that 80% of successful attacks on servers are due to outdated software. Setting up automatic updates, especially for critical components such as the operating system and server, will significantly improve security.

To rent virtual private servers (VPS), contact a proven provider LikeVPS. This service offers a variety of solutions that will help you optimize the management of your resources and ensure reliable operation of your applications.

likevps

Backup

In addition, it is worth paying attention to backups. The need for regular backups is out of the question: data loss costs businesses millions. Statistics show that 60% of companies that lose data close down within six months. Using automatic backup solutions such as rsnapshot or Bacula can help avoid catastrophic consequences.

VPS management requires attention to detail. Automation, monitoring, optimization, regular updates and backups are key aspects that will help you reduce the time and improve the efficiency of your virtual server. Using our tips, you can not only improve VPS management, but also create a sustainable and efficient infrastructure for your business.