From 8545eb3d66ad78230cd62f5bb3c531dbc62eb156 Mon Sep 17 00:00:00 2001 From: Olivier Barais Date: Thu, 2 Dec 2021 11:43:36 +0100 Subject: [PATCH] inisital commit --- LICENSE.md | 2 + lamp_ubuntu1804_2hosts_withroles/README.md | 27 ++++++ .../group_vars/all | 6 ++ .../group_vars/dbservers | 10 ++ .../group_vars/webservers | 6 ++ lamp_ubuntu1804_2hosts_withroles/hosts | 18 ++++ .../roles/common/handlers/main.yml | 8 ++ .../roles/common/tasks/main.yml | 23 +++++ .../roles/common/templates/ntp.conf.j2 | 12 +++ .../roles/db/handlers/main.yml | 9 ++ .../roles/db/tasks/main.yml | 78 +++++++++++++++ .../roles/db/templates/my.cnf.j2 | 12 +++ .../roles/web/handlers/main.yml | 8 ++ .../roles/web/tasks/copy_code.yml | 14 +++ .../roles/web/tasks/install_httpd.yml | 28 ++++++ .../roles/web/tasks/main.yml | 3 + .../roles/web/templates/index.php.j2 | 24 +++++ lamp_ubuntu1804_2hosts_withroles/site.yml | 26 +++++ lamp_ubuntu1804_onehost/files/apache.conf.j2 | 17 ++++ lamp_ubuntu1804_onehost/files/info.php.j2 | 2 + lamp_ubuntu1804_onehost/hosts | 7 ++ lamp_ubuntu1804_onehost/playbook.yml | 94 +++++++++++++++++++ lamp_ubuntu1804_onehost/readme.md | 47 ++++++++++ lamp_ubuntu1804_onehost/vars/default.yml | 7 ++ vagrant/Vagrantfile | 27 ++++++ 25 files changed, 515 insertions(+) create mode 100644 LICENSE.md create mode 100644 lamp_ubuntu1804_2hosts_withroles/README.md create mode 100644 lamp_ubuntu1804_2hosts_withroles/group_vars/all create mode 100644 lamp_ubuntu1804_2hosts_withroles/group_vars/dbservers create mode 100644 lamp_ubuntu1804_2hosts_withroles/group_vars/webservers create mode 100644 lamp_ubuntu1804_2hosts_withroles/hosts create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/common/handlers/main.yml create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/common/tasks/main.yml create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/common/templates/ntp.conf.j2 create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/db/handlers/main.yml create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/db/tasks/main.yml create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/db/templates/my.cnf.j2 create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/web/handlers/main.yml create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/copy_code.yml create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/install_httpd.yml create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/main.yml create mode 100644 lamp_ubuntu1804_2hosts_withroles/roles/web/templates/index.php.j2 create mode 100644 lamp_ubuntu1804_2hosts_withroles/site.yml create mode 100644 lamp_ubuntu1804_onehost/files/apache.conf.j2 create mode 100644 lamp_ubuntu1804_onehost/files/info.php.j2 create mode 100644 lamp_ubuntu1804_onehost/hosts create mode 100755 lamp_ubuntu1804_onehost/playbook.yml create mode 100644 lamp_ubuntu1804_onehost/readme.md create mode 100644 lamp_ubuntu1804_onehost/vars/default.yml create mode 100644 vagrant/Vagrantfile diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e75711c --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,2 @@ +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/deed.en_US. diff --git a/lamp_ubuntu1804_2hosts_withroles/README.md b/lamp_ubuntu1804_2hosts_withroles/README.md new file mode 100644 index 0000000..901772c --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/README.md @@ -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. diff --git a/lamp_ubuntu1804_2hosts_withroles/group_vars/all b/lamp_ubuntu1804_2hosts_withroles/group_vars/all new file mode 100644 index 0000000..213a383 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/group_vars/all @@ -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 diff --git a/lamp_ubuntu1804_2hosts_withroles/group_vars/dbservers b/lamp_ubuntu1804_2hosts_withroles/group_vars/dbservers new file mode 100644 index 0000000..79a7238 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/group_vars/dbservers @@ -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 \ No newline at end of file diff --git a/lamp_ubuntu1804_2hosts_withroles/group_vars/webservers b/lamp_ubuntu1804_2hosts_withroles/group_vars/webservers new file mode 100644 index 0000000..c86a547 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/group_vars/webservers @@ -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 diff --git a/lamp_ubuntu1804_2hosts_withroles/hosts b/lamp_ubuntu1804_2hosts_withroles/hosts new file mode 100644 index 0000000..215dd04 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/hosts @@ -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 \ No newline at end of file diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/common/handlers/main.yml b/lamp_ubuntu1804_2hosts_withroles/roles/common/handlers/main.yml new file mode 100644 index 0000000..bdbded8 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/common/handlers/main.yml @@ -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 diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/common/tasks/main.yml b/lamp_ubuntu1804_2hosts_withroles/roles/common/tasks/main.yml new file mode 100644 index 0000000..7d932c4 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/common/tasks/main.yml @@ -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 diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/common/templates/ntp.conf.j2 b/lamp_ubuntu1804_2hosts_withroles/roles/common/templates/ntp.conf.j2 new file mode 100644 index 0000000..6336c2e --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/common/templates/ntp.conf.j2 @@ -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 + diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/db/handlers/main.yml b/lamp_ubuntu1804_2hosts_withroles/roles/db/handlers/main.yml new file mode 100644 index 0000000..4516011 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/db/handlers/main.yml @@ -0,0 +1,9 @@ +--- +# Handler to handle DB tier notifications + +- name: restart mysql + service: + name: mysql + state: restarted + become: yes + diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/db/tasks/main.yml b/lamp_ubuntu1804_2hosts_withroles/roles/db/tasks/main.yml new file mode 100644 index 0000000..1ec8603 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/db/tasks/main.yml @@ -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 }}" diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/db/templates/my.cnf.j2 b/lamp_ubuntu1804_2hosts_withroles/roles/db/templates/my.cnf.j2 new file mode 100644 index 0000000..b3f8f5f --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/db/templates/my.cnf.j2 @@ -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 diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/web/handlers/main.yml b/lamp_ubuntu1804_2hosts_withroles/roles/web/handlers/main.yml new file mode 100644 index 0000000..52ef3c9 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/web/handlers/main.yml @@ -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 diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/copy_code.yml b/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/copy_code.yml new file mode 100644 index 0000000..6495843 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/copy_code.yml @@ -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 diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/install_httpd.yml b/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/install_httpd.yml new file mode 100644 index 0000000..d9e400b --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/install_httpd.yml @@ -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 + diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/main.yml b/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/main.yml new file mode 100644 index 0000000..796842e --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/web/tasks/main.yml @@ -0,0 +1,3 @@ +--- +- include: install_httpd.yml +- include: copy_code.yml diff --git a/lamp_ubuntu1804_2hosts_withroles/roles/web/templates/index.php.j2 b/lamp_ubuntu1804_2hosts_withroles/roles/web/templates/index.php.j2 new file mode 100644 index 0000000..2614a3f --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/roles/web/templates/index.php.j2 @@ -0,0 +1,24 @@ + + + Ansible Application + + +
+ Homepage +
+"; +echo "List of Databases:
"; + {% 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"; + } +?> + + + diff --git a/lamp_ubuntu1804_2hosts_withroles/site.yml b/lamp_ubuntu1804_2hosts_withroles/site.yml new file mode 100644 index 0000000..3c579c7 --- /dev/null +++ b/lamp_ubuntu1804_2hosts_withroles/site.yml @@ -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 diff --git a/lamp_ubuntu1804_onehost/files/apache.conf.j2 b/lamp_ubuntu1804_onehost/files/apache.conf.j2 new file mode 100644 index 0000000..0f61217 --- /dev/null +++ b/lamp_ubuntu1804_onehost/files/apache.conf.j2 @@ -0,0 +1,17 @@ + + ServerAdmin webmaster@localhost + ServerName {{ http_host }} + ServerAlias www.{{ http_host }} + DocumentRoot /var/www/{{ http_host }} + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + + + Options -Indexes + + + + DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm + + + \ No newline at end of file diff --git a/lamp_ubuntu1804_onehost/files/info.php.j2 b/lamp_ubuntu1804_onehost/files/info.php.j2 new file mode 100644 index 0000000..61ace19 --- /dev/null +++ b/lamp_ubuntu1804_onehost/files/info.php.j2 @@ -0,0 +1,2 @@ +