update file for adding lab

This commit is contained in:
Olivier Barais
2022-12-12 08:29:26 +01:00
parent 4cf2d220e1
commit 2f100754bb
25 changed files with 90 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
Building a simple LAMP stack and deploying Application using Ansible Playbooks.
-------------------------------------------
These playbooks require Ansible 1.2.
These playbooks are meant to be a reference and starter's guide to building
Ansible Playbooks. These playbooks were tested on CentOS 6.x so we recommend
that you use CentOS or RHEL to test these modules.
This LAMP stack can be on a single node or multiple nodes. The inventory file
'hosts' defines the nodes in which the stacks should be configured.
[webservers]
localhost
[dbservers]
bensible
Here the webserver would be configured on the local host and the dbserver on a
server called `bensible`. The stack can be deployed using the following
command:
ansible-playbook -i hosts site.yml
Once done, you can check the results by browsing to http://localhost/index.php.
You should see a simple test page and a list of databases retrieved from the
database server.

View File

@@ -0,0 +1,6 @@
---
# Variables listed here are applicable to all host groups
httpd_port: 80
ntpserver: 192.168.1.2
repository: https://github.com/barais/mywebapp.git

View File

@@ -0,0 +1,10 @@
---
# The variables file used by the playbooks in the dbservers group.
# These don't have to be explicitly imported by vars_files: they are autopopulated.
mysqlservice: mysqld
mysql_port: 3306
dbuser: foouser
dbname: foodb
upassword: abc
mysql_root_password: root

View File

@@ -0,0 +1,6 @@
---
# The variables file used by the playbooks in the dbservers group.
# These don't have to be explicitly imported by vars_files: they are autopopulated.
databasepublicip: 192.168.56.142
publicip: 192.168.56.141

View File

@@ -0,0 +1,18 @@
[webservers]
192.168.56.141
[dbservers]
192.168.56.142
[webservers:vars]
ansible_connection=ssh
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible_become_pass=vagrant
ansible_python_interpreter=/usr/bin/python3
[dbservers:vars]
ansible_connection=ssh
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible_become_pass=vagrant
ansible_python_interpreter=/usr/bin/python3

View File

@@ -0,0 +1,8 @@
---
# Handler to handle common notifications. Handlers are called by other plays.
# See http://docs.ansible.com/playbooks_intro.html for more information about handlers.
- name: restart ntp
service:
name: ntp
state: restarted

View File

@@ -0,0 +1,23 @@
---
# This playbook contains common plays that will be run on all nodes.
- name: Install ntp
apt:
name: ntp
update_cache: true
state: present
tags: ntp
- name: Configure ntp file
template:
src: ntp.conf.j2
dest: /etc/ntp.conf
tags: ntp
notify: restart ntp
- name: Start the ntp service
service:
name: ntp
state: started
enabled: yes
tags: ntp

View File

@@ -0,0 +1,12 @@
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
server {{ ntpserver }}
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

View File

@@ -0,0 +1,9 @@
---
# Handler to handle DB tier notifications
- name: restart mysql
service:
name: mysql
state: restarted
become: yes

View File

@@ -0,0 +1,78 @@
---
# This playbook will install mysql and create db user and give permissions.
- name: Install Mysql package
apt:
update_cache: true
name: ['mysql-server', 'python3-pip']
state: present
- name: Make sure pymysql is present
become: true # needed if the other tasks are not played as root
pip:
name: pymysql
state: present
- name: Create Mysql configuration file
template:
src: my.cnf.j2
dest: /etc/my.cnf
notify:
- restart mysql
- name: (DATABASE) Allow remote hosts to connect (Debian)
lineinfile:
path: /etc/mysql/mysql.conf.d/mysqld.cnf
backrefs: yes
regexp: '^bind-address'
line: 'bind-address = 0.0.0.0'
state: present
notify: restart mysql
- name: Start Mysql Service
service:
name: mysql
state: started
enabled: yes
- name: Sets the root password
no_log: true
mysql_user:
name: root
password: "{{ mysql_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
ignore_errors: yes
#- name: Removes all anonymous user accounts
# mysql_user:
# name: ''
# host_all: yes
# state: absent
#- name: Removes the MySQL test database
# mysql_db:
# name: test
# state: absent
- name: Create Application Database
mysql_db:
name: "{{ dbname }}"
login_user: root
login_password: "{{ mysql_root_password }}"
state: present
- name: Create Application DB User
no_log: true
mysql_user:
name: "{{ dbuser }}"
password: "{{ upassword }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
priv: "*.*:ALL"
host: '%'
state: present
login_user: root
login_password: "{{ mysql_root_password }}"

View File

@@ -0,0 +1,12 @@
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port={{ mysql_port }}
bind-address = 0.0.0.0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

View File

@@ -0,0 +1,8 @@
---
# Handler for the webtier: handlers are called by other plays.
# See http://docs.ansible.com/playbooks_intro.html for more information about handlers.
- name: restart iptables
service:
name: iptables
state: restarted

View File

@@ -0,0 +1,14 @@
---
# These tasks are responsible for copying the latest dev/production code from
# the version control system.
- name: Copy the code from repository
git:
repo: "{{ repository }}"
dest: /var/www/html/
- name: Creates the index.php file
template:
src: index.php.j2
dest: /var/www/html/index.php
become: yes

View File

@@ -0,0 +1,28 @@
---
# These tasks install http and the php modules.
- name: Install http and php etc
apt:
name: ['apache2', 'php', 'php-mysql', 'git']
state: present
become: yes
- name: Recursively remove directory
ansible.builtin.file:
path: /var/www/html
state: absent
- name: Create a directory if it does not exist
ansible.builtin.file:
path: /var/www/html
state: directory
mode: '0755'
- name: http service state
service:
name: apache2
state: started
enabled: yes
become: yes

View File

@@ -0,0 +1,3 @@
---
- include: install_httpd.yml
- include: copy_code.yml

View File

@@ -0,0 +1,24 @@
<html>
<head>
<title>Ansible Application</title>
</head>
<body>
</br>
<a href=http://{{ publicip }}/index.html>Homepage</a>
</br>
<?php
Print "Hello, World! I am a web server configured using Ansible and I am : ";
echo exec('hostname');
Print "</BR>";
echo "List of Databases: </BR>";
{% for host in groups['dbservers'] %}
$link = mysqli_connect('{{ databasepublicip }}', '{{ hostvars[host].dbuser }}', '{{ hostvars[host].upassword }}') or die(mysqli_connect_error($link));
{% endfor %}
$res = mysqli_query($link, "SHOW DATABASES;");
while ($row = mysqli_fetch_assoc($res)) {
echo $row['Database'] . "\n";
}
?>
</body>
</html>

View File

@@ -0,0 +1,26 @@
---
# This playbook deploys the whole application stack in this site.
- name: apply common configuration to all nodes
hosts: all
remote_user: vagrant
become: yes
roles:
- common
- name: configure and deploy the webservers and application code
hosts: webservers
remote_user: vagrant
become: yes
roles:
- web
- name: deploy MySQL and configure the databases
hosts: dbservers
remote_user: vagrant
become: yes
roles:
- db