chrisluiz16

LAMP with partition

Jun 27th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 13.43 KB | None | 0 0
  1.  
  2.  
  3. #### Playbook ####
  4.  
  5. ---
  6.  
  7. - name : "Wordpress LAMP installation"
  8.   hosts: staging
  9.   become: true
  10.   vars_files:
  11.    - apache.vars
  12.     - mariadb.vars
  13.     - wordpress.vars
  14.   tasks:
  15.     - name: "Installing Apache and php-fpm"
  16.       yum:
  17.         name: "{{ packages }}"
  18.         state: present
  19.       register: package_status
  20.       tags:
  21.        - never
  22.         - lamp
  23.     - name: "Copying the httpd conf template to the remote machine"
  24.       template:
  25.         src: ./httpd.conf.tmpl
  26.         dest: /etc/httpd/conf/httpd.conf
  27.       register: conf_status
  28.       tags:
  29.        - never
  30.         - lamp
  31.     - name: "Creating the document root /var/www/html/{{ httpd_site }}"
  32.       file:
  33.         path: "/var/www/html/{{ httpd_site }}"
  34.         state: directory
  35.         owner: "{{ httpd_owner }}"
  36.         group: "{{ httpd_group }}"
  37.       tags:
  38.        - never
  39.         - lamp
  40.     - name: "Copying the virtual host template to /etc/httpd/conf.d/{{ httpd_site }}"
  41.       template:
  42.         src: ./virtualhost.conf.tmpl
  43.         dest: "/etc/httpd/conf.d/default.conf"
  44.       register: virtualhost_status
  45.       tags:
  46.        - never
  47.         - lamp
  48.     - name: "Copying the contents to document root"
  49.       when: package_status.changed == true
  50.       copy:
  51.         src: ./www/
  52.         dest: "/var/www/html/{{ httpd_site }}"
  53.         owner: "{{ httpd_owner }}"
  54.         group: "{{ httpd_group }}"
  55.       register: index_status
  56.       tags:
  57.        - never
  58.         - lamp
  59.     - name: "Service php-fpm restart/enable when condition meets"
  60.       when: package_status.changed == true or index_status.changed == true
  61.       service:
  62.         name: php-fpm
  63.         state: restarted
  64.         enabled: true
  65.       tags:
  66.        - never
  67.         - lamp
  68.     - name: "Service httpd restart/enable when condition meets"
  69.       when: package_status.changed == true or conf_status.changed == true or virtualhost_status.changed == true
  70.       service:
  71.         name: httpd
  72.         state: restarted
  73.         enabled: true
  74.       tags:
  75.        - never
  76.         - lamp
  77.     - name: "Installing mariadb required packages"
  78.       yum:
  79.         name: "{{ mysql_packages }}"
  80.         state: present
  81.       tags:
  82.        - never
  83.         - lamp
  84.         - mysql
  85.       register: mysql_packages
  86.     - name: "Restarting mariadb server"
  87.       when: mysql_packages.changed == true
  88.       service:
  89.         name: mariadb
  90.         state: restarted
  91.         enabled: true
  92.       tags:
  93.        - never
  94.         - lamp
  95.         - mysql
  96.     - name: "Creating a new directory to copy the {{ mysql_docroot }}"
  97.       file:
  98.         path: /var/lib/new
  99.         state: directory
  100.         owner: mysql
  101.         group: mysql
  102.       tags:
  103.        - never
  104.         - lamp
  105.         - mysql
  106.     - name: "Copy the {{ mysql_docroot }} to /var/lib/new"
  107.       when: mysql_packages.changed == true
  108.       copy:
  109.         src: "{{ mysql_docroot }}/"
  110.         dest: /var/lib/new
  111.         remote_src: true
  112.       tags:
  113.        - never
  114.         - lamp
  115.         - mysql
  116.     - name: "Adding a filesystem ext4 to {{ mysql_docroot }}"
  117.       filesystem:
  118.         fstype: ext4
  119.         dev: /dev/xvdb
  120.         state: present
  121.       tags:
  122.        - never
  123.         - lamp
  124.         - mysql
  125.     - name: "Adding {{ mysql_docroot }} it to fstab"
  126.       mount:
  127.         path: "{{ mysql_docroot }}/"
  128.         src: /dev/xvdb
  129.         fstype: ext4
  130.         state: mounted
  131.       tags:
  132.        - never
  133.         - lamp
  134.         - mysql
  135.     - name: "Copy the {{ mysql_docroot }} to /var/lib/new"
  136.       when: mysql_packages.changed == true
  137.       copy:
  138.         src: /var/lib/new/
  139.         dest: "{{ mysql_docroot }}"
  140.         owner: mysql
  141.         group: mysql
  142.         remote_src: true
  143.       tags:
  144.        - never
  145.         - lamp
  146.         - mysql
  147.  
  148.     - name: "Installing the pip3 module for mysql"
  149.       pip:
  150.         name: PyMySQL
  151.         state: present
  152.       tags:
  153.        - never
  154.         - lamp
  155.         - mysql
  156.     - name: "Restarting mariadb server"
  157.       when: mysql_packages.changed == true
  158.       service:
  159.         name: mariadb
  160.         state: restarted
  161.         enabled: true
  162.       tags:
  163.        - never
  164.         - lamp
  165.         - mysql
  166.     - name: "Setting root password for mysql"
  167.       mysql_user:
  168.         login_user: root
  169.         login_password: ""
  170.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  171.         name: root
  172.         password: "{{ mysqlrootpasswd }}"
  173.       ignore_errors: true
  174.       tags:
  175.        - never
  176.         - lamp
  177.         - mysql
  178.     - name: "Creating a datababe {{ blog_db }}"
  179.       mysql_db:
  180.         login_user: root
  181.         login_password: "{{ mysqlrootpasswd }}"
  182.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  183.         name: "{{ blog_db }}"
  184.       tags:
  185.        - never
  186.         - lamp
  187.         - mysql
  188.     - name: "Creating an extra user {{ blog_user }}"
  189.       mysql_user:
  190.         login_user: root
  191.         login_password: "{{ mysqlrootpasswd }}"
  192.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  193.         name: "{{ blog_user}}"
  194.         password: "{{ blog_passwd }}"
  195.         host: "%"
  196.         priv: "{{ blog_db }}.*:ALL"
  197.       tags:
  198.        - never
  199.         - lamp
  200.         - mysql
  201.     - name: "Wget the wordpress"
  202.       get_url:
  203.         url: "{{ wordpress_url }}"
  204.         dest: /tmp/wordpress.tar.gz
  205.       tags:
  206.        - never
  207.         - wordpress
  208.     - name: "unarchive the worpress url"
  209.       unarchive:
  210.         src: /tmp/wordpress.tar.gz
  211.         dest: /tmp/
  212.         remote_src: true
  213.       tags:
  214.        - never
  215.         - wordpress
  216.     - name: "Copying the wordpress contents to the docroot"
  217.       copy:
  218.         src: /tmp/wordpress/
  219.         dest: "/var/www/html/{{ httpd_site }}"
  220.         owner: "{{ httpd_owner }}"
  221.         group: "{{ httpd_group }}"
  222.         remote_src: true
  223.       tags:
  224.        - never
  225.         - wordpress
  226.     - name: "Copying wp-config.php to remote server"
  227.       template:
  228.         src: ./wp-config.php.tmpl
  229.         dest: "/var/www/html/{{ httpd_site }}/wp-config.php"
  230.         owner: "{{ httpd_owner }}"
  231.         group: "{{ httpd_group }}"
  232.       tags:
  233.        - never
  234.         - wordpress
  235.     - name: "Cleanup"
  236.       file:
  237.         name: "{{ item }}"
  238.         state: absent
  239.       with_items:
  240.        - /tmp/wordpress.tar.gz
  241.         - /tmp/wordpress
  242.         - /var/lib/new
  243.       tags:
  244.        - never
  245.         - wordpress
  246.  
  247.  
  248.  
  249. #### Output ####
  250.  
  251. [ec2-user@ip-172-31-45-107 ~]$ ansible-playbook -i inventory main.yml -t lamp,mysql,wordpress --vault-password-file=passwd.sh
  252.  
  253. PLAY [Wordpress LAMP installation] *************************************************************************************************************************
  254. TASK [Gathering Facts] *************************************************************************************************************************************[WARNING]: Platform linux on host 172.31.40.182 is using the discovered Python interpreter at /usr/bin/python3.9, but future installation of another Python
  255. interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more
  256. information.
  257. ok: [172.31.40.182]
  258.  
  259. TASK [Installing Apache and php-fpm] ***********************************************************************************************************************changed: [172.31.40.182]
  260.  
  261. TASK [Copying the httpd conf template to the remote machine] ***********************************************************************************************changed: [172.31.40.182]
  262.  
  263. TASK [Creating the document root /var/www/html/blog.chrisich.fun] ******************************************************************************************changed: [172.31.40.182]
  264.  
  265. TASK [Copying the virtual host template to /etc/httpd/conf.d/blog.chrisich.fun] ****************************************************************************changed: [172.31.40.182]
  266.  
  267. TASK [Copying the contents to document root] ***************************************************************************************************************changed: [172.31.40.182]
  268.  
  269. TASK [Service php-fpm restart/enable when condition meets] *************************************************************************************************changed: [172.31.40.182]
  270.  
  271. TASK [Service httpd restart/enable when condition meets] ***************************************************************************************************changed: [172.31.40.182]
  272.  
  273. TASK [Installing mariadb required packages] ****************************************************************************************************************changed: [172.31.40.182]
  274.  
  275. TASK [Restarting mariadb server] ***************************************************************************************************************************changed: [172.31.40.182]
  276.  
  277. TASK [Creating a new directory to copy the /var/lib/mysql] *************************************************************************************************changed: [172.31.40.182]
  278.  
  279. TASK [Copy the /var/lib/mysql to /var/lib/new] *************************************************************************************************************changed: [172.31.40.182]
  280.  
  281. TASK [Adding a filesystem ext4 to /var/lib/mysql] **********************************************************************************************************changed: [172.31.40.182]
  282.  
  283. TASK [Adding /var/lib/mysql it to fstab] *******************************************************************************************************************changed: [172.31.40.182]
  284.  
  285. TASK [Copy the /var/lib/mysql to /var/lib/new] *************************************************************************************************************changed: [172.31.40.182]
  286.  
  287. TASK [Installing the pip3 module for mysql] ****************************************************************************************************************changed: [172.31.40.182]
  288.  
  289. TASK [Restarting mariadb server] ***************************************************************************************************************************changed: [172.31.40.182]
  290.  
  291. TASK [Setting root password for mysql] *********************************************************************************************************************changed: [172.31.40.182]
  292.  
  293. TASK [Creating a datababe blog_db] *************************************************************************************************************************changed: [172.31.40.182]
  294.  
  295. TASK [Creating an extra user blog_user] ********************************************************************************************************************changed: [172.31.40.182]
  296.  
  297. TASK [Wget the wordpress] **********************************************************************************************************************************changed: [172.31.40.182]
  298.  
  299. TASK [unarchive the worpress url] **************************************************************************************************************************changed: [172.31.40.182]
  300.  
  301. TASK [Copying the wordpress contents to the docroot] *******************************************************************************************************changed: [172.31.40.182]
  302.  
  303. TASK [Copying wp-config.php to remote server] **************************************************************************************************************changed: [172.31.40.182]
  304.  
  305. TASK [Cleanup] *********************************************************************************************************************************************changed: [172.31.40.182] => (item=/tmp/wordpress.tar.gz)
  306. changed: [172.31.40.182] => (item=/tmp/wordpress)
  307. changed: [172.31.40.182] => (item=/var/lib/new)
  308.  
  309. PLAY RECAP *************************************************************************************************************************************************172.31.40.182              : ok=25   changed=24   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  310.  
  311.  
  312.  
  313. #### Verification ####
  314.  
  315. [ec2-user@ip-172-31-40-182 ~]$ lsblk
  316. NAME      MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
  317. xvda      202:0    0   8G  0 disk
  318. ├─xvda1   202:1    0   8G  0 part /
  319. ├─xvda127 259:0    0   1M  0 part
  320. └─xvda128 259:1    0  10M  0 part
  321. xvdb      202:16   0   5G  0 disk /var/lib/mysql
  322. [ec2-user@ip-172-31-40-182 ~]$
  323. [ec2-user@ip-172-31-40-182 ~]$
  324. [ec2-user@ip-172-31-40-182 ~]$ cat /etc/fstab
  325. #
  326. UUID=0bfb954f-5412-4d5a-8d40-3a2596532c79     /           xfs    defaults,noatime  1   1
  327. UUID=8EFE-43B9        /boot/efi       vfat    defaults,noatime,uid=0,gid=0,umask=0077,shortname=winnt,x-systemd.automount 0 2
  328. /dev/xvdb /var/lib/mysql/ ext4 defaults 0 0
  329. [ec2-user@ip-172-31-40-182 ~]$
  330. [ec2-user@ip-172-31-40-182 ~]$
  331. [ec2-user@ip-172-31-40-182 ~]$ ll /var/lib/mysql/
  332. total 122944
  333. -rw-rw----. 1 mysql mysql     24576 Jun 27 18:55 aria_log.00000001
  334. -rw-rw----. 1 mysql mysql        52 Jun 27 18:55 aria_log_control
  335. drwx------. 2 mysql mysql      4096 Jun 27 18:52 blog_db
  336. -rw-rw----. 1 mysql mysql       976 Jun 27 18:55 ib_buffer_pool
  337. -rw-rw----. 1 mysql mysql 100663296 Jun 27 18:55 ib_logfile0
  338. -rw-rw----. 1 mysql mysql  12582912 Jun 27 18:55 ibdata1
  339. -rw-rw----. 1 mysql mysql  12582912 Jun 27 18:55 ibtmp1
  340. drwx------. 2 mysql mysql     16384 Jun 27 18:52 lost+found
  341. -rw-rw----. 1 mysql mysql         0 Jun 27 18:52 multi-master.info
  342. drwx------. 2 mysql mysql      4096 Jun 27 18:52 mysql
  343. srwxrwxrwx. 1 mysql mysql         0 Jun 27 18:55 mysql.sock
  344. -rw-rw----. 1 mysql mysql        16 Jun 27 18:52 mysql_upgrade_info
  345. drwx------. 2 mysql mysql      4096 Jun 27 18:52 performance_schema
Advertisement
Add Comment
Please, Sign In to add comment