Skip to content

Commit 350203e

Browse files
committed
added ansible configuration
1 parent 6f231b8 commit 350203e

File tree

11 files changed

+245
-2
lines changed

11 files changed

+245
-2
lines changed

.github/workflows/ansible.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Ansible
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
paths:
7+
- ansible/**
8+
pull_request:
9+
paths:
10+
- ansible/**
11+
12+
jobs:
13+
lint:
14+
name: "Ansible Lint"
15+
runs-on: ubuntu-latest
16+
timeout-minutes: 10
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
- name: Run Ansible-lint
21+
uses: ansible/ansible-lint-action@v6
22+
with:
23+
target: "./ansible"

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ Status of last event for Terraform workflow: <br>
1313
- terraform plan
1414
- terraform apply -auto-approve
1515
3. check
16-
- curl http://localhost:8080/
17-
- curl http://localhost:8080/healthz
16+
- ```curl http://localhost:8080/```
17+
- ```curl http://localhost:8080/healthz```
1818

19+
20+
### Ansible
21+
22+
```Testing in ubuntu 20.04```
23+
24+
1. Requirements: installed ansible
25+
2. ```cd ansible``` and run command:
26+
- ```ansible-playbook -i inventory/containers.ini playbooks/site.yml```
27+
3. check
28+
- ```curl http://localhost/```
29+
- ```curl http://localhost/healthz```

ansible/inventory/containers.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[web]
2+
localhost ansible_connection=local

ansible/playbooks/site.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: Deploy web role
3+
hosts: web
4+
become: true
5+
6+
roles:
7+
- roles/web
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
# defaults file for web
3+
app_env: "dev"
4+
apt_php_version: "7.4"
5+
# ubuntu run nginx user
6+
php_owner: "nginx"
7+
php_group: "nginx"

ansible/roles/web/files/index.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
header('Content-Type: application/json');
3+
echo json_encode([
4+
"status" => "ok",
5+
"service" => "php",
6+
"env" => getenv('APP_ENV') ?: 'dev'
7+
]);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
# handlers file for web
3+
- name: restart nginx
4+
service:
5+
name: nginx
6+
state: restarted
7+
8+
- name: restart php-fpm
9+
service:
10+
name: "php{{ apt_php_version }}-fpm"
11+
state: restarted

ansible/roles/web/meta/main.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
galaxy_info:
2+
author: Anatolii
3+
description: Configure Nginx + PHP-FPM
4+
company: WebIncorp
5+
6+
# If the issue tracker for your role is not on github, uncomment the
7+
# next line and provide a value
8+
# issue_tracker_url: http://example.com/issue/tracker
9+
10+
# Choose a valid license ID from https://spdx.org - some suggested licenses:
11+
# - BSD-3-Clause (default)
12+
# - MIT
13+
# - GPL-2.0-or-later
14+
# - GPL-3.0-only
15+
# - Apache-2.0
16+
# - CC-BY-4.0
17+
license: license MIT
18+
19+
min_ansible_version: "2.1"
20+
21+
# If this a Container Enabled role, provide the minimum Ansible Container version.
22+
# min_ansible_container_version:
23+
24+
#
25+
# Provide a list of supported platforms, and for each platform a list of versions.
26+
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
27+
# To view available platforms and versions (or releases), visit:
28+
# https://galaxy.ansible.com/api/v1/platforms/
29+
#
30+
platforms:
31+
- name: Ubuntu
32+
# - name: Fedora
33+
# versions:
34+
# - all
35+
# - 25
36+
# - name: SomePlatform
37+
# versions:
38+
# - all
39+
# - 1.0
40+
# - 7
41+
# - 99.99
42+
43+
galaxy_tags: []
44+
# List tags for your role here, one per line. A tag is a keyword that describes
45+
# and categorizes the role. Users find roles by searching for tags. Be sure to
46+
# remove the '[]' above, if you add tags to this list.
47+
#
48+
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
49+
# Maximum 20 tags per role.
50+
51+
dependencies: []
52+
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
53+
# if you add dependencies to this list.

ansible/roles/web/tasks/main.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
# tasks file for web
3+
- name: Install required packages
4+
apt:
5+
name:
6+
- nginx
7+
- "php{{ apt_php_version }}-fpm"
8+
state: present
9+
update_cache: yes
10+
11+
- name: Deploy nginx config
12+
template:
13+
src: nginx.conf.j2
14+
dest: /etc/nginx/conf.d/website.conf
15+
owner: root
16+
group: root
17+
mode: 0644
18+
notify:
19+
- restart nginx
20+
21+
- name: ensures /var/www/html dir exists
22+
file:
23+
path: "/var/www/html"
24+
state: directory
25+
owner: www-data
26+
group: www-data
27+
mode: 0755
28+
29+
# setting the www.conf .
30+
- name: Set permissions on socket - owner
31+
lineinfile:
32+
dest: "/etc/php/{{ apt_php_version }}/fpm/pool.d/www.conf"
33+
regexp: '^;?listen.owner'
34+
line: "listen.owner = {{ php_owner }}"
35+
notify: restart php-fpm
36+
37+
- name: Set permissions on socket - group
38+
lineinfile:
39+
dest: "/etc/php/{{ apt_php_version }}/fpm/pool.d/www.conf"
40+
regexp: '^;?listen.group'
41+
line: 'listen.group = {{ php_group }}'
42+
notify: restart php-fpm
43+
44+
- name: Set permissions on socket - mode
45+
lineinfile:
46+
dest: "/etc/php/{{ apt_php_version }}/fpm/pool.d/www.conf"
47+
regexp: '^;?listen.mode'
48+
line: 'listen.mode = 0660'
49+
notify: restart php-fpm
50+
51+
- name: Deploy index.php
52+
copy:
53+
src: index.php
54+
dest: /var/www/html/index.php
55+
owner: www-data
56+
group: www-data
57+
mode: 0644
58+
notify:
59+
- restart php-fpm
60+
61+
- name: Ensure php-fpm service is enabled and started
62+
service:
63+
name: php{{ apt_php_version }}-fpm
64+
state: started
65+
enabled: yes
66+
67+
- name: Ensure nginx service is enabled and started
68+
service:
69+
name: nginx
70+
state: started
71+
enabled: yes
72+
73+
- name: Deploy logrotate config for nginx logs
74+
copy:
75+
content: |
76+
/var/log/nginx/*log {
77+
daily
78+
missingok
79+
rotate 14
80+
compress
81+
delaycompress
82+
notifempty
83+
create 0640 www-data adm
84+
sharedscripts
85+
postrotate
86+
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
87+
endscript
88+
}
89+
dest: /etc/logrotate.d/nginx
90+
owner: root
91+
group: root
92+
mode: 0644
93+
notify:
94+
- restart nginx
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
root /var/www/html;
5+
index index.php index.html;
6+
7+
location /healthz {
8+
default_type application/json;
9+
return 200 '{"status":"ok","service":"nginx","env":"{{ app_env }}"}';
10+
}
11+
12+
location / {
13+
try_files $uri $uri/ /index.php?$query_string;
14+
}
15+
16+
location ~ \.php$ {
17+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
18+
fastcgi_pass unix:/run/php/php-fpm.sock;
19+
fastcgi_index index.php;
20+
include fastcgi_params;
21+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
22+
}
23+
24+
access_log /var/log/nginx/access-web.log;
25+
error_log /var/log/nginx/error-web.log warn;
26+
}

0 commit comments

Comments
 (0)