Skip to content

Instantly share code, notes, and snippets.

@INDIAN2020
Created December 28, 2023 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save INDIAN2020/6ef4ed7457bb19bb9d5baaa18ad8d2f4 to your computer and use it in GitHub Desktop.
Save INDIAN2020/6ef4ed7457bb19bb9d5baaa18ad8d2f4 to your computer and use it in GitHub Desktop.
---
- hosts: all
become: true
vars:
#System Settings
php_modules: [ 'php-curl', 'php-gd', 'php-mbstring', 'php-xml', 'php-xmlrpc', 'php-soap', 'php-intl', 'php-zip' ]
#MySQL Settings
mysql_root_password: "mysql_root_password"
mysql_db: "wordpress"
mysql_user: "sammy"
mysql_password: "password"
#HTTP Settings
http_host: "your_domain"
http_conf: "your_domain.conf"
http_port: "80"
tasks:
- name: Install prerequisites
apt: name=aptitude update_cache=yes state=latest force_apt_get=yes
tags: [ system ]
- name: Install LAMP Packages
apt: name={{ item }} update_cache=yes state=latest
loop: [ 'apache2', 'mysql-server', 'python3-pymysql', 'php', 'php-mysql', 'libapache2-mod-php' ]
tags: [ system ]
- name: Install PHP Extensions
apt: name={{ item }} update_cache=yes state=latest
loop: "{{ php_modules }}"
tags: [ system ]
# Apache Configuration
- name: Create document root
file:
path: "/var/www/{{ http_host }}"
state: directory
owner: "www-data"
group: "www-data"
mode: '0755'
tags: [ apache ]
- name: Set up Apache VirtualHost
template:
src: "files/apache.conf.j2"
dest: "/etc/apache2/sites-available/{{ http_conf }}"
notify: Reload Apache
tags: [ apache ]
- name: Enable rewrite module
shell: /usr/sbin/a2enmod rewrite
notify: Reload Apache
tags: [ apache ]
- name: Enable new site
shell: /usr/sbin/a2ensite {{ http_conf }}
notify: Reload Apache
tags: [ apache ]
- name: Disable default Apache site
shell: /usr/sbin/a2dissite 000-default.conf
notify: Restart Apache
tags: [ apache ]
# MySQL Configuration
- name: Set the root password
mysql_user:
name: root
password: "{{ mysql_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
tags: [ mysql, mysql-root ]
- name: Remove all anonymous user accounts
mysql_user:
name: ''
host_all: yes
state: absent
login_user: root
login_password: "{{ mysql_root_password }}"
tags: [ mysql ]
- name: Remove the MySQL test database
mysql_db:
name: test
state: absent
login_user: root
login_password: "{{ mysql_root_password }}"
tags: [ mysql ]
- name: Creates database for WordPress
mysql_db:
name: "{{ mysql_db }}"
state: present
login_user: root
login_password: "{{ mysql_root_password }}"
tags: [ mysql ]
- name: Create MySQL user for WordPress
mysql_user:
name: "{{ mysql_user }}"
password: "{{ mysql_password }}"
priv: "{{ mysql_db }}.*:ALL"
state: present
login_user: root
login_password: "{{ mysql_root_password }}"
tags: [ mysql ]
# UFW Configuration
- name: "UFW - Allow HTTP on port {{ http_port }}"
ufw:
rule: allow
port: "{{ http_port }}"
proto: tcp
tags: [ system ]
# WordPress Configuration
- name: Download and unpack latest WordPress
unarchive:
src: https://wordpress.org/latest.tar.gz
dest: "/var/www/{{ http_host }}"
remote_src: yes
creates: "/var/www/{{ http_host }}/wordpress"
tags: [ wordpress ]
- name: Set ownership
file:
path: "/var/www/{{ http_host }}"
state: directory
recurse: yes
owner: www-data
group: www-data
tags: [ wordpress ]
- name: Set permissions for directories
shell: "/usr/bin/find /var/www/{{ http_host }}/wordpress/ -type d -exec chmod 750 {} \\;"
tags: [ wordpress ]
- name: Set permissions for files
shell: "/usr/bin/find /var/www/{{ http_host }}/wordpress/ -type f -exec chmod 640 {} \\;"
tags: [ wordpress ]
# - name: Set up wp-config
# template:
# src: "files/wp-config.php.j2"
# dest: "/var/www/{{ http_host }}/wordpress/wp-config.php"
# tags: [ wordpress ]
- name: Fetch WP salts metadata
uri:
url: https://api.wordpress.org/secret-key/1.1/salt/
return_content: yes
register: wpsalts
- name: Sets Nginx conf file
copy:
content: |
<?php
define( 'DB_NAME', '{{ mysql_db }}' );
define( 'DB_USER', '{{ mysql_user }}' );
define( 'DB_PASSWORD', '{{ mysql_password }}' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );
define('FS_METHOD', 'direct');
{{ wpsalts }}
$table_prefix = 'wp_';
define( 'WP_DEBUG', false );
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
require_once( ABSPATH . 'wp-settings.php' );
dest: "/var/www/{{ http_host }}/wordpress/wp-config.php"
tags: [ wordpress ]
handlers:
- name: Reload Apache
service:
name: apache2
state: reloaded
- name: Restart Apache
service:
name: apache2
state: restarted
# - name: Task sends the request to index.html page on nginx installed locally to retrive the data
# ansible.builtin.debug:
# msg: "{{ lookup('url', '<http://localhost:80/index.html>')}}"
# - name: Task sends the request to adamtheautomator website and retrive the data
# ansible.builtin.debug:
# msg: "{{ lookup('url', '<https://adamtheautomator.com/resources/>', username='U', password='p') }}"
# - name: Retriving DNS records for google.com
# debug:
# msg: "{{ lookup ('dig', 'google.com')}}"
# - name: Fetch instance metadata
# uri:
# url: http://169.254.169.254/path/to/ip_address
# return_content: yes
# register: jsondata
# - debug: msg="Operating on instance {{ jsondata['content'] }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment