How To Automate Initial Server Setup of Multiple Ubuntu 22.04 Servers Using Ansible
Automation is a key aspect of modern infrastructure management. It allows you to quickly and easily perform repetitive tasks across multiple servers with minimal human intervention. Ansible is a popular automation tool that enables you to automate the initial server setup of multiple Ubuntu 22.04 servers with ease.
In this guide, we will walk you through the steps to automate the initial server setup of multiple Ubuntu 22.04 servers using Ansible. We will cover the installation of Ansible, creating an inventory file, configuring SSH access, setting up sudo access, and installing some common packages.
Prerequisites
Before we begin, you will need the following:
- Multiple Ubuntu 22.04 servers.
- A user account with sudo privileges on each server.
- Ansible installed on your local machine.
Step 1: Installing Ansible
Ansible is not installed by default on Ubuntu 22.04. To install Ansible on your local machine, follow these steps:
- Open a terminal window on your local machine.
- Update the package lists and install Ansible with the following command:
sudo apt update
sudo apt install ansible
- Verify that Ansible is installed by running the following command:
ansible --version
You should see the version of Ansible that you installed.
Step 2: Creating an Inventory File
The inventory file is a list of all the servers that Ansible should manage. This file is written in INI format and can be located anywhere on your local machine. To create an inventory file, follow these steps:
- Open a terminal window on your local machine.
- Create a new text file with the following command:
nano inventory.ini
- Add the IP addresses or hostnames of each server to the file, one per line:
[webserver]
192.168.1.101
192.168.1.102
[database]
192.168.1.103
192.168.1.104
In this example, we have two groups of servers: webserver and database. The IP addresses of the servers in each group are listed below the group name.
- Save and close the file.
Step 3: Configuring SSH Access
In order for Ansible to manage your servers, it needs to be able to connect to them using SSH. To configure SSH access, follow these steps:
- Generate an SSH key pair on your local machine with the following command:
ssh-keygen
- Copy the public key to each server with the following command:
ssh-copy-id username@server_ip_address
- Test that you can connect to each server with the following command:
ssh username@server_ip_address
Step 4: Setting Up Sudo Access
In order to perform certain tasks, such as installing packages, Ansible needs to be able to run commands with sudo privileges. To set up sudo access, follow these steps:
- Open a terminal window on each server.
- Add your user account to the sudo group with the following command:
sudo usermod -aG sudo username
- Test that your user account has sudo access with the following command:
sudo whoami
You should see “root” as the output.
Step 5: Installing Common Packages
Now that Ansible is set up and configured to manage your servers, we can install some common packages. To do this, we will create a playbook.
- Create a new text file with the following command:
nano playbook.yml
- Add the following code to the file:
---
- name: Install common packages
hosts: all
become: true
tasks:
- name: Update package lists
apt:
update_cache: yes
- name: Install packages
apt:
name:
- nano
- git
- curl
- wget
This playbook will update the package lists and install the Nano, Git, Curl, and Wget packages on all servers in the inventory file.
- Save and close the file.
- Run the playbook with the following command:
ansible-playbook -i inventory.ini playbook.yml
Ansible will connect to each server, update the package lists, and install the specified packages.
Step 6: Creating a Custom User
By default, Ubuntu 22.04 comes with a user named “ubuntu”. It is recommended that you create a custom user with a unique username and password for security reasons. To create a new user, follow these steps:
- Open a terminal window on each server.
- Create a new user with the following command, replacing “newuser” with your desired username:
sudo adduser newuser
- Set a password for the new user when prompted.
- Add the new user to the sudo group with the following command:
sudo usermod -aG sudo newuser
- Test that the new user has sudo access with the following command:
sudo whoami
You should see “root” as the output.
Step 7: Configuring Firewall
A firewall is an essential security tool that prevents unauthorized access to your servers. Ubuntu 22.04 comes with UFW (Uncomplicated Firewall) pre-installed. To configure UFW, follow these steps:
- Open a terminal window on each server.
- Enable UFW with the following command:
sudo ufw enable
- Allow SSH access with the following command:
sudo ufw allow ssh
- Allow HTTP and HTTPS access (if applicable) with the following command:
sudo ufw allow http
sudo ufw allow https
- Verify that the firewall is configured correctly with the following command:
sudo ufw status verbose
You should see the rules that you just configured listed.
Step 8: Configuring Timezone
By default, Ubuntu 22.04 is set to the UTC timezone. To change the timezone, follow these steps:
- Open a terminal window on each server.
- List the available time zones with the following command:
timedatectl list-timezones
- Set the timezone to your desired timezone with the following command, replacing “America/New_York” with your desired timezone:
sudo timedatectl set-timezone America/New_York
- Verify that the timezone is set correctly with the following command:
timedatectl
You should see the timezone that you just set listed.
Conclusion
In this guide, we have shown you how to automate the initial server setup of multiple Ubuntu 22.04 servers using Ansible. We covered the installation of Ansible, creating an inventory file, configuring SSH access, setting up sudo access, installing some common packages, creating a custom user, configuring firewall, and configuring timezone. With this knowledge, you can easily automate the setup and configuration of your infrastructure, saving you time and effort.