Skip to content

Commit

Permalink
06.Ansible.WorkShop added
Browse files Browse the repository at this point in the history
  • Loading branch information
xaos613 committed Jan 4, 2025
1 parent b7c4d3e commit 3d75d96
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 0 deletions.
Empty file.
6 changes: 6 additions & 0 deletions Sholomitski_Dmitry/06.Ansible.WorkShop/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[defaults]
inventory = ~/ansible/inv.yaml
# remote_tmp = /tmp/.ansible/tmp
forks = 3
host_key_checking = false
callbacks_enabled = profile_tasks, timer
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q [email protected] -p 32510"'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q [email protected] -p 32510"'
38 changes: 38 additions & 0 deletions Sholomitski_Dmitry/06.Ansible.WorkShop/inv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
all_workers:
children:
work_sa:
vars:
ansible_user: root
hosts:
host_19:
ansible_host: 192.168.202.19
host_20:
ansible_host: 192.168.202.20

web:
hosts:
web_21:
ansible_host: 192.168.56.21
ansible_user: root
web_22:
ansible_host: 192.168.56.22
ansible_user: root



db_all:
vars:
# ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q [email protected] -p 32510"'
hosts:
mysql:
ansible_host: 192.168.201.10
ansible_user: root


jump_sa:
vars:
env: "it-academy"
hosts:
jump_bastion:
ansible_host: 178.124.206.53
ansible_user: jump_sa
80 changes: 80 additions & 0 deletions Sholomitski_Dmitry/06.Ansible.WorkShop/mysql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
- hosts: mysql
vars:
app_packages:
- mysql-server
- mysql-client
- python3-pymysql
- python3-mysqldb
db_name: "{{ name_db | default('test') }}"
db_user: "{{ user_db | default('test') }}"
db_pass: "{{ pass_db | default('test') }}"
pre_tasks:
- name: Validate
debug:
msg:
- "DB host: {{ ansible_host }}"
- "DB name: {{ db_name }}"
- "DB user: {{ db_user }}"
- "DB pass: {{ db_pass }}"
tags: always
tasks:
- name: MySQL. Install packages
apt:
name: "{{ app_packages }}"
state: latest
update_cache: true
environment:
DEBIAN_FRONTEND: noninteractive
tags: install
##################
- name: MySQL. add bind-address
ini_file:
dest: /etc/mysql/my.cnf
section: mysqld
option: "bind-address"
value: "{{ ansible_host }}"
register: restart_needed

- name: Check variable
debug:
msg: "{{ restart_needed }}"

- name: restart mysql if necessary
command: service mysql restart
when: restart_needed.changed

- name: Test connect
wait_for:
host: "{{ ansible_host }}"
port: 3306
timeout: 3

- name: "Remove {{ db_name }} database"
mysql_db:
name: "{{ db_name }}"
state: absent
login_unix_socket: /var/run/mysqld/mysqld.sock
tags: never

- name: "Remove {{ db_user }} user"
mysql_user:
name: "{{ db_user }}"
host: "%"
state: absent
login_unix_socket: /run/mysqld/mysqld.sock
tags: never

- mysql_db:
name: "{{ db_name }}"
encoding: utf8
collation: utf8_bin
login_unix_socket: /var/run/mysqld/mysqld.sock

- name: "Create user {{ db_user }} for {{ db_name }}"
mysql_user:
name: "{{ db_user }}"
host: "%"
password: "{{ db_pass }}"
priv: "{{ db_name }}.*:ALL"
login_unix_socket: /var/run/mysqld/mysqld.sock
no_log: yes
82 changes: 82 additions & 0 deletions Sholomitski_Dmitry/06.Ansible.WorkShop/nginx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
- name: Install and configure Nginx with two virtual hosts
hosts: web

vars:
# List of virtual hosts to create
virtualhosts:
- { server_name: 'vhost1.local', document_root: '/var/www/vhost1'}
- { server_name: 'vhost2.local', document_root: '/var/www/vhost2'}


pre_tasks:
- name: Validate
debug:
msg:
- "host: {{ ansible_host }}"
- "http://www.{{ item.server_name }}"
loop: "{{ virtualhosts }}"



tasks:
- name: Install Nginx
ansible.builtin.apt:
name: nginx
state: present

- name: Start and enable Nginx service
service:
name: nginx
state: started
enabled: yes


- name: Create directories for virtual hosts
file:
path: "{{ item.document_root }}"
state: directory
mode: '0755'
loop: "{{ virtualhosts }}"



- name: Generate HTML files for each virtual host
template:
src: index.j2
dest: "{{ item.document_root }}/index.html"
mode: '0644'
loop: "{{ virtualhosts }}"

- name: Create Nginx virtual host configuration files
template:
src: vhost.conf.j2
dest: "/etc/nginx/conf.d/{{ item.server_name }}.conf"
mode: '0644'
loop: "{{ virtualhosts }}"

- name: Restart nginx service
service:
name: nginx
state: restarted

- name: Add host name to hosts file
lineinfile:
dest: /etc/hosts
line: "{{ ansible_host }} {{item.server_name}}"
loop: "{{ virtualhosts }}"

- name: Check that you can connect (GET) to a page and it returns a status 200
ansible.builtin.uri:
url: "http://{{ item.server_name }}"
loop: "{{ virtualhosts }}"




- name: Check that a page returns successfully but fail if websites names is not in the page contents
ansible.builtin.uri:
url: "http://{{ item.server_name }}"
return_content: true
register: this
failed_when: "this is failed or \"{{ item.server_name }}\" not in this.content"
loop: "{{ virtualhosts }}"
13 changes: 13 additions & 0 deletions Sholomitski_Dmitry/06.Ansible.WorkShop/templates/index.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ ansible_fqdn }} - VirtualHost </title>
</head>
<body>
<h1>Welcome to {{ item.server_name }} on ({{ ansible_fqdn }}) - VirtualHost</h1>
<p>This page is served from {{ ansible_hostname }} with FQDN {{ ansible_fqdn }}.</p>

</body>
</html>
14 changes: 14 additions & 0 deletions Sholomitski_Dmitry/06.Ansible.WorkShop/templates/vhost.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server {
listen 80;
server_name {{ item.server_name }};
root {{ item.document_root }};

index index.html;

location / {
try_files $uri $uri/ =404;
}

access_log /var/log/nginx/{{ item.server_name }}-access.log;
error_log /var/log/nginx/{{ item.server_name }}-error.log;
}
Empty file added Sholomitski_Dmitry/READMI.md
Empty file.

0 comments on commit 3d75d96

Please sign in to comment.