How to Deploy the Web Server In Ansible

To Deploy the Web Server In Ansible

Introduction:

Ansible is an open-source automation platform that manages software installations, updates, configurations, and tasks within your network environment.

A web server is a computer that runs websites. It's a computer program that distributes web pages as they are requisitioned. The basic objective of the web server is to store, process and deliver web pages to the users. A Domain Name Server (DNS) converts this URL to an IP Address

Check the Os version by using following command:

[root@linuxhelp ~]# lsb_release -d
Description:	CentOS Linux release 7.6.1810 (Core)  

Install httpd server:

[root@linuxhelp ~]# yum install httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * epel: mirror2.totbb.net
 * extras: centos.excellmedia.net
 * updates: mirror.vanehost.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-97.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-97.el7.centos for package: httpd-2.4.6-97.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-7.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-97.el7.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================
 Package                   Arch                 Version                           Repository             Size
==============================================================================================================
Installing:
 httpd                     x86_64               2.4.6-97.el7.centos               updates               2.7 M
Installing for dependencies:
 apr                       x86_64               1.4.8-7.el7                       base                  104 k
 apr-util                  x86_64               1.5.2-6.el7                       base                   92 k
 httpd-tools               x86_64               2.4.6-97.el7.centos               updates                93 k
Transaction Summary
==============================================================================================================
Install  1 Package (+3 Dependent packages)
Total download size: 3.0 M
Installed size: 9.9 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): apr-1.4.8-7.el7.x86_64.rpm                                                      | 104 kB  00:00:00     
(2/4): apr-util-1.5.2-6.el7.x86_64.rpm                                                 |  92 kB  00:00:00     
(3/4): httpd-tools-2.4.6-97.el7.centos.x86_64.rpm                                      |  93 kB  00:00:00     
(4/4): httpd-2.4.6-97.el7.centos.x86_64.rpm                                            | 2.7 MB  00:00:00     
--------------------------------------------------------------------------------------------------------------
Total                                                                         2.1 MB/s | 3.0 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-7.el7.x86_64                                                                     1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                                2/4 
  Installing : httpd-tools-2.4.6-97.el7.centos.x86_64                                                     3/4 
  Installing : httpd-2.4.6-97.el7.centos.x86_64                                                           4/4 
  Verifying  : httpd-2.4.6-97.el7.centos.x86_64                                                           1/4 
  Verifying  : apr-1.4.8-7.el7.x86_64                                                                     2/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                3/4 
  Verifying  : httpd-tools-2.4.6-97.el7.centos.x86_64                                                     4/4 
Installed:
  httpd.x86_64 0:2.4.6-97.el7.centos                                                                          
Dependency Installed:
  apr.x86_64 0:1.4.8-7.el7     apr-util.x86_64 0:1.5.2-6.el7     httpd-tools.x86_64 0:2.4.6-97.el7.centos    
Complete!

Add the firewall service:

[root@linuxhelp ~]# firewall-cmd --permanent --add-service=http
success

Reload the firewall:

[root@linuxhelp ~]# firewall-cmd --reload
success

Start the apache server:

[root@linuxhelp ~]# systemctl start httpd

Check the status of the apacheche service

[root@linuxhelp ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-03-31 17:40:13 PDT; 14s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 64215 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   CGroup: /system.slice/httpd.service
           ├─64215 /usr/sbin/httpd -DFOREGROUND
           ├─64219 /usr/sbin/httpd -DFOREGROUND
           ├─64220 /usr/sbin/httpd -DFOREGROUND
           ├─64221 /usr/sbin/httpd -DFOREGROUND
           ├─64222 /usr/sbin/httpd -DFOREGROUND
           └─64223 /usr/sbin/httpd -DFOREGROUND
Mar 31 17:40:13 linuxhelp.localdomain systemd[1]: Starting The Apache HTTP Server...
Mar 31 17:40:13 linuxhelp.localdomain httpd[64215]: AH00558: httpd: Could not reliably determine the ser...age
Mar 31 17:40:13 linuxhelp.localdomain systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

Create a directory:

[root@linuxhelp ~]# mkdir webserver

Switch to webserver directory:

[root@linuxhelp ~]# cd webserver/

Open yaml file:

[root@linuxhelp webserver]# vi main.yaml

Open hosts file:

[root@linuxhelp webserver]# vi hosts

Configure the ansible.config file:

[root@linuxhelp webserver]# vi ansible.cfg

Check the syntax of the main.yaml file:

[root@linuxhelp webserver]# ansible-playbook main.yaml --syntax-check
playbook: main.yaml

Execute the main file:

[root@linuxhelp webserver]# ansible-playbook main.yaml 
PLAY [webserver playbook] ************************************************************************************
TASK [Gathering Facts] ***************************************************************************************
ok: [Client]
TASK [install httpd] *****************************************************************************************
ok: [Client]
TASK [enable and start httpd] ********************************************************************************
ok: [Client]
TASK [creating the index.html] *******************************************************************************
ok: [Client]
TASK [open firewall port] ************************************************************************************
ok: [Client]
PLAY RECAP ***************************************************************************************************
Client                     : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Check the output from client server:

[root@linuxhelp webserver]# curl http://192.168.6.111/
welcome to linuxhelp[root@linuxhelp webserver]#

With this method, Deploy webserver in Ansible comes to an end.

FAQ
Q
How does a client-server work?
A
Clients, taking the form of laptops, desktops, tablets, or smartphones, then request a file or application from the remote server. The server hears the request, verifies credentials, and if everything checks out, serves the client the requested file. The communication between clients and servers is a two-way street.
Q
What is Playbook?
A
The real power of Ansible, however, is in learning how to use playbooks to run multiple, complex tasks against a set of targeted hosts in an easily repeatable manner.
Q
What is the most used Web server?
A
Apache
We'll dive into Apache first since it was released first. After Tim Berners-Lee's CERN HTTPd and NCSA HTTPd in the first couple of years of the internet, Apache – first released in 1995 – quickly conquered the market and became the world's most popular web server.
Q
What is the function of a web server?
A
A web server is a software and hardware that uses HTTP (Hypertext Transfer Protocol) and other protocols to respond to client requests made over the World Wide Web. The main job of a web server is to display website content through storing, processing, and delivering webpages to users.
Q
What is a Web server?
A
A web server is a computer that runs websites. It's a computer program that distributes web pages as they are requisitioned. The basic objective of the web server is to store, process, and deliver web pages to the users. A Domain Name Server (DNS) converts this URL to an IP Address.