Tasks
- Create a directory named “quiz23” in your student number directory in Quiz 1.3
- Create a markdown file named “README.md” in the newly created directory with the directory summary.
- Transform this procedure (Links to an external site.) as a playbook
- Then create a Pull request and put your forked repo in the only question of this quiz (Note answer this quiz as well as create a pull request).
Output
1811023/quiz23/ansible.cfg
[defaults]
# Basic Configuration
inventory = ./inventory
remote_user = jpcabral-tip
private_key_file = ./private.key
# Priviege Escalation
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
# NOTE: Configure remote host/s to allow remote_user to execute sudo withoout password using sudo visudo. Append `<remote_user> ALL=(ALL) NOPASSWD:ALL`
1811023/quiz23/inventory
[ubuntu]
192.168.254.135
1811023/quiz23/roles/copyphp/tasks/main.yml
---
# tasks file for roles/copyphp
- name: Copy PHP file
copy:
src: ""
dest: "/var/www//"
owner: ""
group: ""
mode: '0755'
register: phpfile
- name: Restart Apache
service:
name: apache2
state: restarted
when: phpfile.changed
1811023/quiz23/roles/createdbmysql/tasks/main.yml
---
# tasks file for roles/createdbmysql
- name: Create database
mysql_db:
login_host: ""
login_user: ""
login_password: ""
name: "{( db_name }}"
state: present
- name: Copy the SQL script on remote host
copy:
src: ""
dest: "~"
owner: ""
group: ""
mode: '0755'
register: sqlFile
- name: Import SQL scipt
mysql_db:
login_host: ""
login_user: ""
login_password: ""
state: import
name: all
target: "~/"
when: sqlFile.changed
1811023/quiz23/roles/createdbusermysql/tasks/main.yml
---
# tasks file for roles/createdbusermysql
- name: Create MySQL user
shell: mysql -e "CREATE USER IF NOT EXISTS ''@'localhost' IDENTIFIED WITH mysql_native_password BY ''"; mysql -e "CREATE USER IF NOT EXISTS ''@'%' IDENTIFIED WITH mysql_native_password BY ''";
- name: Escalate created MySQL user
shell: set -f; mysql -e "GRANT ALL PRIVILEGES ON *.* TO ''@'localhost'"; mysql -e "GRANT ALL PRIVILEGES ON *.* TO ''@'%'"
1811023/quiz23/roles/createvirtualhost/tasks/main.yml
---
# tasks file for roles/createvirtualhost
- name: Create directory for domain in /var/www/
file:
path: "/var/www/"
state: directory
owner: ""
group: ""
mode: '0755'
- name: Copy domain configuration to /etc/apache2/sites-available/
copy:
src: ""
dest: "/etc/apache2/sites-available/.conf"
owner: ""
group: ""
mode: '0755'
register: fileregister
- name: Set domain in front
shell: "a2ensite ; a2dissite 000-default"
register: fileregister
- name: New index.html
copy:
src: ""
dest: "/var/www//index.html"
owner: ""
group: ""
mode: '0755'
register: fileregister
- name: Create info.php on the domain directory inside /var/www/
copy:
content: "<?php phpinfo();"
dest: "/var/www//info.php"
owner: ""
group: ""
mode: '0755'
register: fileregister
- name: Restart Apache
service:
name: apache2
state: restarted
when: fileregister.changed
1811023/quiz23/roles/installpackages/tasks/main.yml
---
# tasks file for roles/installpackages
- name: Install Packages
apt:
name: ""
state: latest
update_cache: yes
1811023/quiz23/roles/pipinstall/tasks/main.yml
---
# tasks file for roles/pipinstall
- name: Install python packages using pip
pip:
name: ""
© 2021 GitHub, Inc.
1811023/quiz23/roles/startservice/tasks/main.yml
---
# tasks file for roles/startservice
- name: Start and Enable Service/s
service:
name: ""
state: started
enabled: yes
1811023/quiz23/playbook.yaml
---
- name: Quiz 2.3 Ansible Roles
hosts: ubuntu
tasks:
- name: Include db authentication
include_vars:
file: vars/db_config_vars.yaml
- name: Install LAMP (Linux, Apache, MySQL PHP Stack)
include_role:
name: installpackages
vars:
package:
- apache2
- mysql-server
- php
- libapache2-mod-php
- php-mysql
- name: Allow HTTP on Port 80
ufw:
rule: allow
port: "80"
proto: tcp
- name: Start and enable Apache service
include_role:
name: startservice
vars:
service: apache2
- name: Start and enable MySQL service
include_role:
name: startservice
vars:
service: mysql
- name: Configure Apache with virtual host using createvirtualhost role
include_role:
name: createvirtualhost
vars:
domain_name: jpcabral-tip
owner: jpcabral-tip
group: jpcabral-tip
src: jpcabral-tip.conf
indexhtml: index.html
# OPTIONAL PHP MYSQL TASKS??
- name: Install MySQL module using installpackages role
include_role:
name: installpackages
vars:
package:
- python3
- python3-pymysql
- name: Install pip requirements for MySQL module using pipinstall role
include_role:
name: pipinstall
vars:
pippackage:
- pymysql
- name: Create database user using createdbusermysql role
include_role:
name: createdbusermysql
- name: Create database using createdbmysql role
include_role:
name: createdbmysql
vars:
db_name: example_database
db_sql: db.sql
owner: jpcabral-tip
group: jpcabral-tip
- name: Copy php code using copyphp role
include_role:
name: copyphp
vars:
src: todo_list.php
domain_name: jpcabral-tip
owner: jpcabral-tip
group: jpcabral-tip
1811023/quiz23/files/db.sql
CREATE SCHEMA IF NOT EXISTS example_database;
USE example_database;
CREATE TABLE IF NOT EXISTS example_database.todo_list(
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
-- SAMPLE ENTRIES
INSERT INTO example_database.todo_list(content) VALUES("My first important item");
INSERT INTO example_database.todo_list(content) VALUES("My second important item");
INSERT INTO example_database.todo_list(content) VALUES("My third important item");
INSERT INTO example_database.todo_list(content) VALUES("and this one more thing");
1811023/quiz23/files/index.html
<html>
<head>
<title>jpcabral-tip website</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is the landing page of <strong>jpcabral-tip</strong>.</p>
</body>
</html>
1811023/quiz23/files/jpcabral-tip.conf
<VirtualHost *:80>
ServerName jpcabral-tip
ServerAlias www.jpcabral-tip
DocumentRoot /var/www/jpcabral-tip
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
1811023/quiz23/files/todo_list.php
<?php
$user = "jpcabral-tip";
$password = "password";
$database = "example_database";
$table = "todo_list";
try {
$db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
echo "<h2>TODO</h2><ol>";
foreach($db->query("SELECT content FROM $table") as $row){
echo "<li>" . $row['content'] . "</li>";
}
echo "</ol>";
} catch (PDOExecption $e){
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
1811023/quiz23/README.md
# Directory Summary
**Author:** Jose Paulo Cabral
## Prequisites
* Ansible (installed on local machine)
* SSH (installed on both local and remote machine/s)
## Requirements
* SSH private key file for authentication placed on working directory.
* Declared MySQL database user for local and remote hosts. (to be supplied in ``vars/db_config_vars.yaml``
Note: ``private.key`` and ``vars/db_config_vars.yaml`` are placed on ``.gitignore`` and must be supplied before executing the playbook.
## Directory Structure
```
quiz23
files/
db.sql
index.html
jpcabral-tip.conf
todo_list.php
roles/
copyphp/
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml
README.md
createdbusermysql/
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml
README.md
createdbmysql/
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml
README.md
createvirtualhost/
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml
README.md
installpackages/
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml
README.md
pipinstall/
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml
README.md
startservice/
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml
README.md
vars/
db_config_vars.yaml*
ansible.cfg
inventory
playbook.yaml
private.key*
.gitignore
README.md
```
Note: Files marked with asterisk (*) at the end are declared inside ``.gitignore``.
## Content Structure for Files Declared in .gitgnore
* ``private.key``
The localmachine generated SSH private key (named ``id_rsa`` by default inside ``~/.ssh/``
* ``vars/db_config_vars.yaml``
```
db_host: <database IP>
db_user: <database user>
db_pass: <database user password>
```
Execute using the following command to run the playbook:
localhost:~/1811023/quiz23# ansible-playbook playbook.yaml
As seen on Github.