Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andypendragon/learn-ansible-virtualhost-apache2
An assignment for our sys1 classes, aimed at understanding Ansible and the use of Apache2
https://github.com/andypendragon/learn-ansible-virtualhost-apache2
Last synced: 27 days ago
JSON representation
An assignment for our sys1 classes, aimed at understanding Ansible and the use of Apache2
- Host: GitHub
- URL: https://github.com/andypendragon/learn-ansible-virtualhost-apache2
- Owner: AndyPendragon
- Created: 2023-05-21T17:16:13.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-05-26T10:37:42.000Z (over 1 year ago)
- Last Synced: 2024-04-20T00:51:03.626Z (8 months ago)
- Language: Jinja
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ansible by Red Hat
Ansible is an open-source automation tool that simplifies various IT tasks such as cloud provisioning, configuration management, application deployment, and intra-service orchestration. It offers a simple and powerful way to automate IT operations.
With Ansible, you can use a single automation language that caters to different roles within an IT team, including systems administrators, network administrators, developers, and managers.
# Use Ansible to automate the deployment of Apache virtual hosts.
## Prerequisites
To get started, you need to install Ansible. You can follow the tutorial below:
**ansible-tuto**
```sh
git clone https://github.com/leucos/ansible-tuto.git
```
## UsageFollow the instructions below to use this repository:
1. Clone the repository:
```sh
git clone https://github.com/AndyPendragon/sys1-ansible-virtualHost-Apache2.git
```2. Run the Ansible playbook:
```sh
ansible-playbook deploy_apache.yml -i inventories/production/hosts
```## Technology
This project is built using Python and utilizes Jinja2 for template rendering with the .j2 extension.
Feel free to modify and customize the playbook and templates according to your specific requirements.
I hope this revision meets your expectations. Let me know if you need further assistance!
## Details
There are 3 important files for Ansible:
**hosts**
The hosts file is an inventory file used by Ansible to specify the target hosts or groups of hosts on which the playbook will be executed. In this case, the [webserver] group is defined with localhost as the host. This means that the playbook will be applied to the local machine where Ansible is being run, simulating the web server environment.
```sh
[webserver]
localhost ansible_connection=local
```**virtualhost.conf.j2**
The virtualhost.conf.j2 file is a Jinja2 template used to generate the Apache virtual host configuration files. It contains the template code with placeholders ({{ server_name }}, {{ server_alias }}, {{ document_root }}) that will be replaced with the actual values during the playbook execution. This allows for dynamic configuration of the virtual hosts based on the specified variables.
```jinja
ServerName {{ server_name }}
ServerAlias www.{{ server_alias }}
DocumentRoot {{ document_root }}# ...
```
**deploy_apache.yml**
The deploy_apache.yml file is the Ansible playbook responsible for deploying the Apache virtual host configuration. It consists of a series of tasks executed in order. The playbook performs tasks such as installing Apache, creating the root directory for virtual hosts, copying the virtual host configuration files from the template, enabling the virtual hosts, and restarting Apache. It uses modules and directives provided by Ansible to interact with the target hosts and perform the necessary configurations.
By combining these files, you can automate the deployment of Apache virtual hosts with custom configurations using Ansible. The hosts file specifies where the playbook will be applied, the virtualhost.conf.j2 file defines the template for virtual host configurations, and the deploy_apache.yml playbook orchestrates the entire deployment process.
```yaml
---
- name: Deploy Apache virtualHost configuration
hosts: all
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present- name: Create root directory for virtual hosts
file:
path: /var/www/html
state: directory- name: Copy virtual host configuration files
template:
src: ../templates/virtualhost.conf.j2
dest: /etc/apache2/sites-available/{{ item }}.conf
with_items:
- api.hei.school
- back.hei.school
- front.hei.school
vars:
server_name: "{{ item }}"
server_alias: "www.{{ item }}"
document_root: "/var/www/html/{{ item }}"- name: Enable virtual hosts
command: a2ensite {{ item }}.conf
with_items:
- api.hei.school
- back.hei.school
- front.hei.school- name: Restart Apache
service:
name: apache2
state: restarted
```