Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------- Task ------------------------------------------------------
- #### ansible.cfg file ####
- [ec2-user@ip-172-31-40-167 ~]$ cat /etc/ansible/ansible.cfg
- [defaults]
- host_key_checking = False
- roles_path= $HOME/ansible/roles
- #### 3 roles created ####
- [ec2-user@ip-172-31-40-167 roles]$ pwd
- /home/ec2-user/ansible/roles
- [ec2-user@ip-172-31-40-167 roles]$ ls
- lamp patching reboot
- #### LAMP module ####
- [ec2-user@ip-172-31-40-167 roles]$ cd lamp/
- [ec2-user@ip-172-31-40-167 lamp]$ tree
- .
- ├── defaults
- │ └── main.yml
- ├── files
- │ └── www
- │ ├── info.php
- │ └── test.php
- ├── handlers
- │ └── main.yml
- ├── meta
- │ └── main.yml
- ├── README.md
- ├── tasks
- │ └── main.yml
- ├── templates
- │ ├── httpd.conf.tmpl
- │ └── virtualhost.conf.tmpl
- ├── tests
- │ ├── inventory
- │ └── test.yml
- └── vars
- └── main.yml
- ### vars ####
- [ec2-user@ip-172-31-40-167 lamp]$ cat vars/main.yml
- ---
- httpd_port: 80
- httpd_site: blog.chrisich.fun
- httpd_owner: apache
- httpd_group: apache
- packages:
- - httpd
- - php-fpm
- mysql_packages:
- - mariadb105-server
- - php8.2-mysqlnd
- - python3-pip
- mysqlrootpasswd: mysqlroot123
- mysql_docroot: /var/lib/mysql
- blog_user: blog_user
- blog_passwd: blog123
- blog_db: blog_db
- #### Tasks ####
- [ec2-user@ip-172-31-40-167 lamp]$ cat tasks/main.yml
- ---
- - name: "Installing Apache and php-fpm"
- yum:
- name: "{{ packages }}"
- state: present
- notify:
- - restart-httpd
- - restart-php
- - name: "Copying the httpd conf template to the remote machine"
- template:
- src: httpd.conf.tmpl
- dest: /etc/httpd/conf/httpd.conf
- notify:
- - restart-httpd
- - name: "Creating the document root /var/www/html/{{ httpd_site }}"
- file:
- path: "/var/www/html/{{ httpd_site }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "Copying the virtual host template to /etc/httpd/conf.d/{{ httpd_site }}"
- template:
- src: virtualhost.conf.tmpl
- dest: "/etc/httpd/conf.d/default.conf"
- notify:
- - restart-httpd
- - name: "Copying the contents to document root"
- copy:
- src: www/
- dest: "/var/www/html/{{ httpd_site }}"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- notify:
- - restart-php
- - name: "Installing mariadb required packages"
- yum:
- name: "{{ mysql_packages }}"
- state: present
- - name: "Restarting mariadb server"
- service:
- name: mariadb
- state: restarted
- enabled: true
- - name: "Creating a new directory to copy the {{ mysql_docroot }}"
- when: mysql_disk is defined
- file:
- path: /var/lib/new
- state: directory
- owner: mysql
- group: mysql
- - name: "Copy the {{ mysql_docroot }} to /var/lib/new"
- when: mysql_disk is defined
- copy:
- src: "{{ mysql_docroot }}/"
- dest: /var/lib/new
- remote_src: true
- - name: "Adding a filesystem ext4 to {{ mysql_docroot }}"
- when: mysql_disk is defined
- filesystem:
- fstype: ext4
- dev: "{{ mysql_disk }}"
- state: present
- - name: "Adding {{ mysql_docroot }} it to fstab"
- when: mysql_disk is defined
- mount:
- path: "{{ mysql_docroot }}/"
- src: "{{ mysql_disk }}"
- fstype: ext4
- state: mounted
- - name: "Copy the {{ mysql_docroot }} to /var/lib/new"
- when: mysql_disk is defined
- copy:
- src: /var/lib/new/
- dest: "{{ mysql_docroot }}"
- owner: mysql
- group: mysql
- remote_src: true
- - name: "Installing the pip3 module for mysql"
- pip:
- name: PyMySQL
- state: present
- - name: "Restarting mariadb server"
- when: mysql_disk is defined
- service:
- name: mariadb
- state: restarted
- enabled: true
- - name: "Setting root password for mysql"
- mysql_user:
- login_user: root
- login_password: ""
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: root
- password: "{{ mysqlrootpasswd }}"
- ignore_errors: true
- - name: "Creating a datababe {{ blog_db }}"
- mysql_db:
- login_user: root
- login_password: "{{ mysqlrootpasswd }}"
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: "{{ blog_db }}"
- - name: "Creating an extra user {{ blog_user }}"
- mysql_user:
- login_user: root
- login_password: "{{ mysqlrootpasswd }}"
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: "{{ blog_user}}"
- password: "{{ blog_passwd }}"
- host: "%"
- priv: "{{ blog_db }}.*:ALL"
- #### files ####
- [ec2-user@ip-172-31-40-167 lamp]$ cat files/www/test.php
- <?php
- echo "<h1><center> Hello World this a test php page </h1></center>";
- echo "<h1><center>" .gethostname(). "</center></h1>";
- ?>
- [ec2-user@ip-172-31-40-167 lamp]$
- [ec2-user@ip-172-31-40-167 lamp]$ cat files/www/info.php
- <?php
- phpinfo();
- ?>
- #### template ###
- [ec2-user@ip-172-31-40-167 lamp]$ cat templates/virtualhost.conf.tmpl
- <VirtualHost *:"{{ httpd_port }}">
- ServerName "{{ httpd_site }}"
- DocumentRoot /var/www/html/"{{ httpd_site }}"
- DirectoryIndex index.php index.html
- <Directory "/var/www/html/{{ httpd_site }}">
- AllowOverride all
- </Directory>
- </VirtualHost>
- #### Main Playbook ####
- ---
- - name: "Wordpress Site Hosting"
- hosts: staging
- become: true
- vars:
- mysql_disk: /dev/xvdb
- wordpress_url: https://wordpress.org/wordpress-6.2.tar.gz
- roles:
- - patching
- - lamp
- - reboot
- tasks:
- - name: "Wget the wordpress"
- get_url:
- url: "{{ wordpress_url }}"
- dest: /tmp/wordpress.tar.gz
- - name: "unarchive the worpress url"
- unarchive:
- src: /tmp/wordpress.tar.gz
- dest: /tmp/
- remote_src: true
- - name: "Copying the wordpress contents to the docroot"
- copy:
- src: /tmp/wordpress/
- dest: "/var/www/html/{{ httpd_site }}"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- remote_src: true
- - name: "Copying wp-config.php to remote server"
- template:
- src: ./wp-config.php.tmpl
- dest: "/var/www/html/{{ httpd_site }}/wp-config.php"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "Cleanup"
- file:
- name: "{{ item }}"
- state: absent
- with_items:
- - /tmp/wordpress.tar.gz
- - /tmp/wordpress
- #### Output ####
- [ec2-user@ip-172-31-40-167 ~]$ ansible-playbook -i inventory setup.yml
- PLAY [Wordpress Site Hosting] ******************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.37.48]
- TASK [patching : Patch the Redhat Family OS with the latest update] ****************************************************
- ok: [172.31.37.48]
- TASK [lamp : Installing Apache and php-fpm] ****************************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Copying the httpd conf template to the remote machine] ****************************************************
- changed: [172.31.37.48]
- TASK [lamp : Creating the document root /var/www/html/blog.chrisich.fun] ***********************************************
- changed: [172.31.37.48]
- TASK [lamp : Copying the virtual host template to /etc/httpd/conf.d/blog.chrisich.fun] *********************************
- changed: [172.31.37.48]
- TASK [lamp : Copying the contents to document root] ********************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Installing mariadb required packages] *********************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Restarting mariadb server] ********************************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Creating a new directory to copy the /var/lib/mysql] ******************************************************
- changed: [172.31.37.48]
- TASK [lamp : Copy the /var/lib/mysql to /var/lib/new] ******************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Adding a filesystem ext4 to /var/lib/mysql] ***************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Adding /var/lib/mysql it to fstab] ************************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Copy the /var/lib/mysql to /var/lib/new] ******************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Installing the pip3 module for mysql] *********************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Restarting mariadb server] ********************************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Setting root password for mysql] **************************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Creating a datababe blog_db] ******************************************************************************
- changed: [172.31.37.48]
- TASK [lamp : Creating an extra user blog_user] *************************************************************************
- changed: [172.31.37.48]
- TASK [reboot : Reboot the machine after patching or after partiotioning] ***********************************************
- changed: [172.31.37.48]
- TASK [Wget the wordpress] **********************************************************************************************
- changed: [172.31.37.48]
- TASK [unarchive the worpress url] **************************************************************************************
- changed: [172.31.37.48]
- TASK [Copying the wordpress contents to the docroot] *******************************************************************
- changed: [172.31.37.48]
- TASK [Copying wp-config.php to remote server] **************************************************************************
- changed: [172.31.37.48]
- TASK [Cleanup] *********************************************************************************************************
- changed: [172.31.37.48] => (item=/tmp/wordpress.tar.gz)
- changed: [172.31.37.48] => (item=/tmp/wordpress)
- RUNNING HANDLER [lamp : restart-httpd] *********************************************************************************
- changed: [172.31.37.48]
- RUNNING HANDLER [lamp : restart-php] ***********************************************************************************
- changed: [172.31.37.48]
- PLAY RECAP *************************************************************************************************************
- 172.31.37.48 : ok=27 changed=25 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Advertisement
Add Comment
Please, Sign In to add comment