chrisluiz16

LAMP Role Day 8

Jun 28th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 10.82 KB | None | 0 0
  1. ---------------------------------- Task ------------------------------------------------------
  2.  
  3.  
  4. #### ansible.cfg file ####
  5. [ec2-user@ip-172-31-40-167 ~]$ cat /etc/ansible/ansible.cfg
  6. [defaults]
  7. host_key_checking = False
  8. roles_path= $HOME/ansible/roles
  9.  
  10.  
  11. #### 3 roles created ####
  12.  
  13. [ec2-user@ip-172-31-40-167 roles]$ pwd
  14. /home/ec2-user/ansible/roles
  15. [ec2-user@ip-172-31-40-167 roles]$ ls
  16. lamp  patching  reboot
  17.  
  18.  
  19. #### LAMP module ####
  20.  
  21. [ec2-user@ip-172-31-40-167 roles]$ cd lamp/
  22. [ec2-user@ip-172-31-40-167 lamp]$ tree
  23. .
  24. ├── defaults
  25. │   └── main.yml
  26. ├── files
  27. │   └── www
  28. │       ├── info.php
  29. │       └── test.php
  30. ├── handlers
  31. │   └── main.yml
  32. ├── meta
  33. │   └── main.yml
  34. ├── README.md
  35. ├── tasks
  36. │   └── main.yml
  37. ├── templates
  38. │   ├── httpd.conf.tmpl
  39. │   └── virtualhost.conf.tmpl
  40. ├── tests
  41. │   ├── inventory
  42. │   └── test.yml
  43. └── vars
  44.     └── main.yml
  45.    
  46.  
  47.  
  48. ### vars ####
  49.  
  50. [ec2-user@ip-172-31-40-167 lamp]$ cat vars/main.yml
  51. ---
  52.  
  53. httpd_port: 80
  54. httpd_site: blog.chrisich.fun
  55. httpd_owner: apache
  56. httpd_group: apache
  57. packages:
  58.  - httpd
  59.   - php-fpm
  60. mysql_packages:
  61.  - mariadb105-server
  62.   - php8.2-mysqlnd
  63.   - python3-pip
  64. mysqlrootpasswd: mysqlroot123
  65. mysql_docroot: /var/lib/mysql
  66. blog_user: blog_user
  67. blog_passwd: blog123
  68. blog_db: blog_db
  69.  
  70.  
  71.  
  72. #### Tasks ####
  73.  
  74. [ec2-user@ip-172-31-40-167 lamp]$ cat tasks/main.yml
  75. ---
  76. - name: "Installing Apache and php-fpm"
  77.   yum:
  78.     name: "{{ packages }}"
  79.     state: present
  80.   notify:
  81.    - restart-httpd
  82.     - restart-php
  83. - name: "Copying the httpd conf template to the remote machine"
  84.   template:
  85.     src: httpd.conf.tmpl
  86.     dest: /etc/httpd/conf/httpd.conf
  87.   notify:
  88.    - restart-httpd
  89. - name: "Creating the document root /var/www/html/{{ httpd_site }}"
  90.   file:
  91.     path: "/var/www/html/{{ httpd_site }}"
  92.     state: directory
  93.     owner: "{{ httpd_owner }}"
  94.     group: "{{ httpd_group }}"
  95. - name: "Copying the virtual host template to /etc/httpd/conf.d/{{ httpd_site }}"
  96.   template:
  97.     src: virtualhost.conf.tmpl
  98.     dest: "/etc/httpd/conf.d/default.conf"
  99.   notify:
  100.    - restart-httpd
  101. - name: "Copying the contents to document root"
  102.   copy:
  103.     src: www/
  104.     dest: "/var/www/html/{{ httpd_site }}"
  105.     owner: "{{ httpd_owner }}"
  106.     group: "{{ httpd_group }}"
  107.   notify:
  108.    - restart-php
  109. - name: "Installing mariadb required packages"
  110.   yum:
  111.     name: "{{ mysql_packages }}"
  112.     state: present
  113. - name: "Restarting mariadb server"
  114.   service:
  115.     name: mariadb
  116.     state: restarted
  117.     enabled: true
  118. - name: "Creating a new directory to copy the {{ mysql_docroot }}"
  119.   when: mysql_disk is defined
  120.   file:
  121.     path: /var/lib/new
  122.     state: directory
  123.     owner: mysql
  124.     group: mysql
  125. - name: "Copy the {{ mysql_docroot }} to /var/lib/new"
  126.   when: mysql_disk is defined
  127.   copy:
  128.     src: "{{ mysql_docroot }}/"
  129.     dest: /var/lib/new
  130.     remote_src: true
  131. - name: "Adding a filesystem ext4 to {{ mysql_docroot }}"
  132.   when: mysql_disk is defined
  133.   filesystem:
  134.     fstype: ext4
  135.     dev: "{{ mysql_disk }}"
  136.     state: present
  137. - name: "Adding {{ mysql_docroot }} it to fstab"
  138.   when: mysql_disk is defined
  139.   mount:
  140.     path: "{{ mysql_docroot }}/"
  141.     src: "{{ mysql_disk }}"
  142.     fstype: ext4
  143.     state: mounted
  144. - name: "Copy the {{ mysql_docroot }} to /var/lib/new"
  145.   when: mysql_disk is defined
  146.   copy:
  147.     src: /var/lib/new/
  148.     dest: "{{ mysql_docroot }}"
  149.     owner: mysql
  150.     group: mysql
  151.     remote_src: true
  152. - name: "Installing the pip3 module for mysql"
  153.   pip:
  154.     name: PyMySQL
  155.     state: present
  156. - name: "Restarting mariadb server"
  157.   when: mysql_disk is defined
  158.   service:
  159.     name: mariadb
  160.     state: restarted
  161.     enabled: true
  162. - name: "Setting root password for mysql"
  163.   mysql_user:
  164.     login_user: root
  165.     login_password: ""
  166.     login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  167.     name: root
  168.     password: "{{ mysqlrootpasswd }}"
  169.   ignore_errors: true
  170. - name: "Creating a datababe {{ blog_db }}"
  171.   mysql_db:
  172.     login_user: root
  173.     login_password: "{{ mysqlrootpasswd }}"
  174.     login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  175.     name: "{{ blog_db }}"
  176. - name: "Creating an extra user {{ blog_user }}"
  177.   mysql_user:
  178.     login_user: root
  179.     login_password: "{{ mysqlrootpasswd }}"
  180.     login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  181.     name: "{{ blog_user}}"
  182.     password: "{{ blog_passwd }}"
  183.     host: "%"
  184.     priv: "{{ blog_db }}.*:ALL"
  185.  
  186.  
  187.  
  188. #### files ####
  189.  
  190. [ec2-user@ip-172-31-40-167 lamp]$ cat files/www/test.php
  191. <?php
  192. echo "<h1><center> Hello World this a test php page </h1></center>";
  193. echo "<h1><center>" .gethostname(). "</center></h1>";
  194. ?>
  195. [ec2-user@ip-172-31-40-167 lamp]$
  196. [ec2-user@ip-172-31-40-167 lamp]$ cat files/www/info.php
  197. <?php
  198. phpinfo();
  199. ?>
  200.  
  201. #### template ###
  202.  
  203. [ec2-user@ip-172-31-40-167 lamp]$ cat templates/virtualhost.conf.tmpl
  204. <VirtualHost *:"{{ httpd_port }}">
  205. ServerName "{{ httpd_site }}"
  206. DocumentRoot /var/www/html/"{{ httpd_site }}"
  207. DirectoryIndex index.php index.html
  208. <Directory "/var/www/html/{{ httpd_site }}">
  209.    AllowOverride all
  210. </Directory>
  211. </VirtualHost>
  212.  
  213.  
  214.  
  215. #### Main Playbook ####
  216.  
  217.  
  218. ---
  219.  
  220. - name: "Wordpress Site Hosting"
  221.   hosts: staging
  222.   become: true
  223.   vars:
  224.     mysql_disk: /dev/xvdb
  225.     wordpress_url: https://wordpress.org/wordpress-6.2.tar.gz
  226.   roles:
  227.    - patching
  228.     - lamp
  229.     - reboot
  230.   tasks:
  231.     - name: "Wget the wordpress"
  232.       get_url:
  233.         url: "{{ wordpress_url }}"
  234.         dest: /tmp/wordpress.tar.gz
  235.     - name: "unarchive the worpress url"
  236.       unarchive:
  237.         src: /tmp/wordpress.tar.gz
  238.         dest: /tmp/
  239.         remote_src: true
  240.     - name: "Copying the wordpress contents to the docroot"
  241.       copy:
  242.         src: /tmp/wordpress/
  243.         dest: "/var/www/html/{{ httpd_site }}"
  244.         owner: "{{ httpd_owner }}"
  245.         group: "{{ httpd_group }}"
  246.         remote_src: true
  247.     - name: "Copying wp-config.php to remote server"
  248.       template:
  249.         src: ./wp-config.php.tmpl
  250.         dest: "/var/www/html/{{ httpd_site }}/wp-config.php"
  251.         owner: "{{ httpd_owner }}"
  252.         group: "{{ httpd_group }}"
  253.     - name: "Cleanup"
  254.       file:
  255.         name: "{{ item }}"
  256.         state: absent
  257.       with_items:
  258.        - /tmp/wordpress.tar.gz
  259.         - /tmp/wordpress
  260.        
  261.        
  262. #### Output ####
  263.  
  264. [ec2-user@ip-172-31-40-167 ~]$ ansible-playbook -i inventory setup.yml
  265.  
  266. PLAY [Wordpress Site Hosting] ******************************************************************************************
  267.  
  268. TASK [Gathering Facts] *************************************************************************************************
  269. ok: [172.31.37.48]
  270.  
  271. TASK [patching : Patch the Redhat Family OS with the latest update] ****************************************************
  272. ok: [172.31.37.48]
  273.  
  274. TASK [lamp : Installing Apache and php-fpm] ****************************************************************************
  275. changed: [172.31.37.48]
  276.  
  277. TASK [lamp : Copying the httpd conf template to the remote machine] ****************************************************
  278. changed: [172.31.37.48]
  279.  
  280. TASK [lamp : Creating the document root /var/www/html/blog.chrisich.fun] ***********************************************
  281. changed: [172.31.37.48]
  282.  
  283. TASK [lamp : Copying the virtual host template to /etc/httpd/conf.d/blog.chrisich.fun] *********************************
  284. changed: [172.31.37.48]
  285.  
  286. TASK [lamp : Copying the contents to document root] ********************************************************************
  287. changed: [172.31.37.48]
  288.  
  289. TASK [lamp : Installing mariadb required packages] *********************************************************************
  290. changed: [172.31.37.48]
  291.  
  292. TASK [lamp : Restarting mariadb server] ********************************************************************************
  293. changed: [172.31.37.48]
  294.  
  295. TASK [lamp : Creating a new directory to copy the /var/lib/mysql] ******************************************************
  296. changed: [172.31.37.48]
  297.  
  298. TASK [lamp : Copy the /var/lib/mysql to /var/lib/new] ******************************************************************
  299. changed: [172.31.37.48]
  300.  
  301. TASK [lamp : Adding a filesystem ext4 to /var/lib/mysql] ***************************************************************
  302. changed: [172.31.37.48]
  303.  
  304. TASK [lamp : Adding /var/lib/mysql it to fstab] ************************************************************************
  305. changed: [172.31.37.48]
  306.  
  307. TASK [lamp : Copy the /var/lib/mysql to /var/lib/new] ******************************************************************
  308. changed: [172.31.37.48]
  309.  
  310. TASK [lamp : Installing the pip3 module for mysql] *********************************************************************
  311. changed: [172.31.37.48]
  312.  
  313. TASK [lamp : Restarting mariadb server] ********************************************************************************
  314. changed: [172.31.37.48]
  315.  
  316. TASK [lamp : Setting root password for mysql] **************************************************************************
  317. changed: [172.31.37.48]
  318.  
  319. TASK [lamp : Creating a datababe blog_db] ******************************************************************************
  320. changed: [172.31.37.48]
  321.  
  322. TASK [lamp : Creating an extra user blog_user] *************************************************************************
  323. changed: [172.31.37.48]
  324.  
  325. TASK [reboot : Reboot the machine after patching or after partiotioning] ***********************************************
  326. changed: [172.31.37.48]
  327.  
  328. TASK [Wget the wordpress] **********************************************************************************************
  329. changed: [172.31.37.48]
  330.  
  331. TASK [unarchive the worpress url] **************************************************************************************
  332. changed: [172.31.37.48]
  333.  
  334. TASK [Copying the wordpress contents to the docroot] *******************************************************************
  335. changed: [172.31.37.48]
  336.  
  337. TASK [Copying wp-config.php to remote server] **************************************************************************
  338. changed: [172.31.37.48]
  339.  
  340. TASK [Cleanup] *********************************************************************************************************
  341. changed: [172.31.37.48] => (item=/tmp/wordpress.tar.gz)
  342. changed: [172.31.37.48] => (item=/tmp/wordpress)
  343.  
  344. RUNNING HANDLER [lamp : restart-httpd] *********************************************************************************
  345. changed: [172.31.37.48]
  346.  
  347. RUNNING HANDLER [lamp : restart-php] ***********************************************************************************
  348. changed: [172.31.37.48]
  349.  
  350. PLAY RECAP *************************************************************************************************************
  351. 172.31.37.48               : ok=27   changed=25   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  352.  
Advertisement
Add Comment
Please, Sign In to add comment