chrisluiz16

flaska-app-github-day5

Jun 23rd, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 18.83 KB | None | 0 0
  1.  
  2.  
  3. -------------------------------------------------------------- Task 1-------------------------------------------
  4.  
  5.  
  6. #### Inventory ####
  7.  
  8.  
  9. [ec2-user@ip-172-31-40-58 ~]$ cat inventory
  10. [amazon]
  11. 172.31.37.63 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
  12.  
  13. ###  Converted the conf to template ###
  14.  
  15. [ec2-user@ip-172-31-40-58 ~]$ grep ^Listen httpd.conf.tmpl
  16. Listen {{ httpd_port }}
  17. [ec2-user@ip-172-31-40-58 ~]$
  18. [ec2-user@ip-172-31-40-58 ~]$
  19. [ec2-user@ip-172-31-40-58 ~]$ cat virtual.conf.tmpl
  20. <virtualhost *:{{ httpd_port }}>
  21.  
  22.    ServerName {{ httpd_url }}
  23.    DocumentRoot /var/www/html/{{ httpd_url }}/
  24.    DirectoryIndex index.php index.html
  25.    <Directory "/var/www/html/{{ httpd_url }}/">
  26.                AllowOverride all
  27.    </Directory>
  28. </virtualhost>
  29.  
  30.  
  31.  
  32. #### Playbook without using git module ####
  33.  
  34. ---
  35.  
  36. - name: "Php website hosting using git clone from master machine"
  37.   hosts: amazon
  38.   become: true
  39.   vars_files:
  40.     - variables.yml
  41.   tasks:
  42.     - name: "Installing webserver packages"
  43.       yum:
  44.         name: "{{ packages }}"
  45.         state: present
  46.       register: webpackage_status
  47.     - name: "Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf"
  48.       template:
  49.         src: ./httpd.conf.tmpl
  50.         dest: /etc/httpd/conf/httpd.conf
  51.       register: httpd_conf_status
  52.     - name: "Copying the virtaulhost of {{ httpd_url }} to /etc/httpd/conf.d/{{ httpd_url }}"
  53.       template:
  54.         src: ./virtual.conf.tmpl
  55.         dest: "/etc/httpd/conf.d/{{ httpd_url }}.conf"
  56.       register: virtualhost_conf_status
  57.     - name: "Creating the directory for the documentroot /var/www/html/{{ httpd_url }}"
  58.       file:
  59.         path: "/var/www/html/{{ httpd_url }}"
  60.         state: directory
  61.         owner: "{{ httpd_owner }}"
  62.         group: "{{ httpd_group }}"
  63.  
  64.     - name: "Copying the website to the documentroot /var/www/html/{{ httpd_url }}/"
  65.       copy:
  66.         src: ./website/
  67.         dest: "/var/www/html/{{ httpd_url }}/"
  68.       register: copy_status
  69.  
  70.     - name: "Restart httpd if condition meets"
  71.       when: webpackage_status.changed == true or httpd_conf_status.changed == true or virtualhost_conf_status.changed == true
  72.       service:
  73.         name: httpd
  74.         state: restarted
  75.         enabled: true
  76.     - name: "Restart php-fpm whenc condition meets"
  77.       when: webpackage_status.changed == true or copy_status.changed == true
  78.       service:
  79.         name: php-fpm
  80.         state: restarted
  81.         enabled: true
  82.  
  83.  
  84. #### vars_files ####
  85.  
  86. [ec2-user@ip-172-31-40-58 ~]$ cat variables.yml
  87. ---
  88.  
  89. httpd_port: 8080
  90. httpd_url: bigbasket.chrisich.fun
  91. httpd_owner: apache
  92. httpd_group: apache
  93. packages:
  94.   - httpd
  95.   - php
  96.  
  97.  
  98. ### Output when changed the port 80 to 8080 ####
  99.  
  100. [ec2-user@ip-172-31-40-58 ~]$ ansible-playbook -i inventory php-app.yml
  101.  
  102. PLAY [Php website hosting using git clone from master machine] *********************************************************************************************
  103.  
  104. TASK [Gathering Facts] *************************************************************************************************************************************
  105. ok: [172.31.37.63]
  106.  
  107. TASK [Installing webserver packages] ***********************************************************************************************************************
  108. ok: [172.31.37.63]
  109.  
  110. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
  111. changed: [172.31.37.63]
  112.  
  113. TASK [Copying the virtaulhost of bigbasket.chrisich.fun to /etc/httpd/conf.d/bigbasket.chrisich.fun] *******************************************************
  114. changed: [172.31.37.63]
  115.  
  116. TASK [Creating the directory for the documentroot /var/www/html/bigbasket.chrisich.fun] ********************************************************************
  117. ok: [172.31.37.63]
  118.  
  119. TASK [Copying the website to the documentroot /var/www/html/bigbasket.chrisich.fun/] ***********************************************************************
  120. ok: [172.31.37.63]
  121.  
  122. TASK [Restart httpd if condition meets] ********************************************************************************************************************
  123. changed: [172.31.37.63]
  124.  
  125. TASK [Restart php-fpm whenc condition meets] ***************************************************************************************************************
  126. skipping: [172.31.37.63]
  127.  
  128. PLAY RECAP *************************************************************************************************************************************************
  129. 172.31.37.63               : ok=7    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
  130.  
  131.  
  132.  
  133.  
  134. -------------------------------------------------------------- Task 2 -------------------------------------------
  135.  
  136.  
  137. #### git module playbook ####
  138.  
  139. [ec2-user@ip-172-31-40-58 ~]$ cat variables.yml
  140. ---
  141.  
  142. httpd_port: 80
  143. httpd_url: bigbasket.chrisich.fun
  144. git_url: https://github.com/Chris-luiz-16/aws-elb-site.git
  145. httpd_owner: apache
  146. httpd_group: apache
  147. packages:
  148.   - httpd
  149.   - php
  150. additional_packages:
  151.   - git
  152.  
  153.  
  154.  
  155.  
  156.  
  157. #### Playbook ####
  158.  
  159. ---
  160.  
  161. - name: "Php website hosting using git clone from master machine"
  162.   hosts: amazon
  163.   become: true
  164.   vars_files:
  165.     - variables.yml
  166.   tasks:
  167.     - name: "Installing webserver packages"
  168.       yum:
  169.         name: "{{ packages }}"
  170.         state: present
  171.       register: webpackage_status
  172.     - name: "Installing additional packages"
  173.       yum:
  174.         name: "{{ additional_packages }}"
  175.         state: present
  176.     - name: "Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf"
  177.       template:
  178.         src: ./httpd.conf.tmpl
  179.         dest: /etc/httpd/conf/httpd.conf
  180.       register: httpd_conf_status
  181.     - name: "Copying the virtaulhost of {{ httpd_url }} to /etc/httpd/conf.d/{{ httpd_url }}"
  182.       template:
  183.         src: ./virtual.conf.tmpl
  184.         dest: "/etc/httpd/conf.d/{{ httpd_url }}.conf"
  185.       register: virtualhost_conf_status
  186.     - name: "Git clone to remote machine"
  187.       git:
  188.         repo: "{{ git_url }}"
  189.         dest:  /var/website/
  190.       register: git_status
  191.  
  192.     - name: "Creating the directory for the documentroot /var/www/html/{{ httpd_url }}"
  193.       file:
  194.         path: "/var/www/html/{{ httpd_url }}"
  195.         state: directory
  196.         owner: "{{ httpd_owner }}"
  197.         group: "{{ httpd_group }}"
  198.  
  199.     - name: "Copying the website to the documentroot /var/www/html/{{ httpd_url }}/"
  200.       when: git_status.changed == true
  201.       copy:
  202.         src: /var/website/
  203.         dest: "/var/www/html/{{ httpd_url }}/"
  204.         owner: "{{ httpd_owner }}"
  205.         group: "{{ httpd_group }}"
  206.         remote_src: true
  207.       register: copy_status
  208.  
  209.     - name: "Restart httpd if condition meets"
  210.       when: webpackage_status.changed == true or httpd_conf_status.changed == true or virtualhost_conf_status.changed == true
  211.       service:
  212.         name: httpd
  213.         state: restarted
  214.         enabled: true
  215.     - name: "Restart php-fpm whenc condition meets"
  216.       when: webpackage_status.changed == true or git_status.changed == true
  217.       service:
  218.         name: php-fpm
  219.         state: restarted
  220.         enabled: true
  221.  
  222.  
  223.  
  224. #### Output no change ####
  225.  
  226. PLAY [Php website hosting using git clone from master machine] *********************************************************
  227.  
  228. TASK [Gathering Facts] *************************************************************************************************
  229. [WARNING]: Platform linux on host 172.31.37.63 is using the discovered Python interpreter at /usr/bin/python3.9, but
  230. future installation of another Python interpreter could change the meaning of that path. See
  231. https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
  232. ok: [172.31.37.63]
  233.  
  234. TASK [Installing webserver packages] ***********************************************************************************
  235. ok: [172.31.37.63]
  236.  
  237. TASK [Installing additional packages] **********************************************************************************
  238. ok: [172.31.37.63]
  239.  
  240. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************
  241. ok: [172.31.37.63]
  242.  
  243. TASK [Copying the virtaulhost of bigbasket.chrisich.fun to /etc/httpd/conf.d/bigbasket.chrisich.fun] *******************
  244. ok: [172.31.37.63]
  245.  
  246. TASK [Git clone to remote machine] *************************************************************************************
  247. ok: [172.31.37.63]
  248.  
  249. TASK [Creating the directory for the documentroot /var/www/html/bigbasket.chrisich.fun] ********************************
  250. ok: [172.31.37.63]
  251.  
  252. TASK [Copying the website to the documentroot /var/www/html/bigbasket.chrisich.fun/] ***********************************
  253. skipping: [172.31.37.63]
  254.  
  255. TASK [Restart httpd if condition meets] ********************************************************************************
  256. skipping: [172.31.37.63]
  257.  
  258. TASK [Restart php-fpm whenc condition meets] ***************************************************************************
  259. skipping: [172.31.37.63]
  260.  
  261. PLAY RECAP *************************************************************************************************************
  262. 172.31.37.63               : ok=7    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  263.  
  264.  
  265.  
  266.  
  267. #### Output version upgrade ####
  268.  
  269.  
  270.  [ec2-user@ip-172-31-40-58 ~]$ ansible-playbook -i inventory php-app.yml
  271.  
  272. PLAY [Php website hosting using git clone from master machine] *********************************************************
  273.  
  274. TASK [Gathering Facts] *************************************************************************************************
  275. [WARNING]: Platform linux on host 172.31.37.63 is using the discovered Python interpreter at /usr/bin/python3.9, but
  276. future installation of another Python interpreter could change the meaning of that path. See
  277. https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
  278. ok: [172.31.37.63]
  279.  
  280. TASK [Installing webserver packages] ***********************************************************************************
  281. ok: [172.31.37.63]
  282.  
  283. TASK [Installing additional packages] **********************************************************************************
  284. ok: [172.31.37.63]
  285.  
  286. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************
  287. ok: [172.31.37.63]
  288.  
  289. TASK [Copying the virtaulhost of bigbasket.chrisich.fun to /etc/httpd/conf.d/bigbasket.chrisich.fun] *******************
  290. ok: [172.31.37.63]
  291.  
  292. TASK [Git clone to remote machine] *************************************************************************************
  293. changed: [172.31.37.63]
  294.  
  295. TASK [Creating the directory for the documentroot /var/www/html/bigbasket.chrisich.fun] ********************************
  296. ok: [172.31.37.63]
  297.  
  298. TASK [Copying the website to the documentroot /var/www/html/bigbasket.chrisich.fun/] ***********************************
  299. changed: [172.31.37.63]
  300.  
  301. TASK [Restart httpd if condition meets] ********************************************************************************
  302. skipping: [172.31.37.63]
  303.  
  304. TASK [Restart php-fpm whenc condition meets] ***************************************************************************
  305. changed: [172.31.37.63]
  306.  
  307. PLAY RECAP *************************************************************************************************************
  308. 172.31.37.63               : ok=9    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  ---------------------------- Flask app additional task ------------------------------------------------
  319.  
  320.  
  321.  #### variable ####
  322.  
  323. [ec2-user@ip-172-31-40-58 project]$ cat flask-variable.yml
  324. ---
  325.  
  326. git_url: https://github.com/Chris-luiz-16/Hello-world-flask-app-with-systemd-file.git
  327. flask_owner: flaskapp
  328. flask_group: flaskapp
  329. homedir: /var/flaskapp/
  330. packages:
  331.   - git
  332.   - pip
  333.  
  334.  
  335.  
  336.   #### Playbook ####
  337.  
  338.  ec2-user@ip-172-31-40-58 project]$ cat flask.yml
  339. ---
  340.  
  341. - name: "Flask application deploy"
  342.   hosts: flaskapp
  343.   become: true
  344.   vars_files:
  345.     - flask-variable.yml
  346.   tasks:
  347.     - name: "Package installation"
  348.       yum:
  349.         name: "{{ packages }}"
  350.         state: present
  351.     - name: "Git clone to remote server"
  352.       git:
  353.         repo: "{{ git_url }}"
  354.         dest: /var/website/
  355.       register: git_status
  356.     - name: "Creating the homedirectory {{ homedir }}"
  357.       file:
  358.         path: "{{ homedir }}"
  359.         state: directory
  360.     - name: "Creating user {{ flask_owner }} for the flaskapplication"
  361.       user:
  362.         name: "{{ flask_owner }}"
  363.         shell: /usr/sbin/nologin
  364.         home: "{{ homedir }}"
  365.         uid: 1010
  366.     - name: "Uploading the contents to doccument root {{ homedir }}"
  367.       when: git_status.changed == true
  368.       copy:
  369.         src: /var/website/
  370.         dest: "{{ homedir }}"
  371.         owner: "{{ flask_owner }}"
  372.         group: "{{ flask_group }}"
  373.         remote_src: true
  374.     - name: "Installing required flaskapplication using pip3"
  375.       when: git_status.changed == true
  376.       pip:
  377.         requirements: "{{ homedir }}/requirements.txt"
  378.  
  379.     - name: "Copying the systemd file flask.service"
  380.       when: git_status.changed == true
  381.       copy:
  382.         src: "{{ homedir }}/flask.service"
  383.         dest: /etc/systemd/system/flask.service
  384.         remote_src: true
  385.     - name: "Systemd service restart and daemon-reload "
  386.       when: git_status.changed == true
  387.       systemd:
  388.         daemon_reload: true
  389.         name: flask
  390.         state: restarted
  391.         enabled: true
  392.        
  393.        
  394. #### Output ####
  395.  
  396. [ec2-user@ip-172-31-40-58 project]$ ansible-playbook -i inventory flask.yml
  397.  
  398. PLAY [Flask application deploy] ****************************************************************************************
  399.  
  400. TASK [Gathering Facts] *************************************************************************************************
  401. [WARNING]: Platform linux on host 172.31.40.200 is using the discovered Python interpreter at /usr/bin/python3.9, but
  402. future installation of another Python interpreter could change the meaning of that path. See
  403. https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
  404. ok: [172.31.40.200]
  405.  
  406. TASK [Package installation] ********************************************************************************************
  407. changed: [172.31.40.200]
  408.  
  409. TASK [Git clone to remote server] **************************************************************************************
  410. changed: [172.31.40.200]
  411.  
  412. TASK [Creating the homedirectory /var/flaskapp/] ***********************************************************************
  413. changed: [172.31.40.200]
  414.  
  415. TASK [Creating user flaskapp for the flaskapplication] *****************************************************************
  416. changed: [172.31.40.200]
  417.  
  418. TASK [Uploading the contents to doccument root /var/flaskapp/] *********************************************************
  419. changed: [172.31.40.200]
  420.  
  421. TASK [Installing required flaskapplication using pip3] *****************************************************************
  422. changed: [172.31.40.200]
  423.  
  424. TASK [Copying the systemd file flask.service] **************************************************************************
  425. changed: [172.31.40.200]
  426.  
  427. TASK [Systemd service restart and daemon-reload] ***********************************************************************
  428. changed: [172.31.40.200]
  429.  
  430. PLAY RECAP *************************************************************************************************************
  431. 172.31.40.200              : ok=9    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  432.  
  433.  
  434.  
  435. ### Output after adding a boto3 module in requirements.txt ###
  436.  
  437. [ec2-user@ip-172-31-40-58 project]$ ansible-playbook -i inventory flask.yml
  438.  
  439. PLAY [Flask application deploy] ****************************************************************************************
  440.  
  441. TASK [Gathering Facts] *************************************************************************************************
  442. [WARNING]: Platform linux on host 172.31.40.200 is using the discovered Python interpreter at /usr/bin/python3.9, but
  443. future installation of another Python interpreter could change the meaning of that path. See
  444. https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
  445. ok: [172.31.40.200]
  446.  
  447. TASK [Package installation] ********************************************************************************************
  448. ok: [172.31.40.200]
  449.  
  450. TASK [Git clone to remote server] **************************************************************************************
  451. changed: [172.31.40.200]
  452.  
  453. TASK [Creating the homedirectory /var/flaskapp/] ***********************************************************************
  454. ok: [172.31.40.200]
  455.  
  456. TASK [Creating user flaskapp for the flaskapplication] *****************************************************************
  457. ok: [172.31.40.200]
  458.  
  459. TASK [Uploading the contents to doccument root /var/flaskapp/] *********************************************************
  460. changed: [172.31.40.200]
  461.  
  462. TASK [Installing required flaskapplication using pip3] *****************************************************************
  463. changed: [172.31.40.200]
  464.  
  465. TASK [Copying the systemd file flask.service] **************************************************************************
  466. ok: [172.31.40.200]
  467.  
  468. TASK [Systemd service restart and daemon-reload] ***********************************************************************
  469. changed: [172.31.40.200]
  470.  
  471. PLAY RECAP *************************************************************************************************************
  472. 172.31.40.200              : ok=9    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479. ##### Verifying ####
  480.  
  481.  
  482. [ec2-user@ip-172-31-40-200 ~]$ ps aux | grep python3
  483. flaskapp   54428  0.2  2.9 479216 28764 ?        Ssl  14:12   0:00 /usr/bin/python3 app.py
  484. ec2-user   54538  0.0  0.2 221796  2180 pts/0    S+   14:13   0:00 grep --color=auto python3
  485.  
  486.  
  487.  
  488. [ec2-user@ip-172-31-40-200 ~]$ ls -l /var/flaskapp/
  489. total 16
  490. -rw-r--r--. 1 flaskapp flaskapp 242 Jun 23 14:08 app.py
  491. -rw-r--r--. 1 flaskapp flaskapp 241 Jun 23 14:08 flask.service
  492. -rw-r--r--. 1 flaskapp flaskapp 317 Jun 23 14:08 README.md
  493. -rw-r--r--. 1 flaskapp flaskapp  12 Jun 23 14:12 requirements.txt
  494. [ec2-user@ip-172-31-40-200 ~]$
  495.  
  496.  
  497. [ec2-user@ip-172-31-40-200 ~]$ grep flaskapp /etc/passwd
  498. flaskapp:x:1010:1010::/var/flaskapp/:/usr/sbin/nologin
  499.  
  500.  
  501. [ec2-user@ip-172-31-40-200 ~]$ cat /etc/systemd/system/flask.service
  502. [Unit]
  503. Description=Service to run flask application
  504. After=network.target
  505.  
  506. [Service]
  507. ExecStart=/usr/bin/python3 app.py
  508. WorkingDirectory=/var/flaskapp/
  509. User=flaskapp
  510. Group=flaskapp
  511. TimeoutStopSec=infinity
  512.  
  513. [Install]
  514. WantedBy=multi-user.target
  515.  
  516.  
Advertisement
Add Comment
Please, Sign In to add comment