Lab 1
Lab 1
&
Initiation à l’automatisation
4ArcTIC
2024-2025
Introduction :
Nginx and Ansible are both widely used tools in the world of IT. Nginx is known for its
exceptional performance and scalability as an open-source web server, while Ansible is a
powerful configuration management tool, valued for its agentless architecture that doesn’t
require additional software on nodes and uses SSH to execute automation tasks, with YAML
files to define configuration details. In this lab, we will explain how to install Ansible on Ubuntu
and how to deploy Nginx using an Ansible playbook.
Prerequisites :
• Two virtual machines running Ubuntu 22.04 with OpenSSH server installed : one is the
master node, and the other is the worker node.
• Both machines are accessible via public IP addresses.
• A non-root user with sudo privileges is set up on both machines.
Goals :
1- The configuration and setup of Ansible on a control machine include setting up SSH and
preparing the inventory file to define the managed hosts.
1
Step 1 - Installing Ansible
To begin using Ansible as a means of managing your server infrastructure, you need to install
the Ansible software on the machine that will serve as the Ansible control node.
1.1 From your control node, run the following command to include the official project’s PPA
(personal package archive) in your system’s list of sources :
1.2 Next, refresh your system’s package index so that it is aware of the packages available in
the newly included PPA.
Following this update, you can install the Ansible software with :
$ ansible --version
➔ Your Ansible control node now has all of the software required to administer your hosts.
Next, we will go over how to add your hosts to the control node’s inventory file so that it can
control them.
2
Step 2 - Set Up SSH Keys
The first step is to create a key pair on the Control machine :
$ ssh-keygen
After entering the command, you should see the following output :
Press enter to save the key pair into the .ssh/ subdirectory in your home directory, or specify an
alternate path.
Here you optionally may enter a secure passphrase, which is highly recommended.(But we
suggest without passphrase for learning purpose) A passphrase adds an additional layer of
security to prevent unauthorized users from logging in.
3
3.2 Before copying your public key to the worker machine, check that the firewall is disabled
on both machines, if it is not, disable it with the following commands :
3.3 Once openssh-server is installed and the service is running, you can copy your public key
to the worker machine using the ssh-copy-id command.
$ ssh-copy-id username@remote_host
You may see the following message, answer yes to accept and add this key to the list of known
hosts (~/.ssh/known_hosts).
And once you enter the worker node password, key is added.
4
If you were able to log into your account using SSH without a password, you have successfully
configured SSH-key-based authentication to your account. However, your password-based
authentication mechanism is still active on your worker node.
Ansible uses the standard SSH protocol to communicate with the nodes it manages. However,
these nodes need to be declared in an inventory file. The default inventory file is located at
/etc/ansible/hosts, but you are free to create inventory files in any location that suits your needs,
in either INI or YAML format.
In this case, you’ll need to provide the path to your custom inventory file with the -i parameter
when running Ansible commands and playbooks. Using per-project inventory files is a good
practice to minimize the risk of running a playbook on the wrong group of servers.
5. Create your own inventory file in the /etc/ansible directory and add the IP addresses of the
worker node to this inventory file.
5
In the same directory as your « nginx_setup.yml » playbook, create a subdirectory named
« templates ». Then, in this directory, create the file « index.html.j2 » with the following
customized content :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Welcome to Nginx </title>
</head>
<body>
<h1> Welcome to Nginx ! </h1>
<p> Congratulations, the Nginx web server has been installed and is running correctly on your
worker node.</p>
</body>
</html>
Note : This file will be used as the source for a task that employs the template module to create
the /var/www/html/index.html file on the worker machine from the index.html.j2 template.
Installing Nginx.
To fix this issue, you need to explicitly specify the remote user in the execution command using
the -u option, followed by the username on the remote machine.
6
For example, to run the playbook as the worker user on the remote machine, use the following
command :
After successfully running your playbook, open the web browser on the worker node, enter the
IP address, and verify that the customized Nginx web page is displayed.