commit de96d3c2bb47acf8dfc3b93e627f57cd18c083ce Author: Ludovic Cartier Date: Tue Sep 23 16:06:56 2025 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f486be2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2025 ansible-roles + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..9a74e2c --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,6 @@ +--- +apache2_listen_ip: 127.0.0.1 +apache2_listen_port: 8080 +apache2_user: www-data +apache2_group: www-data +apache2_documentroot_default: /var/www/html \ No newline at end of file diff --git a/files/acme.conf b/files/acme.conf new file mode 100644 index 0000000..5532371 --- /dev/null +++ b/files/acme.conf @@ -0,0 +1,6 @@ +Alias /.well-known/acme-challenge/ /var/www/letsencrypt/.well-known/acme-challenge/ + + + Order allow,deny + Allow from all + \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..3c7e3ad --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,12 @@ +--- +- name: 'apache2 reload' + systemd: + name: apache2 + state: reloaded + tags: ['apache2'] + +- name: 'apache2 restart' + systemd: + name: apache2 + state: restarted + tags: ['apache2'] diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..ab17c92 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,9 @@ +--- +galaxy_info: + author: Ludovic Cartier + description: install & configure apache2 + company: brainsys + license: MIT + min_ansible_version: 2.8 + issue_tracker_url: https://git.brainsys.io/ansible-roles/apache/issues + github_branch: main diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..d814f7f --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,57 @@ +--- +- name: 'apache2 | update ports' + replace: + path: /etc/apache2/ports.conf + regexp: '^Listen 80' + replace: "Listen {{ apache2_listen_ip }}:{{ apache2_listen_port }}" + backup: yes + notify: + - apache2 restart + tags: + - apache2 + - apache2_configure + +- name: 'apache2 | configuration | defaults modules' + community.general.apache2_module: + name: "{{ item.module }}" + state: "{{ item.state }}" + ignore_configcheck: true + loop: + - module: headers + state: present + - module: rewrite + state: present + - module: proxy + state: present + - module: proxy_fcgi + state: present + - module: proxy_http + state: present + notify: + - apache2 restart + tags: + - apache2 + - apache2_configure + +- name: 'apache2 | configuration | create ACME directory' + file: + path: /var/www/letsencrypt/.well-known/acme-challenge/ + state: directory + owner: www-data + group: www-data + mode: '0755' + tags: + - apache2 + - apache2_configure + +- name: 'apache2 | configuration | push ACME configuration' + copy: + src: 'acme.conf' + dest: /etc/apache2/conf-enabled + owner: root + mode: 644 + notify: + - apache2 restart + tags: + - apache2 + - apache2_configure diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..be252c3 --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,32 @@ +--- +- name: "apache2 | apt update cache" + apt: + update_cache: yes + cache_valid_time: 86400 + tags: + - apache2 + - apache2_install + +- name: "apache2 | install packages" + apt: + name: "{{ item }}" + update_cache: true + state: present + with_items: + - apache2 + - apache2-bin + - apache2-data + - apache2-utils + register: is_apache2 + tags: + - apache2 + - apache2_install + +- name: "apache2 | remove default vhost" + file: + path: "/etc/apache2/sites-enabled/000-default.conf" + state: absent + tags: + - apache2 + - apache2_install + diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..1d17c57 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,9 @@ +--- +- name: "apache2 | installation" + include_tasks: install.yml + +- name: "apache2 | custom configuration" + include_tasks: configure.yml + +# - name: "apache2 | configure vhost" +# include_tasks: vhost.yml diff --git a/tasks/vhost.yml b/tasks/vhost.yml new file mode 100644 index 0000000..b3dff43 --- /dev/null +++ b/tasks/vhost.yml @@ -0,0 +1,65 @@ +--- +- name: 'apache2 | vhost | configure vhosts' + template: + src: "{{ item.value.template | default('vhost.conf.j2') }}" + dest: "/etc/apache2/sites-available/{{ item.key }}.conf" + owner: root + group: root + mode: 0644 + loop: "{{ apache2_vhosts | dict2items }}" + notify: + - apache2 reload + tags: + - apache2 + - apache2_vhost + +- name: 'apache2 | vhost | enable vhosts' + file: + src: "/etc/apache2/sites-available/{{ item.key }}.conf" + dest: "/etc/apache2/sites-enabled/{%if item.value.priority is defined%}{{ item.value.priority }}-{%endif%}{{ item.key }}.conf" + state: link + loop: "{{ apache2_vhosts | dict2items }}" + when: item.value.enabled is not defined or item.value.enabled + notify: + - apache2 reload + tags: + - apache2 + - apache2_vhost + +- name: 'apache2 | vhost | configure DocumentRoot' + file: + path: "{{ item.value.documentroot.path | default(apache2_documentroot_default) }}" + state: directory + owner: "{{ item.value.documentroot.user | default(apache2_user) }}" + group: "{{ item.value.documentroot.group | default(apache2_group) }}" + loop: "{{ apache2_vhosts | dict2items }}" + loop_control: + label: "{{ item.value.documentroot | default([]) }}" + when: + - item.value.enabled is undefined or item.value.enabled + - item.value.documentroot is defined + - item.value.documentroot != False + notify: + - apache2 reload + tags: + - apache2 + - apache2_vhost + +- name: 'apache2 | vhost | configure logs directory' + file: + path: "/var/log/apache2/{{ item.value.servername }}" + state: directory + owner: root + group: adm + loop: "{{ apache_vhosts | dict2items }}" + loop_control: + label: "{{ item.value.servername | default([]) }}" + when: + - item.value.enabled is undefined or item.value.enabled + - item.value.documentroot is defined + - item.value.documentroot != False + notify: + - apache2 reload + tags: + - apache2 + - apache2_vhost