trabajo

mail@pastecode.io avatar
unknown
plain_text
7 months ago
5.2 kB
4
Indexable
Never
/home/control/
|_main_call.yml
|_hosts.txt
|_roles/  HAY QUE CAMBIAR LOS NOMBRES, HTTPD.CONF SI POSIBLE Y ESTILO CSS 
    |_ansible-serv/
        |_handlers/
        |    |_main.yml
        |_templates/ 
        |    |_index.php
        |    |_mostrar.php
        |    |_con_dn.php
        |    |_estilo.css
        |    |_httpd.conf.j2
        |_tasks/
        |    |_main.yml
        |_vars/
             |_main.yml
    |_mysql-serv/
        |_handlers/
        |    |_main.yml
        |_files/
        |    |_test_db.sql HAY QUE AÑADIR UNA BASE DE DATOS
        |_tasks/
        |    |_main.yml
        |_vars/
             |_main.yml
----------------------------------------------------
### hosts.txt

[mysql]

server-1 ansible_host=192.168.56.21
server-2 ansible_host=192.168.56.22

---------------------------------------------------------
### main_call.yml
---
- hosts: mysql
  name: Installation of mysql servers
  become: yes

  roles:
    - mysql-serv
    - ansible-serv
----------------------------------------------------------
### roles/ansible-serv/vars/main.yml
---
# vars file for apache-serv
http_port: 8080
admin: ansible-devops
content_dir: /webcontent
httpd_packages:
  - apache2
  - libapache2-mod-wsgi
  - php
  - php-mysql

------------------------------------------------------------
### roles/mysql-serv/tasks/main.yml
---
---
# tasks file for apache-serv
- name: install apache2
  apt:
    name: "{{ item }}"
    state: present
  with_items: "{{ httpd_packages }}"
  notify: restart apache service
- name: create site-enabled directory
  file:
    name: /etc/httpd/conf/sites-enabled
    state: directory
- name: copy httpd.conf
  template:
    src: ../templates/httpd.conf.j2
    dest: /etc/httpd/conf/httpd.conf
  notify: restart apache service
- name: copy index.html
  template:
    src: ../templates/index.php
    dest: /var/www/html/index.php
- name: copy estilo.css
  template:
    src: ../templates/estilo.css
    dest: /var/www/html/estilo.css
- name: copy mostrar.php
  template:
    src: ../templates/mostrar.php
    dest: /var/www/html/mostrar.php
- name: copy con_db.php
  template:
    src: ../templates/con_db.php
    dest: /var/www/html/con_db.php


- name: start apache
  service:
    name: apache2
    state: started
    enabled: yes

-----------------------------------------------------
### roles/mysql-serv/handlers/main.yml
---
# handlers file for apache-serv
- name: restart apache service
  service:
    name: apache2
    state: restarted
    enabled: yes    
---------------------------------------------------------
### roles/mysql-serv/templates/index.php
<!DOCTYPE html>
<html>
<head>
	<title> Mostrar base de datos</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="estilo.css">
</head>
<body>
    <?php 
    include("mostrar.php");
    ?>
</body>
</html>
-----------------------------------------------------------
### roles/mysql-serv/templates/mostrar.php
<?php 
$inc = include("con_db.php");
if ($inc) {
	$consulta = "SELECT * FROM test";
	$resultado = mysqli_query($conex,$consulta);
	if ($resultado) {
		while ($row = $resultado->fetch_array()) {
	    $message = $row['message'];
	    ?>
        <div>
        	<h2><?php echo $message; ?></h2>
        </div> 
	    <?php
	    }
	}
}
?>
-----------------------------------------------------------
###roles/mysql-serv/templates/con_db.php
<?php

$conex = mysqli_connect("localhost","cimsi_user","sudo","mysqlDB"); 

?>
-----------------------------------------------------------
###roles/mysql-serv/templates/style.css
* {
	padding: 0;
	margin: 0;
	font-family: century gothic;
	color: #444
}

h1 {
	padding: 12px;
}

div {
    padding: 10px 20px;
}
----------------------------------------------------------
### roles/mysql-serv/vars/main.yml
---
# vars file for mysql-serv
mysql_apt:
  - mysql-server
  - python3-mysqldb
  - libmysqlclient-dev
  - python-dev
  - python3-dev
  - python-pip
mysql_pip:
  - MySQL-python
mysql_user: cimsi_user
mysql_passwd: sudo
db_name: mysqlDB
------------------------------------------------------------
### roles/mysql-serv/tasks/main.yml
---
---
# tasks file for mysql-serv
- name: Installing mySQL packages with apt
  apt:
    name: "{{ mysql_apt }}"
    state: present
    update_cache: yes
- name: Installing mySQL packages with pip
  pip:
    name: "{{ mysql_pip }}"
    state: present

- name: Start mySQL service
  service:
    name: mysql
    state: started
    enabled: yes

- name: Create mySQL user
  mysql_user:
    name: "{{ mysql_user }}"
    password: "{{ mysql_passwd }}"
    priv: '*.*:ALL'
    host: '%'
    state: present

- name: Create test database
  mysql_db:
    name: "{{ db_name }}"
    state: present

- name: Add data to test database
  copy: src=../files/test_db.sql dest=/tmp/test_db.sql

- name: Insert data into test database
  mysql_db: name="{{ db_name }}" state=import target=/tmp/test_db.sql login_user="{{ mysql_user }}" login_password="{{ mysql_passwd }}"
  notify: restart mySQL
-----------------------------------------------------
### roles/mysql-serv/handlers/main.yml
---
# handlers file for mysql-serv
- name: restart mySQL 
  service:
    name: mysql
    state: restarted
    enabled: yes

Leave a Comment