Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------- Task 1-------------------------------------------
- #### Inventory ####
- [ec2-user@ip-172-31-40-58 ~]$ cat inventory
- [amazon]
- 172.31.37.63 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
- ### Converted the conf to template ###
- [ec2-user@ip-172-31-40-58 ~]$ grep ^Listen httpd.conf.tmpl
- Listen {{ httpd_port }}
- [ec2-user@ip-172-31-40-58 ~]$
- [ec2-user@ip-172-31-40-58 ~]$
- [ec2-user@ip-172-31-40-58 ~]$ cat virtual.conf.tmpl
- <virtualhost *:{{ httpd_port }}>
- ServerName {{ httpd_url }}
- DocumentRoot /var/www/html/{{ httpd_url }}/
- DirectoryIndex index.php index.html
- <Directory "/var/www/html/{{ httpd_url }}/">
- AllowOverride all
- </Directory>
- </virtualhost>
- #### Playbook without using git module ####
- ---
- - name: "Php website hosting using git clone from master machine"
- hosts: amazon
- become: true
- vars_files:
- - variables.yml
- tasks:
- - name: "Installing webserver packages"
- yum:
- name: "{{ packages }}"
- state: present
- register: webpackage_status
- - name: "Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf"
- template:
- src: ./httpd.conf.tmpl
- dest: /etc/httpd/conf/httpd.conf
- register: httpd_conf_status
- - name: "Copying the virtaulhost of {{ httpd_url }} to /etc/httpd/conf.d/{{ httpd_url }}"
- template:
- src: ./virtual.conf.tmpl
- dest: "/etc/httpd/conf.d/{{ httpd_url }}.conf"
- register: virtualhost_conf_status
- - name: "Creating the directory for the documentroot /var/www/html/{{ httpd_url }}"
- file:
- path: "/var/www/html/{{ httpd_url }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "Copying the website to the documentroot /var/www/html/{{ httpd_url }}/"
- copy:
- src: ./website/
- dest: "/var/www/html/{{ httpd_url }}/"
- register: copy_status
- - name: "Restart httpd if condition meets"
- when: webpackage_status.changed == true or httpd_conf_status.changed == true or virtualhost_conf_status.changed == true
- service:
- name: httpd
- state: restarted
- enabled: true
- - name: "Restart php-fpm whenc condition meets"
- when: webpackage_status.changed == true or copy_status.changed == true
- service:
- name: php-fpm
- state: restarted
- enabled: true
- #### vars_files ####
- [ec2-user@ip-172-31-40-58 ~]$ cat variables.yml
- ---
- httpd_port: 8080
- httpd_url: bigbasket.chrisich.fun
- httpd_owner: apache
- httpd_group: apache
- packages:
- - httpd
- - php
- ### Output when changed the port 80 to 8080 ####
- [ec2-user@ip-172-31-40-58 ~]$ ansible-playbook -i inventory php-app.yml
- PLAY [Php website hosting using git clone from master machine] *********************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- ok: [172.31.37.63]
- TASK [Installing webserver packages] ***********************************************************************************************************************
- ok: [172.31.37.63]
- TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
- changed: [172.31.37.63]
- TASK [Copying the virtaulhost of bigbasket.chrisich.fun to /etc/httpd/conf.d/bigbasket.chrisich.fun] *******************************************************
- changed: [172.31.37.63]
- TASK [Creating the directory for the documentroot /var/www/html/bigbasket.chrisich.fun] ********************************************************************
- ok: [172.31.37.63]
- TASK [Copying the website to the documentroot /var/www/html/bigbasket.chrisich.fun/] ***********************************************************************
- ok: [172.31.37.63]
- TASK [Restart httpd if condition meets] ********************************************************************************************************************
- changed: [172.31.37.63]
- TASK [Restart php-fpm whenc condition meets] ***************************************************************************************************************
- skipping: [172.31.37.63]
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.37.63 : ok=7 changed=3 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
- -------------------------------------------------------------- Task 2 -------------------------------------------
- #### git module playbook ####
- [ec2-user@ip-172-31-40-58 ~]$ cat variables.yml
- ---
- httpd_port: 80
- httpd_url: bigbasket.chrisich.fun
- git_url: https://github.com/Chris-luiz-16/aws-elb-site.git
- httpd_owner: apache
- httpd_group: apache
- packages:
- - httpd
- - php
- additional_packages:
- - git
- #### Playbook ####
- ---
- - name: "Php website hosting using git clone from master machine"
- hosts: amazon
- become: true
- vars_files:
- - variables.yml
- tasks:
- - name: "Installing webserver packages"
- yum:
- name: "{{ packages }}"
- state: present
- register: webpackage_status
- - name: "Installing additional packages"
- yum:
- name: "{{ additional_packages }}"
- state: present
- - name: "Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf"
- template:
- src: ./httpd.conf.tmpl
- dest: /etc/httpd/conf/httpd.conf
- register: httpd_conf_status
- - name: "Copying the virtaulhost of {{ httpd_url }} to /etc/httpd/conf.d/{{ httpd_url }}"
- template:
- src: ./virtual.conf.tmpl
- dest: "/etc/httpd/conf.d/{{ httpd_url }}.conf"
- register: virtualhost_conf_status
- - name: "Git clone to remote machine"
- git:
- repo: "{{ git_url }}"
- dest: /var/website/
- register: git_status
- - name: "Creating the directory for the documentroot /var/www/html/{{ httpd_url }}"
- file:
- path: "/var/www/html/{{ httpd_url }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "Copying the website to the documentroot /var/www/html/{{ httpd_url }}/"
- when: git_status.changed == true
- copy:
- src: /var/website/
- dest: "/var/www/html/{{ httpd_url }}/"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- remote_src: true
- register: copy_status
- - name: "Restart httpd if condition meets"
- when: webpackage_status.changed == true or httpd_conf_status.changed == true or virtualhost_conf_status.changed == true
- service:
- name: httpd
- state: restarted
- enabled: true
- - name: "Restart php-fpm whenc condition meets"
- when: webpackage_status.changed == true or git_status.changed == true
- service:
- name: php-fpm
- state: restarted
- enabled: true
- #### Output no change ####
- PLAY [Php website hosting using git clone from master machine] *********************************************************
- TASK [Gathering Facts] *************************************************************************************************
- [WARNING]: Platform linux on host 172.31.37.63 is using the discovered Python interpreter at /usr/bin/python3.9, but
- future installation of another Python interpreter could change the meaning of that path. See
- https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
- ok: [172.31.37.63]
- TASK [Installing webserver packages] ***********************************************************************************
- ok: [172.31.37.63]
- TASK [Installing additional packages] **********************************************************************************
- ok: [172.31.37.63]
- TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************
- ok: [172.31.37.63]
- TASK [Copying the virtaulhost of bigbasket.chrisich.fun to /etc/httpd/conf.d/bigbasket.chrisich.fun] *******************
- ok: [172.31.37.63]
- TASK [Git clone to remote machine] *************************************************************************************
- ok: [172.31.37.63]
- TASK [Creating the directory for the documentroot /var/www/html/bigbasket.chrisich.fun] ********************************
- ok: [172.31.37.63]
- TASK [Copying the website to the documentroot /var/www/html/bigbasket.chrisich.fun/] ***********************************
- skipping: [172.31.37.63]
- TASK [Restart httpd if condition meets] ********************************************************************************
- skipping: [172.31.37.63]
- TASK [Restart php-fpm whenc condition meets] ***************************************************************************
- skipping: [172.31.37.63]
- PLAY RECAP *************************************************************************************************************
- 172.31.37.63 : ok=7 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- #### Output version upgrade ####
- [ec2-user@ip-172-31-40-58 ~]$ ansible-playbook -i inventory php-app.yml
- PLAY [Php website hosting using git clone from master machine] *********************************************************
- TASK [Gathering Facts] *************************************************************************************************
- [WARNING]: Platform linux on host 172.31.37.63 is using the discovered Python interpreter at /usr/bin/python3.9, but
- future installation of another Python interpreter could change the meaning of that path. See
- https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
- ok: [172.31.37.63]
- TASK [Installing webserver packages] ***********************************************************************************
- ok: [172.31.37.63]
- TASK [Installing additional packages] **********************************************************************************
- ok: [172.31.37.63]
- TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************
- ok: [172.31.37.63]
- TASK [Copying the virtaulhost of bigbasket.chrisich.fun to /etc/httpd/conf.d/bigbasket.chrisich.fun] *******************
- ok: [172.31.37.63]
- TASK [Git clone to remote machine] *************************************************************************************
- changed: [172.31.37.63]
- TASK [Creating the directory for the documentroot /var/www/html/bigbasket.chrisich.fun] ********************************
- ok: [172.31.37.63]
- TASK [Copying the website to the documentroot /var/www/html/bigbasket.chrisich.fun/] ***********************************
- changed: [172.31.37.63]
- TASK [Restart httpd if condition meets] ********************************************************************************
- skipping: [172.31.37.63]
- TASK [Restart php-fpm whenc condition meets] ***************************************************************************
- changed: [172.31.37.63]
- PLAY RECAP *************************************************************************************************************
- 172.31.37.63 : ok=9 changed=3 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
- ---------------------------- Flask app additional task ------------------------------------------------
- #### variable ####
- [ec2-user@ip-172-31-40-58 project]$ cat flask-variable.yml
- ---
- git_url: https://github.com/Chris-luiz-16/Hello-world-flask-app-with-systemd-file.git
- flask_owner: flaskapp
- flask_group: flaskapp
- homedir: /var/flaskapp/
- packages:
- - git
- - pip
- #### Playbook ####
- ec2-user@ip-172-31-40-58 project]$ cat flask.yml
- ---
- - name: "Flask application deploy"
- hosts: flaskapp
- become: true
- vars_files:
- - flask-variable.yml
- tasks:
- - name: "Package installation"
- yum:
- name: "{{ packages }}"
- state: present
- - name: "Git clone to remote server"
- git:
- repo: "{{ git_url }}"
- dest: /var/website/
- register: git_status
- - name: "Creating the homedirectory {{ homedir }}"
- file:
- path: "{{ homedir }}"
- state: directory
- - name: "Creating user {{ flask_owner }} for the flaskapplication"
- user:
- name: "{{ flask_owner }}"
- shell: /usr/sbin/nologin
- home: "{{ homedir }}"
- uid: 1010
- - name: "Uploading the contents to doccument root {{ homedir }}"
- when: git_status.changed == true
- copy:
- src: /var/website/
- dest: "{{ homedir }}"
- owner: "{{ flask_owner }}"
- group: "{{ flask_group }}"
- remote_src: true
- - name: "Installing required flaskapplication using pip3"
- when: git_status.changed == true
- pip:
- requirements: "{{ homedir }}/requirements.txt"
- - name: "Copying the systemd file flask.service"
- when: git_status.changed == true
- copy:
- src: "{{ homedir }}/flask.service"
- dest: /etc/systemd/system/flask.service
- remote_src: true
- - name: "Systemd service restart and daemon-reload "
- when: git_status.changed == true
- systemd:
- daemon_reload: true
- name: flask
- state: restarted
- enabled: true
- #### Output ####
- [ec2-user@ip-172-31-40-58 project]$ ansible-playbook -i inventory flask.yml
- PLAY [Flask application deploy] ****************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- [WARNING]: Platform linux on host 172.31.40.200 is using the discovered Python interpreter at /usr/bin/python3.9, but
- future installation of another Python interpreter could change the meaning of that path. See
- https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
- ok: [172.31.40.200]
- TASK [Package installation] ********************************************************************************************
- changed: [172.31.40.200]
- TASK [Git clone to remote server] **************************************************************************************
- changed: [172.31.40.200]
- TASK [Creating the homedirectory /var/flaskapp/] ***********************************************************************
- changed: [172.31.40.200]
- TASK [Creating user flaskapp for the flaskapplication] *****************************************************************
- changed: [172.31.40.200]
- TASK [Uploading the contents to doccument root /var/flaskapp/] *********************************************************
- changed: [172.31.40.200]
- TASK [Installing required flaskapplication using pip3] *****************************************************************
- changed: [172.31.40.200]
- TASK [Copying the systemd file flask.service] **************************************************************************
- changed: [172.31.40.200]
- TASK [Systemd service restart and daemon-reload] ***********************************************************************
- changed: [172.31.40.200]
- PLAY RECAP *************************************************************************************************************
- 172.31.40.200 : ok=9 changed=8 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- ### Output after adding a boto3 module in requirements.txt ###
- [ec2-user@ip-172-31-40-58 project]$ ansible-playbook -i inventory flask.yml
- PLAY [Flask application deploy] ****************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- [WARNING]: Platform linux on host 172.31.40.200 is using the discovered Python interpreter at /usr/bin/python3.9, but
- future installation of another Python interpreter could change the meaning of that path. See
- https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
- ok: [172.31.40.200]
- TASK [Package installation] ********************************************************************************************
- ok: [172.31.40.200]
- TASK [Git clone to remote server] **************************************************************************************
- changed: [172.31.40.200]
- TASK [Creating the homedirectory /var/flaskapp/] ***********************************************************************
- ok: [172.31.40.200]
- TASK [Creating user flaskapp for the flaskapplication] *****************************************************************
- ok: [172.31.40.200]
- TASK [Uploading the contents to doccument root /var/flaskapp/] *********************************************************
- changed: [172.31.40.200]
- TASK [Installing required flaskapplication using pip3] *****************************************************************
- changed: [172.31.40.200]
- TASK [Copying the systemd file flask.service] **************************************************************************
- ok: [172.31.40.200]
- TASK [Systemd service restart and daemon-reload] ***********************************************************************
- changed: [172.31.40.200]
- PLAY RECAP *************************************************************************************************************
- 172.31.40.200 : ok=9 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- ##### Verifying ####
- [ec2-user@ip-172-31-40-200 ~]$ ps aux | grep python3
- flaskapp 54428 0.2 2.9 479216 28764 ? Ssl 14:12 0:00 /usr/bin/python3 app.py
- ec2-user 54538 0.0 0.2 221796 2180 pts/0 S+ 14:13 0:00 grep --color=auto python3
- [ec2-user@ip-172-31-40-200 ~]$ ls -l /var/flaskapp/
- total 16
- -rw-r--r--. 1 flaskapp flaskapp 242 Jun 23 14:08 app.py
- -rw-r--r--. 1 flaskapp flaskapp 241 Jun 23 14:08 flask.service
- -rw-r--r--. 1 flaskapp flaskapp 317 Jun 23 14:08 README.md
- -rw-r--r--. 1 flaskapp flaskapp 12 Jun 23 14:12 requirements.txt
- [ec2-user@ip-172-31-40-200 ~]$
- [ec2-user@ip-172-31-40-200 ~]$ grep flaskapp /etc/passwd
- flaskapp:x:1010:1010::/var/flaskapp/:/usr/sbin/nologin
- [ec2-user@ip-172-31-40-200 ~]$ cat /etc/systemd/system/flask.service
- [Unit]
- Description=Service to run flask application
- After=network.target
- [Service]
- ExecStart=/usr/bin/python3 app.py
- WorkingDirectory=/var/flaskapp/
- User=flaskapp
- Group=flaskapp
- TimeoutStopSec=infinity
- [Install]
- WantedBy=multi-user.target
Advertisement
Add Comment
Please, Sign In to add comment