chrisluiz16

Day 7

Jun 27th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 18.18 KB | None | 0 0
  1.  
  2.  
  3. -------------------------------------------------------------- Task 1-------------------------------------------
  4.  
  5. #### variables ####
  6.  
  7. [ec2-user@ip-172-31-45-107 lamp-project]$ cat apache.vars
  8. ---
  9.  
  10. httpd_port: 80
  11. httpd_site: blog.chrisich.fun
  12. httpd_owner: apache
  13. httpd_group: apache
  14. packages:
  15.  - httpd
  16.   - php-fpm
  17. [ec2-user@ip-172-31-45-107 lamp-project]$
  18. [ec2-user@ip-172-31-45-107 lamp-project]$
  19. [ec2-user@ip-172-31-45-107 lamp-project]$ cat mariadb.vars
  20. ---
  21.  
  22. mysql_packages:
  23.  - mariadb105-server
  24.   - php8.1-mysqlnd
  25.   - python3-pip
  26. mysqlrootpasswd: mysqlroot123
  27. mysql_docroot: /var/lib/mysql
  28. blog_user: blog_user
  29. blog_passwd: blog123
  30. blog_db: blog_db
  31. [ec2-user@ip-172-31-45-107 lamp-project]$
  32. [ec2-user@ip-172-31-45-107 lamp-project]$ cat wordpress.vars
  33. ---
  34.  
  35. wordpress_url: https://wordpress.org/wordpress-6.2.tar.gz
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. #### Playbook ####
  45.  
  46.  
  47. ---
  48.  
  49. - name : "Wordpress LAMP installation"
  50.   hosts: amazon
  51.   become: true
  52.   vars_files:
  53.    - apache.vars
  54.     - mariadb.vars
  55.     - wordpress.vars
  56.   tasks:
  57.     - name: "Installing Apache and php-fpm"
  58.       yum:
  59.         name: "{{ packages }}"
  60.         state: present
  61.       register: package_status
  62.       tags:
  63.        - lamp
  64.     - name: "Copying the httpd conf template to the remote machine"
  65.       template:
  66.         src: ./httpd.conf.tmpl
  67.         dest: /etc/httpd/conf/httpd.conf
  68.       register: conf_status
  69.       tags:
  70.        - lamp
  71.     - name: "Creating the document root /var/www/html/{{ httpd_site }}"
  72.       file:
  73.         path: "/var/www/html/{{ httpd_site }}"
  74.         state: directory
  75.         owner: "{{ httpd_owner }}"
  76.         group: "{{ httpd_group }}"
  77.       tags:
  78.        - lamp
  79.     - name: "Copying the virtual host template to /etc/httpd/conf.d/{{ httpd_site }}"
  80.       template:
  81.         src: ./virtualhost.conf.tmpl
  82.         dest: "/etc/httpd/conf.d/default.conf"
  83.       register: virtualhost_status
  84.       tags:
  85.        - lamp
  86.     - name: "Copying the contents to document root"
  87.       when: package_status.changed == true
  88.       copy:
  89.         src: ./www/
  90.         dest: "/var/www/html/{{ httpd_site }}"
  91.         owner: "{{ httpd_owner }}"
  92.         group: "{{ httpd_group }}"
  93.       register: index_status
  94.       tags:
  95.        - lamp
  96.     - name: "Service php-fpm restart/enable when condition meets"
  97.       when: package_status.changed == true or index_status.changed == true
  98.       service:
  99.         name: php-fpm
  100.         state: restarted
  101.         enabled: true
  102.       tags:
  103.        - lamp
  104.     - name: "Service httpd restart/enable when condition meets"
  105.       when: package_status.changed == true or conf_status.changed == true or virtualhost_status.changed == true
  106.       service:
  107.         name: httpd
  108.         state: restarted
  109.         enabled: true
  110.       tags:
  111.        - lamp
  112.     - name: "Installing mariadb required packages"
  113.       yum:
  114.         name: "{{ mysql_packages }}"
  115.         state: present
  116.       tags:
  117.        - lamp
  118.         - mysql
  119.     - name: "Restarting mariadb server"
  120.       service:
  121.         name: mariadb
  122.         state: restarted
  123.         enabled: true
  124.       tags:
  125.        - lamp
  126.         - mysql
  127.     - name: "Installing the pip3 module for mysql"
  128.       pip:
  129.         name: PyMySQL
  130.         state: present
  131.       tags:
  132.        - lamp
  133.         - mysql
  134.     - name: "Setting root password for mysql"
  135.       mysql_user:
  136.         login_user: root
  137.         login_password: ""
  138.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  139.         name: root
  140.         password: "{{ mysqlrootpasswd }}"
  141.       ignore_errors: true
  142.       tags:
  143.        - lamp
  144.         - mysql
  145.     - name: "Creating a datababe {{ blog_db }}"
  146.       mysql_db:
  147.         login_user: root
  148.         login_password: "{{ mysqlrootpasswd }}"
  149.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  150.         name: "{{ blog_db }}"
  151.       tags:
  152.        - lamp
  153.         - mysql
  154.     - name: "Creating an extra user {{ blog_user }}"
  155.       mysql_user:
  156.         login_user: root
  157.         login_password: "{{ mysqlrootpasswd }}"
  158.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  159.         name: "{{ blog_user}}"
  160.         password: "{{ blog_passwd }}"
  161.         host: "%"
  162.         priv: "{{ blog_db }}.*:ALL"
  163.       tags:
  164.        - lamp
  165.         - mysql
  166.     - name: "Wget the wordpress"
  167.       get_url:
  168.         url: "{{ wordpress_url }}"
  169.         dest: /tmp/wordpress.tar.gz
  170.       tags: wordpress
  171.     - name: "unarchive the worpress url"
  172.       unarchive:
  173.         src: /tmp/wordpress.tar.gz
  174.         dest: /tmp/
  175.         remote_src: true
  176.       tags: wordpress
  177.     - name: "Copying the wordpress contents to the docroot"
  178.       copy:
  179.         src: /tmp/wordpress/
  180.         dest: "/var/www/html/{{ httpd_site }}"
  181.         owner: "{{ httpd_owner }}"
  182.         group: "{{ httpd_group }}"
  183.         remote_src: true
  184.       tags: wordpress
  185.     - name: "Copying wp-config.php to remote server"
  186.       template:
  187.         src: ./wp-config.php.tmpl
  188.         dest: "/var/www/html/{{ httpd_site }}/wp-config.php"
  189.         owner: "{{ httpd_owner }}"
  190.         group: "{{ httpd_group }}"
  191.       tags: wordpress
  192.     - name: "Cleanup"
  193.       file:
  194.         name: "{{ item }}"
  195.         state: absent
  196.       with_items:
  197.        - /tmp/wordpress.tar.gz
  198.         - /tmp/wordpress
  199.       tags: wordpress
  200.      
  201.    
  202.    
  203. #### Output ####
  204.  
  205. [ec2-user@ip-172-31-45-107 lamp-project]$ ansible-vault encrypt apache.vars mariadb.vars wordpress.vars
  206. New Vault password:
  207. Confirm New Vault password:
  208. Encryption successful
  209.  
  210.  
  211.  
  212.  
  213.  
  214. [ec2-user@ip-172-31-45-107 lamp-project]$ ansible-playbook -i inventory lamp.yml -t lamp,mysql,wordpress --ask-vault-pass
  215. Vault password:
  216. PLAY [Wordpress LAMP installation] *************************************************************************************************************************
  217.  
  218. TASK [Gathering Facts] *************************************************************************************************************************************
  219. [WARNING]: Platform linux on host 172.31.42.193 is using the discovered Python interpreter at /usr/bin/python3.9, but future installation of another Python
  220. interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more
  221. information.
  222. ok: [172.31.42.193]
  223.  
  224. TASK [Installing Apache and php-fpm] ***********************************************************************************************************************
  225. changed: [172.31.42.193]
  226.  
  227. TASK [Copying the httpd conf template to the remote machine] ***********************************************************************************************
  228. changed: [172.31.42.193]
  229.  
  230. TASK [Creating the document root /var/www/html/blog.chrisich.fun] ******************************************************************************************
  231. changed: [172.31.42.193]
  232.  
  233. TASK [Copying the virtual host template to /etc/httpd/conf.d/blog.chrisich.fun] ****************************************************************************
  234. changed: [172.31.42.193]
  235.  
  236. TASK [Copying the contents to document root] ***************************************************************************************************************
  237. changed: [172.31.42.193]
  238.  
  239. TASK [Service php-fpm restart/enable when condition meets] *************************************************************************************************
  240. changed: [172.31.42.193]
  241.  
  242. TASK [Service httpd restart/enable when condition meets] ***************************************************************************************************
  243. changed: [172.31.42.193]
  244.  
  245. TASK [Installing mariadb required packages] ****************************************************************************************************************
  246. changed: [172.31.42.193]
  247.  
  248. TASK [Restarting mariadb server] ***************************************************************************************************************************
  249. changed: [172.31.42.193]
  250.  
  251. TASK [Installing the pip3 module for mysql] ****************************************************************************************************************
  252. ok: [172.31.42.193]
  253.  
  254. TASK [Setting root password for mysql] *********************************************************************************************************************
  255. changed: [172.31.42.193]
  256.  
  257. TASK [Creating a datababe blog_db] *************************************************************************************************************************
  258. changed: [172.31.42.193]
  259.  
  260. TASK [Creating an extra user blog_user] ********************************************************************************************************************
  261. changed: [172.31.42.193]
  262.  
  263. TASK [Wget the wordpress] **********************************************************************************************************************************
  264. changed: [172.31.42.193]
  265.  
  266. TASK [unarchive the worpress url] **************************************************************************************************************************
  267. changed: [172.31.42.193]
  268.  
  269. TASK [Copying the wordpress contents to the docroot] *******************************************************************************************************
  270. changed: [172.31.42.193]
  271.  
  272. TASK [Copying wp-config.php to remote server] **************************************************************************************************************
  273. changed: [172.31.42.193]
  274.  
  275. TASK [Cleanup] *********************************************************************************************************************************************
  276. changed: [172.31.42.193] => (item=/tmp/wordpress.tar.gz)
  277. changed: [172.31.42.193] => (item=/tmp/wordpress)
  278.  
  279. PLAY RECAP *************************************************************************************************************************************************
  280. 172.31.42.193              : ok=19   changed=17   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  281.  
  282.  
  283.  
  284.  
  285. ------------------------------------------------------------ Task 2---------------------------------------------------------------------------------------------------
  286.  
  287.  
  288.  
  289. ---
  290.  
  291. - name : "Wordpress LAMP installation"
  292.   hosts: amazon
  293.   become: true
  294.   vars_files:
  295.    - apache.vars
  296.     - mariadb.vars
  297.     - wordpress.vars
  298.   tasks:
  299.     - name: "Installing Apache and php-fpm"
  300.       yum:
  301.         name: "{{ packages }}"
  302.         state: present
  303.       register: package_status
  304.       tags:
  305.        - never
  306.         - lamp
  307.     - name: "Copying the httpd conf template to the remote machine"
  308.       template:
  309.         src: ./httpd.conf.tmpl
  310.         dest: /etc/httpd/conf/httpd.conf
  311.       register: conf_status
  312.       tags:
  313.        - never
  314.         - lamp
  315.     - name: "Creating the document root /var/www/html/{{ httpd_site }}"
  316.       file:
  317.         path: "/var/www/html/{{ httpd_site }}"
  318.         state: directory
  319.         owner: "{{ httpd_owner }}"
  320.         group: "{{ httpd_group }}"
  321.       tags:
  322.        - never
  323.         - lamp
  324.     - name: "Copying the virtual host template to /etc/httpd/conf.d/{{ httpd_site }}"
  325.       template:
  326.         src: ./virtualhost.conf.tmpl
  327.         dest: "/etc/httpd/conf.d/default.conf"
  328.       register: virtualhost_status
  329.       tags:
  330.        - never
  331.         - lamp
  332.     - name: "Copying the contents to document root"
  333.       when: package_status.changed == true
  334.       copy:
  335.         src: ./www/
  336.         dest: "/var/www/html/{{ httpd_site }}"
  337.         owner: "{{ httpd_owner }}"
  338.         group: "{{ httpd_group }}"
  339.       register: index_status
  340.       tags:
  341.        - never
  342.         - lamp
  343.     - name: "Service php-fpm restart/enable when condition meets"
  344.       when: package_status.changed == true or index_status.changed == true
  345.       service:
  346.         name: php-fpm
  347.         state: restarted
  348.         enabled: true
  349.       tags:
  350.        - never
  351.         - lamp
  352.     - name: "Service httpd restart/enable when condition meets"
  353.       when: package_status.changed == true or conf_status.changed == true or virtualhost_status.changed == true
  354.       service:
  355.         name: httpd
  356.         state: restarted
  357.         enabled: true
  358.       tags:
  359.        - never
  360.         - lamp
  361.     - name: "Installing mariadb required packages"
  362.       yum:
  363.         name: "{{ mysql_packages }}"
  364.         state: present
  365.       tags:
  366.        - never
  367.         - lamp
  368.         - mysql
  369.     - name: "Restarting mariadb server"
  370.       service:
  371.         name: mariadb
  372.         state: restarted
  373.         enabled: true
  374.       tags:
  375.        - never
  376.         - lamp
  377.         - mysql
  378.     - name: "Installing the pip3 module for mysql"
  379.       pip:
  380.         name: PyMySQL
  381.         state: present
  382.       tags:
  383.        - never
  384.         - lamp
  385.         - mysql
  386.     - name: "Setting root password for mysql"
  387.       mysql_user:
  388.         login_user: root
  389.         login_password: ""
  390.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  391.         name: root
  392.         password: "{{ mysqlrootpasswd }}"
  393.       ignore_errors: true
  394.       tags:
  395.        - never
  396.         - lamp
  397.         - mysql
  398.     - name: "Creating a datababe {{ blog_db }}"
  399.       mysql_db:
  400.         login_user: root
  401.         login_password: "{{ mysqlrootpasswd }}"
  402.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  403.         name: "{{ blog_db }}"
  404.       tags:
  405.        - never
  406.         - lamp
  407.         - mysql
  408.     - name: "Creating an extra user {{ blog_user }}"
  409.       mysql_user:
  410.         login_user: root
  411.         login_password: "{{ mysqlrootpasswd }}"
  412.         login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
  413.         name: "{{ blog_user}}"
  414.         password: "{{ blog_passwd }}"
  415.         host: "%"
  416.         priv: "{{ blog_db }}.*:ALL"
  417.       tags:
  418.        - never
  419.         - lamp
  420.         - mysql
  421.     - name: "Wget the wordpress"
  422.       get_url:
  423.         url: "{{ wordpress_url }}"
  424.         dest: /tmp/wordpress.tar.gz
  425.       tags: never,wordpress
  426.     - name: "unarchive the worpress url"
  427.       unarchive:
  428.         src: /tmp/wordpress.tar.gz
  429.         dest: /tmp/
  430.         remote_src: true
  431.       tags: never,wordpress
  432.     - name: "Copying the wordpress contents to the docroot"
  433.       copy:
  434.         src: /tmp/wordpress/
  435.         dest: "/var/www/html/{{ httpd_site }}"
  436.         owner: "{{ httpd_owner }}"
  437.         group: "{{ httpd_group }}"
  438.         remote_src: true
  439.       tags: never,wordpress
  440.     - name: "Copying wp-config.php to remote server"
  441.       template:
  442.         src: ./wp-config.php.tmpl
  443.         dest: "/var/www/html/{{ httpd_site }}/wp-config.php"
  444.         owner: "{{ httpd_owner }}"
  445.         group: "{{ httpd_group }}"
  446.       tags: never,wordpress
  447.     - name: "Cleanup"
  448.       file:
  449.         name: "{{ item }}"
  450.         state: absent
  451.       with_items:
  452.        - /tmp/wordpress.tar.gz
  453.         - /tmp/wordpress
  454.       tags: never,wordpress
  455.      
  456.  
  457.  
  458. #### Output {checking whether playbook is running without tags} ####
  459.  
  460. [ec2-user@ip-172-31-45-107 lamp-project]$ ansible-playbook -i inventory  lamp.yml  --ask-vault-pass
  461. Vault password:
  462. PLAY [Wordpress LAMP installation] *************************************************************************************************************************
  463.  
  464. TASK [Gathering Facts] *************************************************************************************************************************************
  465. [WARNING]: Platform linux on host 172.31.42.193 is using the discovered Python interpreter at /usr/bin/python3.9, but future installation of another Python
  466. interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more
  467. information.
  468. ok: [172.31.42.193]
  469.  
  470. PLAY RECAP *************************************************************************************************************************************************
  471. 172.31.42.193              : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482. ------------------------------------------------------------ Task 3 --vault-password-file ---------------------------------------------------------------------------------------------------
  483.  
  484. [ec2-user@ip-172-31-45-107 lamp-project]$ cat passwd.sh
  485. #!/bin/bash
  486.  
  487. aws secretsmanager get-secret-value --secret-id ansible_vault | jq --raw-output .SecretString | jq -r ".vault_passwd"
  488.  
  489.  
  490.  
  491. #### output ####
  492. [ec2-user@ip-172-31-45-107 lamp-project]$ ansible-playbook -i inventory  -t wordpress lamp.yml  --vault-password-file=passwd.sh
  493.  
  494. PLAY [Wordpress LAMP installation] *************************************************************************************************************************
  495.  
  496. TASK [Gathering Facts] *************************************************************************************************************************************
  497. [WARNING]: Platform linux on host 172.31.42.193 is using the discovered Python interpreter at /usr/bin/python3.9, but future installation of another Python
  498. interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more
  499. information.
  500. ok: [172.31.42.193]
  501.  
  502. TASK [Wget the wordpress] **********************************************************************************************************************************
  503. changed: [172.31.42.193]
  504.  
  505. TASK [unarchive the worpress url] **************************************************************************************************************************
  506. changed: [172.31.42.193]
  507.  
  508. TASK [Copying the wordpress contents to the docroot] *******************************************************************************************************
  509. ok: [172.31.42.193]
  510.  
  511. TASK [Copying wp-config.php to remote server] **************************************************************************************************************
  512. ok: [172.31.42.193]
  513.  
  514. TASK [Cleanup] *********************************************************************************************************************************************
  515. changed: [172.31.42.193] => (item=/tmp/wordpress.tar.gz)
  516. changed: [172.31.42.193] => (item=/tmp/wordpress)
  517.  
  518. PLAY RECAP *************************************************************************************************************************************************
  519. 172.31.42.193              : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  520.  
Advertisement
Add Comment
Please, Sign In to add comment