How to deploy a webserver apache httpd on Debian-like systems with Ansible? I’m going to show you a live Playbook with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Deploy a web server apache httpd on Debian-like systems
- install packages =>
ansible.builtin.apt
- custom index.html =>
ansible.builtin.copy
- start service =>
ansible.builtin.service
- open firewall =>
community.general.ufw
Today we’re talking about how to Deploy a web server apache httpd on Debian-like Linux systems.
The full process requires six steps that you could automate with different Ansible modules.
Firstly you need to install the apache2
package and dependency using the ansible.builtin.apt
Ansible module.
Secondly, you need to create the custom index.html with ansible.builtin.copy
Ansible module. You could upgrade this step using the template
module.
Thirsty you need to start the apache2
service and enable it on boot and all the dependant using the ansible.builtin.service
Ansible module.
Fourthly you need to open the relevant firewall service-related ports using the community.general.ufw
Ansible module.
Playbook
How to deploy a web server apache httpd on Debian-like systems with Ansible Playbook.
code
---
- name: setup webserver
hosts: all
become: true
tasks:
- name: apache installed
ansible.builtin.apt:
name: apache2
update_cache: true
state: latest
- name: custom index.html
ansible.builtin.copy:
dest: "/var/www/html/index.html"
content: |
Custom Web Page
- name: apache2 service enabled
ansible.builtin.service:
name: apache2
enabled: true
state: started
- name: open firewall
community.general.ufw:
rule: allow
port: 80
proto: tcp
execution
ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu/inventory services/httpd_debian.yml
PLAY [setup webserver] ****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [ubuntu.example.com]
TASK [apache installed] ***************************************************************************
changed: [ubuntu.example.com]
TASK [custom index.html] **************************************************************************
changed: [ubuntu.example.com]
TASK [apache2 service enabled] ********************************************************************
ok: [ubuntu.example.com]
TASK [open firewall] ******************************************************************************
changed: [ubuntu.example.com]
PLAY RECAP ****************************************************************************************
ubuntu.example.com : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu/inventory services/httpd_debian.yml
PLAY [setup webserver] ****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [ubuntu.example.com]
TASK [apache installed] ***************************************************************************
ok: [ubuntu.example.com]
TASK [custom index.html] **************************************************************************
ok: [ubuntu.example.com]
TASK [apache2 service enabled] ********************************************************************
ok: [ubuntu.example.com]
TASK [open firewall] ******************************************************************************
ok: [ubuntu.example.com]
PLAY RECAP ****************************************************************************************
ubuntu.example.com : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
ansible-pilot $ ssh [email protected]
The authenticity of host 'ubuntu.example.com (192.168.0.191)' can't be established.
ECDSA key fingerprint is SHA256:SLtrfIjRKhuJtdTxjJ4V9yk+o/gO3MZi59KHehOQ3Ao.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ubuntu.example.com,192.168.0.191' (ECDSA) to the list of known hosts.
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
$ sudo su
root@ubuntu:/home/devops# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
root@ubuntu:/home/devops# apt list apache2
Listing... Done
apache2/focal-updates 2.4.41-4ubuntu3.8 amd64
apache2/focal-updates 2.4.41-4ubuntu3.8 i386
root@ubuntu:/home/devops# apt list apache2 --installed
Listing... Done
root@ubuntu:/home/devops# dpkg -l | grep apache2
root@ubuntu:/home/devops# cat /var/www/html/index.html
cat: /var/www/html/index.html: No such file or directory
root@ubuntu:/home/devops# ls -al /var/www
ls: cannot access '/var/www': No such file or directory
root@ubuntu:/home/devops#
after execution
ansible-pilot $ ssh [email protected]
Last login: Mon Feb 14 14:26:02 2022 from 192.168.0.101
$ sudo su
root@ubuntu:/home/devops# apt list apache2
Listing... Done
apache2/focal-updates,focal-security,now 2.4.41-4ubuntu3.9 amd64 [installed]
apache2/focal-updates,focal-security 2.4.41-4ubuntu3.9 i386
root@ubuntu:/home/devops# dpkg -l | grep apache2
ii apache2 2.4.41-4ubuntu3.9 amd64 Apache HTTP Server
ii apache2-bin 2.4.41-4ubuntu3.9 amd64 Apache HTTP Server (modules and other binary files)
ii apache2-data 2.4.41-4ubuntu3.9 all Apache HTTP Server (common files)
ii apache2-utils 2.4.41-4ubuntu3.9 amd64 Apache HTTP Server (utility programs for web servers)
root@ubuntu:/home/devops# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-02-14 14:25:31 UTC; 1min 20s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 3182 (apache2)
Tasks: 55 (limit: 1071)
Memory: 5.1M
CGroup: /system.slice/apache2.service
├─3182 /usr/sbin/apache2 -k start
├─3185 /usr/sbin/apache2 -k start
└─3186 /usr/sbin/apache2 -k start
Feb 14 14:25:31 ubuntu systemd[1]: Starting The Apache HTTP Server...
Feb 14 14:25:31 ubuntu systemd[1]: Started The Apache HTTP Server.
root@ubuntu:/home/devops# cat /var/www/html/index.html
Custom Web Page
root@ubuntu:/home/devops# ls -al /var/www/
total 12
drwxr-xr-x 3 root root 4096 Feb 14 14:25 .
drwxr-xr-x 14 root root 4096 Feb 14 14:25 ..
drwxr-xr-x 2 root root 4096 Feb 14 14:25 html
root@ubuntu:/home/devops#
Conclusion
Now you know how to deploy a webserver apache httpd on Debian-like systems with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate