├── README.md ├── Section_2_Web_Playbook ├── Exercise_1 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Exercise_10 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Exercise_2 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Exercise_3 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Exercise_4 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Exercise_5 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Exercise_6 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Exercise_7 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Exercise_8 │ ├── app.py │ ├── inventory │ └── playbook.yml └── Exercise_9 │ ├── app.py │ ├── inventory │ └── playbook.yml ├── Section_3_HostVars_Include ├── Exercise_1 │ ├── app.py │ ├── host_vars │ │ └── db_and_web_server.yml │ ├── inventory │ └── playbook.yml ├── Exercise_2 │ ├── app.py │ ├── host_vars │ │ └── db_and_web_server.yml │ ├── inventory │ └── playbook.yml ├── Exercise_3 │ ├── app.py │ ├── host_vars │ │ └── db_and_web_server.yml │ ├── inventory │ ├── playbook.yml │ └── tasks │ │ └── deploy_db.yml ├── Exercise_4 │ ├── app.py │ ├── host_vars │ │ └── db_and_web_server.yml │ ├── inventory │ ├── playbook.yml │ └── tasks │ │ └── deploy_db.yml └── Exercise_5 │ ├── app.py │ ├── host_vars │ └── db_and_web_server.yml │ ├── inventory │ ├── playbook.yml │ └── tasks │ ├── deploy_db.yml │ └── deploy_web.yml ├── Section_4_Roles ├── Exercise_1 │ ├── app.py │ ├── host_vars │ │ └── db_and_web_server.yml │ ├── inventory │ ├── playbook.yml │ └── roles │ │ └── mysql_db │ │ └── tasks │ │ └── main.yml ├── Exercise_2 │ ├── app.py │ ├── host_vars │ │ └── db_and_web_server.yml │ ├── inventory │ ├── playbook.yml │ └── roles │ │ ├── flask_web │ │ └── tasks │ │ │ └── main.yml │ │ └── mysql_db │ │ └── tasks │ │ └── main.yml ├── Exercise_3 │ ├── app.py │ ├── host_vars │ │ └── db_and_web_server.yml │ ├── inventory │ ├── playbook.yml │ └── roles │ │ ├── flask_web │ │ └── tasks │ │ │ └── main.yml │ │ ├── mysql_db │ │ └── tasks │ │ │ └── main.yml │ │ └── python │ │ └── tasks │ │ └── main.yml ├── Exercise_4 │ ├── app.py │ ├── host_vars │ │ └── db_and_web_server.yml │ ├── inventory │ ├── playbook.yml │ └── roles │ │ ├── flask_web │ │ └── tasks │ │ │ └── main.yml │ │ ├── mysql_db │ │ └── tasks │ │ │ └── main.yml │ │ └── python │ │ └── tasks │ │ └── main.yml ├── Exercise_5 │ ├── app.py │ ├── host_vars │ │ ├── db_server.yml │ │ └── web_server.yml │ ├── inventory │ ├── playbook.yml │ └── roles │ │ ├── flask_web │ │ └── tasks │ │ │ └── main.yml │ │ ├── mysql_db │ │ └── tasks │ │ │ └── main.yml │ │ └── python │ │ └── tasks │ │ └── main.yml └── Exercise_6 │ ├── app.py │ ├── host_vars │ ├── db_server.yml │ └── web_server.yml │ ├── inventory │ ├── playbook.yml │ └── roles │ ├── flask_web │ └── tasks │ │ └── main.yml │ ├── mysql_db │ └── tasks │ │ └── main.yml │ └── python │ └── tasks │ └── main.yml ├── Section_5_Async ├── Exercise_1 │ ├── inventory │ └── playbook.yml ├── Exercise_2 │ ├── inventory │ └── playbook.yml ├── Exercise_3 │ ├── inventory │ └── playbook.yml └── Exercise_4 │ ├── inventory │ └── playbook.yml ├── Section_6_Strategy ├── Exercise_1 │ ├── inventory │ └── playbook.yml ├── Exercise_2 │ ├── inventory │ └── playbook.yml ├── Exercise_3 │ ├── inventory │ └── playbook.yml ├── Exercise_4 │ ├── inventory │ └── playbook.yml └── Exercise_5 │ ├── inventory │ └── playbook.yml ├── Section_7_ErrorHandling ├── Exercise_1 │ ├── inventory │ └── playbook.yml └── Exercise_2 │ ├── inventory │ └── playbook.yml ├── Section_8_Jinja2 ├── Exercise_1 │ └── playbook.yml ├── Exercise_2 │ └── playbook.yml ├── Exercise_3 │ └── playbook.yml ├── Exercise_4 │ └── playbook.yml ├── Exercise_5 │ └── playbook.yml ├── Exercise_6 │ └── playbook.yml └── Exercise_7 │ └── playbook.yml └── Section_9_Lookups ├── Exercise_1 ├── credentials.csv ├── inventory └── playbook.yml └── Exercise_2 ├── credentials.ini ├── inventory └── playbook.yml /README.md: -------------------------------------------------------------------------------- 1 | # ansible-training-answer-keys-2 2 | 3 | This is the answer key for coding exercises on the Udemy Course - https://www.udemy.com/learn-ansible-advanced 4 | 5 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_1/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_1/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_1/playbook.yml: -------------------------------------------------------------------------------- 1 | In this section we will try to create a complete playbook using the knowledge we gained in the previous course 2 | "Ansible for the Absolute Beginners" to deploy a multi-tiered web application. 3 | We will start with deploying both web and database tiers on the same server called "db_and_web_server". Refer to the inventory file. 4 | In this exercise update the host on the play to work with our target server "db_and_web_server". 5 | # 6 | # ------------------------------ 7 | # BEFORE 8 | # ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: localhost 12 | # tasks: 13 | # - name: Install dependencies 14 | # ------------------------------ 15 | # AFTER 16 | # ------------------------------ 17 | - 18 | name: Deploy a web application 19 | hosts: db_and_web_server 20 | tasks: 21 | - name: Install dependencies 22 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_10/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_10/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server1 ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.6 2 | db_and_web_server2 ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.15 3 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_10/playbook.yml: -------------------------------------------------------------------------------- 1 | # Let us now improvise a little bit. Let us move some of the variable details out to a vars section at the top. 2 | # Add a vars section under the play and define db_name, db_user and db_password. Then replace the static definitions to use variable name 3 | ## 4 | # 5 | ## ------------------------------ 6 | ## BEFORE 7 | ## ------------------------------ 8 | #- 9 | # name: Deploy a web application 10 | # hosts: db_and_web_server 11 | # tasks: 12 | # - name: Install dependencies 13 | # apt: name={{ item }} state=present 14 | # with_items: 15 | # - python 16 | # - python-setuptools 17 | # - python-dev 18 | # - build-essential 19 | # - python-pip 20 | # - python-mysqldb 21 | # 22 | # - name: Install MySQL database 23 | # apt: 24 | # name: "{{ item }}" 25 | # state: present 26 | # with_items: 27 | # - mysql-server 28 | # - mysql-client 29 | # 30 | # - name: Start Mysql Service 31 | # service: 32 | # name: mysql 33 | # state: started 34 | # enabled: yes 35 | # 36 | # - name: Create Application Database 37 | # mysql_db: name='employee_db' state=present 38 | # 39 | # 40 | # - name: Create Application DB User 41 | # mysql_user: name='db_user' password='Passw0rd' priv='*.*:ALL' host='%' state='present' 42 | # 43 | # - name: Install Python Flask dependencies 44 | # pip: 45 | # name: '{{ item }}' 46 | # state: present 47 | # with_items: 48 | # - flask 49 | # - flask-mysql 50 | # 51 | # - name: Copy web-server code 52 | # copy: src=app.py dest=/opt/app.py 53 | # 54 | # - name: Start web-application 55 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 56 | 57 | # ------------------------------ 58 | # AFTER 59 | # ------------------------------ 60 | - 61 | name: Deploy a web application 62 | hosts: db_and_web_server 63 | vars: 64 | db_name: employee_db 65 | db_user: db_user 66 | db_password: Passw0rd 67 | tasks: 68 | - name: Install dependencies 69 | apt: name={{ item }} state=present 70 | with_items: 71 | - python 72 | - python-setuptools 73 | - python-dev 74 | - build-essential 75 | - python-pip 76 | - python-mysqldb 77 | 78 | - name: Install MySQL database 79 | apt: 80 | name: "{{ item }}" 81 | state: present 82 | with_items: 83 | - mysql-server 84 | - mysql-client 85 | 86 | - name: Start Mysql Service 87 | service: 88 | name: mysql 89 | state: started 90 | enabled: yes 91 | 92 | - name: Create Application Database 93 | mysql_db: name={{ db_name }} state=present 94 | 95 | - name: Create Application DB User 96 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 97 | 98 | - name: Install Python Flask dependencies 99 | pip: 100 | name: '{{ item }}' 101 | state: present 102 | with_items: 103 | - flask 104 | - flask-mysql 105 | 106 | - name: Copy web-server code 107 | copy: src=app.py dest=/opt/app.py 108 | 109 | - name: Start web-application 110 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 111 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_2/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_2/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_2/playbook.yml: -------------------------------------------------------------------------------- 1 | # In the given playbook the first task is incomplete. Only a name is given. Update the task to use the "apt" 2 | # module to install all required packages. The list of packages to be present are given below. Remember to use loop (with_items) to install all packages in one task: 3 | # - python 4 | # - python-setuptools 5 | # - python-dev 6 | # - build-essential 7 | # - python-pip 8 | # - python-mysqldb 9 | # apt documentation - http://docs.ansible.com/ansible/latest/apt_module.html 10 | 11 | # ------------------------------ 12 | # BEFORE 13 | # ------------------------------ 14 | #- 15 | # name: Deploy a web application 16 | # hosts: db_and_web_server 17 | # tasks: 18 | # - name: Install dependencies 19 | # ------------------------------ 20 | # AFTER 21 | # ------------------------------ 22 | - 23 | name: Deploy a web application 24 | hosts: db_and_web_server 25 | tasks: 26 | - name: Install dependencies 27 | apt: name='{{ item }}' state=present 28 | with_items: 29 | - python 30 | - python-setuptools 31 | - python-dev 32 | - build-essential 33 | - python-pip 34 | - python-mysqldb 35 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_3/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_3/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_3/playbook.yml: -------------------------------------------------------------------------------- 1 | # Now let us add a new task to the play to install MySQL database 2 | # Create a new task similar to the first one and use apt to install two more packages: 3 | # - mysql-server 4 | # - mysql-client 5 | # apt documentation - http://docs.ansible.com/ansible/latest/apt_module.html 6 | # 7 | # ------------------------------ 8 | # BEFORE 9 | # ------------------------------ 10 | #- 11 | # name: Deploy a web application 12 | # hosts: db_and_web_server 13 | # tasks: 14 | # - name: Install dependencies 15 | # apt: name={{ item }} state=present 16 | # with_items: 17 | # - python 18 | # - python-setuptools 19 | # - python-dev 20 | # - build-essential 21 | # - python-pip 22 | # - python-mysqldb 23 | # ------------------------------ 24 | # AFTER 25 | # ------------------------------ 26 | - 27 | name: Deploy a web application 28 | hosts: db_and_web_server 29 | tasks: 30 | - name: Install dependencies 31 | apt: name={{ item }} state=present 32 | with_items: 33 | - python 34 | - python-setuptools 35 | - python-dev 36 | - build-essential 37 | - python-pip 38 | - python-mysqldb 39 | 40 | - name: Install MySQL database 41 | apt: name='{{ item }}' state=present 42 | with_items: 43 | - mysql-server 44 | - mysql-client 45 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_4/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_4/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_4/playbook.yml: -------------------------------------------------------------------------------- 1 | # Now let us add a new task to start the mysql service 2 | # Create a new task and use "service" module to start the "mysql" service. 3 | # Ensure you set enabled to true so that the service starts automatically everytime the server restarts 4 | # service module documentation - http://docs.ansible.com/ansible/latest/service_module.html 5 | # 6 | ## ------------------------------ 7 | ## BEFORE 8 | ## ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: db_and_web_server 12 | # tasks: 13 | # - name: Install dependencies 14 | # apt: name={{ item }} state=present 15 | # with_items: 16 | # - python 17 | # - python-setuptools 18 | # - python-dev 19 | # - build-essential 20 | # - python-pip 21 | # - python-mysqldb 22 | # 23 | # - name: Install MySQL database 24 | # apt: 25 | # name: "{{ item }}" 26 | # state: present 27 | # with_items: 28 | # - mysql-server 29 | # - mysql-client 30 | # ------------------------------ 31 | # AFTER 32 | # ------------------------------ 33 | - 34 | name: Deploy a web application 35 | hosts: db_and_web_server 36 | tasks: 37 | - name: Install dependencies 38 | apt: name={{ item }} state=present 39 | with_items: 40 | - python 41 | - python-setuptools 42 | - python-dev 43 | - build-essential 44 | - python-pip 45 | - python-mysqldb 46 | 47 | - name: Install MySQL database 48 | apt: 49 | name: "{{ item }}" 50 | state: present 51 | with_items: 52 | - mysql-server 53 | - mysql-client 54 | 55 | - name: Start Mysql Service 56 | service: 57 | name: mysql 58 | state: started 59 | enabled: yes 60 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_5/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_5/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_5/playbook.yml: -------------------------------------------------------------------------------- 1 | # Now let us create application database on the sql server. 2 | # Create a new task and use "mysql_db" module to create a new database by the name "employee_db". 3 | # Refer to mysql_db module documentation - http://docs.ansible.com/ansible/latest/mysql_db_module.html 4 | # 5 | ## ------------------------------ 6 | ## BEFORE 7 | ## ------------------------------ 8 | #- 9 | # name: Deploy a web application 10 | # hosts: db_and_web_server 11 | # tasks: 12 | # - name: Install dependencies 13 | # apt: name={{ item }} state=present 14 | # with_items: 15 | # - python 16 | # - python-setuptools 17 | # - python-dev 18 | # - build-essential 19 | # - python-pip 20 | # - python-mysqldb 21 | # 22 | # - name: Install MySQL database 23 | # apt: 24 | # name: "{{ item }}" 25 | # state: present 26 | # with_items: 27 | # - mysql-server 28 | # - mysql-client 29 | # 30 | # - name: Start Mysql Service 31 | # service: 32 | # name: mysql 33 | # state: started 34 | # enabled: yes 35 | # ------------------------------ 36 | # AFTER 37 | # ------------------------------ 38 | - 39 | name: Deploy a web application 40 | hosts: db_and_web_server 41 | tasks: 42 | - name: Install dependencies 43 | apt: name={{ item }} state=present 44 | with_items: 45 | - python 46 | - python-setuptools 47 | - python-dev 48 | - build-essential 49 | - python-pip 50 | - python-mysqldb 51 | 52 | - name: Install MySQL database 53 | apt: 54 | name: "{{ item }}" 55 | state: present 56 | with_items: 57 | - mysql-server 58 | - mysql-client 59 | 60 | - name: Start Mysql Service 61 | service: 62 | name: mysql 63 | state: started 64 | enabled: yes 65 | 66 | - name: Create Application Database 67 | mysql_db: 68 | name: employee_db 69 | state: present 70 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_6/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_6/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_6/playbook.yml: -------------------------------------------------------------------------------- 1 | # Now let us create application database user on the sql server. 2 | # Create a new task and use "mysql_user" module to create a new user. Use the details below: 3 | # user name: db_user 4 | # user password: Passw0rd 5 | # user privilege: *.*:ALL 6 | # host: '%' 7 | # Refer to mysql_user module documentation - http://docs.ansible.com/ansible/latest/mysql_user_module.html 8 | # 9 | ## ------------------------------ 10 | ## BEFORE 11 | ## ------------------------------ 12 | #- 13 | # name: Deploy a web application 14 | # hosts: db_and_web_server 15 | # tasks: 16 | # - name: Install dependencies 17 | # apt: name={{ item }} state=present 18 | # with_items: 19 | # - python 20 | # - python-setuptools 21 | # - python-dev 22 | # - build-essential 23 | # - python-pip 24 | # - python-mysqldb 25 | # 26 | # - name: Install MySQL database 27 | # apt: 28 | # name: "{{ item }}" 29 | # state: present 30 | # with_items: 31 | # - mysql-server 32 | # - mysql-client 33 | # 34 | # - name: Start Mysql Service 35 | # service: 36 | # name: mysql 37 | # state: started 38 | # enabled: yes 39 | # 40 | # - name: Create Application Database 41 | # mysql_db: name='employee_db' state=present 42 | # ------------------------------ 43 | # AFTER 44 | # ------------------------------ 45 | - 46 | name: Deploy a web application 47 | hosts: db_and_web_server 48 | tasks: 49 | - name: Install dependencies 50 | apt: name={{ item }} state=present 51 | with_items: 52 | - python 53 | - python-setuptools 54 | - python-dev 55 | - build-essential 56 | - python-pip 57 | - python-mysqldb 58 | 59 | - name: Install MySQL database 60 | apt: 61 | name: "{{ item }}" 62 | state: present 63 | with_items: 64 | - mysql-server 65 | - mysql-client 66 | 67 | - name: Start Mysql Service 68 | service: 69 | name: mysql 70 | state: started 71 | enabled: yes 72 | 73 | - name: Create Application Database 74 | mysql_db: name='employee_db' state=present 75 | 76 | - name: Create Application DB User 77 | mysql_user: name='db_user' password='Passw0rd' priv='*.*:ALL' host='%' state='present' 78 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_7/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_7/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_7/playbook.yml: -------------------------------------------------------------------------------- 1 | # Now let us install python dependencies for the Flask Application 2 | # Create a new task and use "pip" module to install python dependencies. Remember to use loop with with_items to install below dependencies: 3 | # - flask 4 | # - flask-mysql 5 | # 6 | # Refer to pip module documentation - http://docs.ansible.com/ansible/latest/pip_module.html 7 | # 8 | ## ------------------------------ 9 | ## BEFORE 10 | ## ------------------------------ 11 | #- 12 | # name: Deploy a web application 13 | # hosts: db_and_web_server 14 | # tasks: 15 | # - name: Install dependencies 16 | # apt: name={{ item }} state=present 17 | # with_items: 18 | # - python 19 | # - python-setuptools 20 | # - python-dev 21 | # - build-essential 22 | # - python-pip 23 | # - python-mysqldb 24 | # 25 | # - name: Install MySQL database 26 | # apt: 27 | # name: "{{ item }}" 28 | # state: present 29 | # with_items: 30 | # - mysql-server 31 | # - mysql-client 32 | # 33 | # - name: Start Mysql Service 34 | # service: 35 | # name: mysql 36 | # state: started 37 | # enabled: yes 38 | # 39 | # - name: Create Application Database 40 | # mysql_db: name='employee_db' state=present 41 | # 42 | # 43 | # - name: Create Application DB User 44 | # mysql_user: name='db_user' password='Passw0rd' priv='*.*:ALL' host='%' state='present' 45 | 46 | # ------------------------------ 47 | # AFTER 48 | # ------------------------------ 49 | - 50 | name: Deploy a web application 51 | hosts: db_and_web_server 52 | tasks: 53 | - name: Install dependencies 54 | apt: name={{ item }} state=present 55 | with_items: 56 | - python 57 | - python-setuptools 58 | - python-dev 59 | - build-essential 60 | - python-pip 61 | - python-mysqldb 62 | 63 | - name: Install MySQL database 64 | apt: 65 | name: "{{ item }}" 66 | state: present 67 | with_items: 68 | - mysql-server 69 | - mysql-client 70 | 71 | - name: Start Mysql Service 72 | service: 73 | name: mysql 74 | state: started 75 | enabled: yes 76 | 77 | - name: Create Application Database 78 | mysql_db: name='employee_db' state=present 79 | 80 | - name: Create Application DB User 81 | mysql_user: name='db_user' password='Passw0rd' priv='*.*:ALL' host='%' state='present' 82 | 83 | - name: Install Python Flask dependencies 84 | pip: 85 | name: '{{ item }}' 86 | state: present 87 | with_items: 88 | - flask 89 | - flask-mysql 90 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_8/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_8/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_8/playbook.yml: -------------------------------------------------------------------------------- 1 | # Now we copy our application code. As you can see our application code is located right next to our playbook. 2 | # Feel free to explore it, but do not modify it. 3 | # Use the 'copy' module to copy the application code (app.py) to the location - /opt/app.py on the target server 4 | # 5 | # Refer to pip module documentation - http://docs.ansible.com/ansible/latest/copy_module.html 6 | # 7 | ## ------------------------------ 8 | ## BEFORE 9 | ## ------------------------------ 10 | #- 11 | # name: Deploy a web application 12 | # hosts: db_and_web_server 13 | # tasks: 14 | # - name: Install dependencies 15 | # apt: name={{ item }} state=present 16 | # with_items: 17 | # - python 18 | # - python-setuptools 19 | # - python-dev 20 | # - build-essential 21 | # - python-pip 22 | # - python-mysqldb 23 | # 24 | # - name: Install MySQL database 25 | # apt: 26 | # name: "{{ item }}" 27 | # state: present 28 | # with_items: 29 | # - mysql-server 30 | # - mysql-client 31 | # 32 | # - name: Start Mysql Service 33 | # service: 34 | # name: mysql 35 | # state: started 36 | # enabled: yes 37 | # 38 | # - name: Create Application Database 39 | # mysql_db: name='employee_db' state=present 40 | # 41 | # 42 | # - name: Create Application DB User 43 | # mysql_user: name='db_user' password='Passw0rd' priv='*.*:ALL' host='%' state='present' 44 | # 45 | # - name: Install Python Flask dependencies 46 | # pip: 47 | # name: '{{ item }}' 48 | # state: present 49 | # with_items: 50 | # - flask 51 | # - flask-mysql 52 | 53 | # ------------------------------ 54 | # AFTER 55 | # ------------------------------ 56 | - 57 | name: Deploy a web application 58 | hosts: db_and_web_server 59 | tasks: 60 | - name: Install dependencies 61 | apt: name={{ item }} state=present 62 | with_items: 63 | - python 64 | - python-setuptools 65 | - python-dev 66 | - build-essential 67 | - python-pip 68 | - python-mysqldb 69 | 70 | - name: Install MySQL database 71 | apt: 72 | name: "{{ item }}" 73 | state: present 74 | with_items: 75 | - mysql-server 76 | - mysql-client 77 | 78 | - name: Start Mysql Service 79 | service: 80 | name: mysql 81 | state: started 82 | enabled: yes 83 | 84 | - name: Create Application Database 85 | mysql_db: name='employee_db' state=present 86 | 87 | - name: Create Application DB User 88 | mysql_user: name='db_user' password='Passw0rd' priv='*.*:ALL' host='%' state='present' 89 | 90 | - name: Install Python Flask dependencies 91 | pip: 92 | name: '{{ item }}' 93 | state: present 94 | with_items: 95 | - flask 96 | - flask-mysql 97 | 98 | - name: Copy web-server code 99 | copy: src=app.py dest=/opt/app.py 100 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_9/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_9/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server ansible_connection=ssh ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 2 | -------------------------------------------------------------------------------- /Section_2_Web_Playbook/Exercise_9/playbook.yml: -------------------------------------------------------------------------------- 1 | # Let us now start the web application using the below command: 2 | # FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 3 | # Use the 'shell' module to execute the above command on the target host 4 | # 5 | # Refer to shell module documentation - http://docs.ansible.com/ansible/latest/shell_module.html 6 | # 7 | ## ------------------------------ 8 | ## BEFORE 9 | ## ------------------------------ 10 | #- 11 | # name: Deploy a web application 12 | # hosts: db_and_web_server 13 | # tasks: 14 | # - name: Install dependencies 15 | # apt: name={{ item }} state=present 16 | # with_items: 17 | # - python 18 | # - python-setuptools 19 | # - python-dev 20 | # - build-essential 21 | # - python-pip 22 | # - python-mysqldb 23 | # 24 | # - name: Install MySQL database 25 | # apt: 26 | # name: "{{ item }}" 27 | # state: present 28 | # with_items: 29 | # - mysql-server 30 | # - mysql-client 31 | # 32 | # - name: Start Mysql Service 33 | # service: 34 | # name: mysql 35 | # state: started 36 | # enabled: yes 37 | # 38 | # - name: Create Application Database 39 | # mysql_db: name='employee_db' state=present 40 | # 41 | # - name: Create Application DB User 42 | # mysql_user: name='db_user' password='Passw0rd' priv='*.*:ALL' host='%' state='present' 43 | # 44 | # - name: Install Python Flask dependencies 45 | # pip: 46 | # name: '{{ item }}' 47 | # state: present 48 | # with_items: 49 | # - flask 50 | # - flask-mysql 51 | # 52 | # - name: Copy web-server code 53 | # copy: src=app.py dest=/opt/app.py 54 | 55 | # ------------------------------ 56 | # AFTER 57 | # ------------------------------ 58 | - 59 | name: Deploy a web application 60 | hosts: db_and_web_server 61 | tasks: 62 | - name: Install dependencies 63 | apt: name={{ item }} state=present 64 | with_items: 65 | - python 66 | - python-setuptools 67 | - python-dev 68 | - build-essential 69 | - python-pip 70 | - python-mysqldb 71 | 72 | - name: Install MySQL database 73 | apt: 74 | name: "{{ item }}" 75 | state: present 76 | with_items: 77 | - mysql-server 78 | - mysql-client 79 | 80 | - name: Start Mysql Service 81 | service: 82 | name: mysql 83 | state: started 84 | enabled: yes 85 | 86 | - name: Create Application Database 87 | mysql_db: name='employee_db' state=present 88 | 89 | - name: Create Application DB User 90 | mysql_user: name='db_user' password='Passw0rd' priv='*.*:ALL' host='%' state='present' 91 | 92 | - name: Install Python Flask dependencies 93 | pip: 94 | name: '{{ item }}' 95 | state: present 96 | with_items: 97 | - flask 98 | - flask-mysql 99 | 100 | - name: Copy web-server code 101 | copy: src=app.py dest=/opt/app.py 102 | 103 | - name: Start web-application 104 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 105 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_1/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_1/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | # Variable file for host db_and_web_server 2 | 3 | # ------------------------------ 4 | # BEFORE 5 | # ------------------------------ 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | ansible_ssh_pass: Passw0rd 12 | ansible_host: 192.168.1.14 13 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_1/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server ansible_ssh_pass=Passw0rd ansible_host=192.168.1.14 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_and_web_server -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_1/playbook.yml: -------------------------------------------------------------------------------- 1 | # Let us improvise our playbook a little bit more. 2 | # We have created a folder called host_vars and created a file under it called 'db_and_web_server.yml' 3 | # Move the connection information such as the IP and password to the new file and remove it from the inventory file 4 | ## 5 | # 6 | ## ------------------------------ 7 | ## BEFORE 8 | ## ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: db_and_web_server 12 | # vars: 13 | # db_name: employee_db 14 | # db_user: db_user 15 | # db_password: Passw0rd 16 | # tasks: 17 | # - name: Install dependencies 18 | # apt: name={{ item }} state=installed 19 | # with_items: 20 | # - python 21 | # - python-setuptools 22 | # - python-dev 23 | # - build-essential 24 | # - python-pip 25 | # - python-mysqldb 26 | # 27 | # - name: Install MySQL database 28 | # apt: 29 | # name: "{{ item }}" 30 | # state: installed 31 | # with_items: 32 | # - mysql-server 33 | # - mysql-client 34 | # 35 | # - name: Start Mysql Service 36 | # service: 37 | # name: mysql 38 | # state: started 39 | # enabled: yes 40 | # 41 | # - name: Create Application Database 42 | # mysql_db: name={{ db_name }} state=present 43 | # 44 | # 45 | # - name: Create Application DB User 46 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 47 | # 48 | # - name: Install Python Flask dependencies 49 | # pip: 50 | # name: '{{ item }}' 51 | # state: present 52 | # with_items: 53 | # - flask 54 | # - flask-mysql 55 | # 56 | # - name: Copy web-server code 57 | # copy: src=app.py dest=/opt/app.py 58 | # 59 | # - name: Start web-application 60 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 61 | 62 | # ------------------------------ 63 | # AFTER 64 | # ------------------------------ 65 | - 66 | name: Deploy a web application 67 | hosts: db_and_web_server 68 | vars: 69 | db_name: employee_db 70 | db_user: db_user 71 | db_password: Passw0rd 72 | tasks: 73 | - name: Install dependencies 74 | apt: name={{ item }} state=installed 75 | with_items: 76 | - python 77 | - python-setuptools 78 | - python-dev 79 | - build-essential 80 | - python-pip 81 | - python-mysqldb 82 | 83 | - name: Install MySQL database 84 | apt: 85 | name: "{{ item }}" 86 | state: installed 87 | with_items: 88 | - mysql-server 89 | - mysql-client 90 | 91 | - name: Start Mysql Service 92 | service: 93 | name: mysql 94 | state: started 95 | enabled: yes 96 | 97 | - name: Create Application Database 98 | mysql_db: name={{ db_name }} state=present 99 | 100 | - name: Create Application DB User 101 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 102 | 103 | - name: Install Python Flask dependencies 104 | pip: 105 | name: '{{ item }}' 106 | state: present 107 | with_items: 108 | - flask 109 | - flask-mysql 110 | 111 | - name: Copy web-server code 112 | copy: src=app.py dest=/opt/app.py 113 | 114 | - name: Start web-application 115 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 116 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_2/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_2/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | # Variable file for host db_and_web_server 2 | 3 | # ------------------------------ 4 | # BEFORE 5 | # ------------------------------ 6 | #ansible_ssh_pass: Passw0rd 7 | #ansible_host: 192.168.1.14 8 | # ------------------------------ 9 | # AFTER 10 | # ------------------------------ 11 | 12 | ansible_ssh_pass: Passw0rd 13 | ansible_host: 192.168.1.14 14 | db_name: employee_db 15 | db_user: db_user 16 | db_password: Passw0rd 17 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_2/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_and_web_server -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_2/playbook.yml: -------------------------------------------------------------------------------- 1 | # Now let us move the database parameters as well to the host_vars file. 2 | # 3 | 4 | # ------------------------------ 5 | # BEFORE 6 | # ------------------------------ 7 | #- 8 | # name: Deploy a web application 9 | # hosts: db_and_web_server 10 | # vars: 11 | # db_name: employee_db 12 | # db_user: db_user 13 | # db_password: Passw0rd 14 | # tasks: 15 | # - name: Install dependencies 16 | # apt: name={{ item }} state=installed 17 | # with_items: 18 | # - python 19 | # - python-setuptools 20 | # - python-dev 21 | # - build-essential 22 | # - python-pip 23 | # - python-mysqldb 24 | # 25 | # - name: Install MySQL database 26 | # apt: 27 | # name: "{{ item }}" 28 | # state: installed 29 | # with_items: 30 | # - mysql-server 31 | # - mysql-client 32 | # 33 | # - name: Start Mysql Service 34 | # service: 35 | # name: mysql 36 | # state: started 37 | # enabled: yes 38 | # 39 | # - name: Create Application Database 40 | # mysql_db: name={{ db_name }} state=present 41 | # 42 | # - name: Create Application Database 43 | # mysql_db: name={{ db_name }} state=present 44 | # 45 | # - name: Create Application DB User 46 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 47 | # 48 | # - name: Install Python Flask dependencies 49 | # pip: 50 | # name: '{{ item }}' 51 | # state: present 52 | # with_items: 53 | # - flask 54 | # - flask-mysql 55 | # 56 | # - name: Copy web-server code 57 | # copy: src=app.py dest=/opt/app.py 58 | # 59 | # - name: Start web-application 60 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 61 | 62 | # ------------------------------ 63 | # AFTER 64 | # ------------------------------ 65 | - 66 | name: Deploy a web application 67 | hosts: db_and_web_server 68 | tasks: 69 | - name: Install dependencies 70 | apt: name={{ item }} state=installed 71 | with_items: 72 | - python 73 | - python-setuptools 74 | - python-dev 75 | - build-essential 76 | - python-pip 77 | - python-mysqldb 78 | 79 | - name: Install MySQL database 80 | apt: 81 | name: "{{ item }}" 82 | state: installed 83 | with_items: 84 | - mysql-server 85 | - mysql-client 86 | 87 | - name: Start Mysql Service 88 | service: 89 | name: mysql 90 | state: started 91 | enabled: yes 92 | 93 | - name: Create Application Database 94 | mysql_db: name={{ db_name }} state=present 95 | 96 | - name: Create Application DB User 97 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 98 | 99 | - name: Install Python Flask dependencies 100 | pip: 101 | name: '{{ item }}' 102 | state: present 103 | with_items: 104 | - flask 105 | - flask-mysql 106 | 107 | - name: Copy web-server code 108 | copy: src=app.py dest=/opt/app.py 109 | 110 | - name: Start web-application 111 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 112 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_3/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_3/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | # Variable file for host db_and_web_server 2 | 3 | # ------------------------------ 4 | # BEFORE 5 | # ------------------------------ 6 | #ansible_ssh_pass: Passw0rd 7 | #ansible_host: 192.168.1.14 8 | #db_name: employee_db 9 | #db_user: db_user 10 | #db_password: Passw0rd 11 | # ------------------------------ 12 | # AFTER 13 | # ------------------------------ 14 | 15 | ansible_ssh_pass: Passw0rd 16 | ansible_host: 192.168.1.14 17 | db_name: employee_db 18 | db_user: db_user 19 | db_password: Passw0rd 20 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_3/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_3/playbook.yml: -------------------------------------------------------------------------------- 1 | # We will now separate the tasks into different files. I have now created a folder called "tasks" 2 | # and created two files under it called "deploy_db.yml" and "deploy_web.yml". 3 | # We will now move the tasks from the main playbook into respective tasks files - deploy_db.yml and deploy_web.yml 4 | # First, move (simply cut and paste) the below tasks into deploy_db.yml file 5 | # - Install MySQL database 6 | # - Start Mysql Service 7 | # - Create Application Database 8 | # - Create Application DB User 9 | # 10 | # Note, we will leave Install Dependencies there, since it is not specific to database or web. 11 | # We will add the include statement in the next Exercise 12 | # 13 | ## ------------------------------ 14 | ## BEFORE 15 | ## ------------------------------ 16 | #- 17 | # name: Deploy a web application 18 | # hosts: db_and_web_server 19 | # tasks: 20 | # - name: Install dependencies 21 | # apt: name={{ item }} state=installed 22 | # with_items: 23 | # - python 24 | # - python-setuptools 25 | # - python-dev 26 | # - build-essential 27 | # - python-pip 28 | # - python-mysqldb 29 | # 30 | # - name: Install MySQL database 31 | # apt: 32 | # name: "{{ item }}" 33 | # state: installed 34 | # with_items: 35 | # - mysql-server 36 | # - mysql-client 37 | # 38 | # - name: Start Mysql Service 39 | # service: 40 | # name: mysql 41 | # state: started 42 | # enabled: yes 43 | # 44 | # - name: Create Application Database 45 | # mysql_db: name={{ db_name }} state=present 46 | # 47 | # 48 | # - name: Create Application DB User 49 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 50 | # 51 | # - name: Install Python Flask dependencies 52 | # pip: 53 | # name: '{{ item }}' 54 | # state: present 55 | # with_items: 56 | # - flask 57 | # - flask-mysql 58 | # 59 | # - name: Copy web-server code 60 | # copy: src=app.py dest=/opt/app.py 61 | # 62 | # - name: Start web-application 63 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 64 | 65 | # ------------------------------ 66 | # AFTER 67 | # ------------------------------ 68 | - 69 | name: Deploy a web application 70 | hosts: db_and_web_server 71 | tasks: 72 | - name: Install dependencies 73 | apt: name={{ item }} state=installed 74 | with_items: 75 | - python 76 | - python-setuptools 77 | - python-dev 78 | - build-essential 79 | - python-pip 80 | - python-mysqldb 81 | 82 | - name: Install Python Flask dependencies 83 | pip: 84 | name: '{{ item }}' 85 | state: present 86 | with_items: 87 | - flask 88 | - flask-mysql 89 | 90 | - name: Copy web-server code 91 | copy: src=app.py dest=/opt/app.py 92 | 93 | - name: Start web-application 94 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 95 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_3/tasks/deploy_db.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: installed 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_4/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_4/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | ansible_ssh_pass: Passw0rd 2 | ansible_host: 192.168.1.14 3 | db_name: employee_db 4 | db_user: db_user 5 | db_password: Passw0rd 6 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_4/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_4/playbook.yml: -------------------------------------------------------------------------------- 1 | # Let us now add an include statement in the playbook to include the tasks into this playbook. 2 | # After the first task - "Install dependencies" and before the current second task "Install Python Flask dependencies" add include statement to include the file "tasks/deploy_db.yml" 3 | # 4 | 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Deploy a web application 10 | # hosts: db_and_web_server 11 | # tasks: 12 | # - name: Install dependencies 13 | # apt: name={{ item }} state=installed 14 | # with_items: 15 | # - python 16 | # - python-setuptools 17 | # - python-dev 18 | # - build-essential 19 | # - python-pip 20 | # - python-mysqldb 21 | # 22 | # - name: Install Python Flask dependencies 23 | # pip: 24 | # name: '{{ item }}' 25 | # state: present 26 | # with_items: 27 | # - flask 28 | # - flask-mysql 29 | # 30 | # - name: Copy web-server code 31 | # copy: src=app.py dest=/opt/app.py 32 | # 33 | # - name: Start web-application 34 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 35 | 36 | # ------------------------------ 37 | # AFTER 38 | # ------------------------------ 39 | - 40 | name: Deploy a web application 41 | hosts: db_and_web_server 42 | tasks: 43 | - name: Install dependencies 44 | apt: name={{ item }} state=installed 45 | with_items: 46 | - python 47 | - python-setuptools 48 | - python-dev 49 | - build-essential 50 | - python-pip 51 | - python-mysqldb 52 | 53 | - include: ./tasks/deploy_db.yml 54 | 55 | - name: Install Python Flask dependencies 56 | pip: 57 | name: '{{ item }}' 58 | state: present 59 | with_items: 60 | - flask 61 | - flask-mysql 62 | 63 | - name: Copy web-server code 64 | copy: src=app.py dest=/opt/app.py 65 | 66 | - name: Start web-application 67 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 68 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_4/tasks/deploy_db.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: installed 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_5/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_5/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | # Variable file for host db_and_web_server 2 | ansible_ssh_pass: Passw0rd 3 | ansible_host: 192.168.1.14 4 | db_name: employee_db 5 | db_user: db_user 6 | db_password: Passw0rd 7 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_5/inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_5/playbook.yml: -------------------------------------------------------------------------------- 1 | # Do the same for all the tasks under web application deployment. 2 | # A new file is created under tasks folder named - deploy_web.yml. 3 | # Move the below tasks to the new file and add include statement in the main playbook to include the file "tasks/deploy_web.yml" 4 | ## 5 | # 6 | ## ------------------------------ 7 | ## BEFORE 8 | ## ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: db_and_web_server 12 | # tasks: 13 | # - name: Install dependencies 14 | # apt: name={{ item }} state=installed 15 | # with_items: 16 | # - python 17 | # - python-setuptools 18 | # - python-dev 19 | # - build-essential 20 | # - python-pip 21 | # - python-mysqldb 22 | # 23 | # - include: tasks/deploy_db.yml 24 | # 25 | # - name: Install Python Flask dependencies 26 | # pip: 27 | # name: '{{ item }}' 28 | # state: present 29 | # with_items: 30 | # - flask 31 | # - flask-mysql 32 | # 33 | # - name: Copy web-server code 34 | # copy: src=app.py dest=/opt/app.py 35 | # 36 | # - name: Start web-application 37 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 38 | 39 | # ------------------------------ 40 | # AFTER 41 | # ------------------------------ 42 | - 43 | name: Deploy a web application 44 | hosts: db_and_web_server 45 | tasks: 46 | - name: Install dependencies 47 | apt: name={{ item }} state=installed 48 | with_items: 49 | - python 50 | - python-setuptools 51 | - python-dev 52 | - build-essential 53 | - python-pip 54 | - python-mysqldb 55 | 56 | - include: tasks/deploy_db.yml 57 | 58 | - include: tasks/deploy_web.yml 59 | 60 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_5/tasks/deploy_db.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: installed 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_3_HostVars_Include/Exercise_5/tasks/deploy_web.yml: -------------------------------------------------------------------------------- 1 | - name: Install Python Flask dependencies 2 | pip: 3 | name: '{{ item }}' 4 | state: present 5 | with_items: 6 | - flask 7 | - flask-mysql 8 | 9 | - name: Copy web-server code 10 | copy: src=app.py dest=/opt/app.py 11 | 12 | - name: Start web-application 13 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 14 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_1/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_1/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | # Variable file for host db_and_web_server 2 | 3 | ansible_ssh_pass: Passw0rd 4 | ansible_host: 192.168.1.14 5 | db_name: employee_db 6 | db_user: db_user 7 | db_password: Passw0rd 8 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_1/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_and_web_server -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_1/playbook.yml: -------------------------------------------------------------------------------- 1 | # In the previous Exercises we moved tasks to a separate tasks file and used include statements to include tasks. 2 | # Now we will start using Roles to organize and reuse our work 3 | # We have created a new role using the following command 'ansible-galaxy init mysql_db'. 4 | # This has created a folder structure for us. See the folder structure in the attached image. 5 | # First, move (simply cut and paste) the below tasks into roles/mysql_db/tasks/main.yml file 6 | # - Install MySQL database 7 | # - Start Mysql Service 8 | # - Create Application Database 9 | # - Create Application DB User 10 | # 11 | # Note, we will leave Install Dependencies there, since it is not specific to database or web. 12 | # We will add roles statement in a later Exercise 13 | # 14 | # 15 | # ------------------------------ 16 | # BEFORE 17 | # ------------------------------ 18 | #- 19 | # name: Deploy a web application 20 | # hosts: db_and_web_server 21 | # tasks: 22 | # - name: Install dependencies 23 | # apt: name={{ item }} state=present 24 | # with_items: 25 | # - python 26 | # - python-setuptools 27 | # - python-dev 28 | # - build-essential 29 | # - python-pip 30 | # - python-mysqldb 31 | # 32 | # - name: Install MySQL database 33 | # apt: 34 | # name: "{{ item }}" 35 | # state: present 36 | # with_items: 37 | # - mysql-server 38 | # - mysql-client 39 | # 40 | # - name: Start Mysql Service 41 | # service: 42 | # name: mysql 43 | # state: started 44 | # enabled: yes 45 | # 46 | # 47 | # - name: Create Application Database 48 | # mysql_db: name={{ db_name }} state=present 49 | # 50 | # 51 | # - name: Create Application DB User 52 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 53 | # 54 | # - name: Install Python Flask dependencies 55 | # pip: 56 | # name: '{{ item }}' 57 | # state: present 58 | # with_items: 59 | # - flask 60 | # - flask-mysql 61 | # 62 | # - name: Copy web-server code 63 | # copy: src=app.py dest=/opt/app.py 64 | # 65 | # - name: Start web-application 66 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 67 | # 68 | # ------------------------------ 69 | # AFTER 70 | # ------------------------------ 71 | - 72 | name: Deploy a web application 73 | hosts: db_and_web_server 74 | tasks: 75 | - name: Install dependencies 76 | apt: name={{ item }} state=present 77 | with_items: 78 | - python 79 | - python-setuptools 80 | - python-dev 81 | - build-essential 82 | - python-pip 83 | - python-mysqldb 84 | 85 | - name: Install Python Flask dependencies 86 | pip: 87 | name: '{{ item }}' 88 | state: present 89 | with_items: 90 | - flask 91 | - flask-mysql 92 | 93 | - name: Copy web-server code 94 | copy: src=app.py dest=/opt/app.py 95 | 96 | - name: Start web-application 97 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 98 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_1/roles/mysql_db/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: present 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_2/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_2/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | # Variable file for host db_and_web_server 2 | 3 | # ------------------------------ 4 | # BEFORE 5 | # ------------------------------ 6 | #ansible_ssh_pass: Passw0rd 7 | #ansible_host: 192.168.1.14 8 | #db_name: employee_db 9 | #db_user: db_user 10 | #db_password: Passw0rd 11 | # ------------------------------ 12 | # AFTER 13 | # ------------------------------ 14 | 15 | ansible_ssh_pass: Passw0rd 16 | ansible_host: 192.168.1.14 17 | db_name: employee_db 18 | db_user: db_user 19 | db_password: Passw0rd 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_2/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_and_web_server -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_2/playbook.yml: -------------------------------------------------------------------------------- 1 | We have now created a new role using the following command 'ansible-galaxy init flask_web'. 2 | This has created a folder structure for us. See the folder structure in the attached image. 3 | Let us move the web related tasks to roles/flask_web/tasks/main.yml file 4 | - Install Python Flask dependencies 5 | - Copy web-server code 6 | - Start web-application 7 | 8 | Note, we will leave Install Dependencies there, since it is not specific to database or web. 9 | We will add roles statement in a later Exercise 10 | 11 | 12 | # ------------------------------ 13 | # BEFORE 14 | # ------------------------------ 15 | #- 16 | # name: Deploy a web application 17 | # hosts: db_and_web_server 18 | # tasks: 19 | # - name: Install dependencies 20 | # apt: name={{ item }} state=present 21 | # with_items: 22 | # - python 23 | # - python-setuptools 24 | # - python-dev 25 | # - build-essential 26 | # - python-pip 27 | # - python-mysqldb 28 | # 29 | # - name: Install Python Flask dependencies 30 | # pip: 31 | # name: '{{ item }}' 32 | # state: present 33 | # with_items: 34 | # - flask 35 | # - flask-mysql 36 | # 37 | # - name: Copy web-server code 38 | # copy: src=app.py dest=/opt/app.py 39 | # 40 | # - name: Start web-application 41 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 42 | # 43 | # ------------------------------ 44 | # AFTER 45 | # ------------------------------ 46 | - 47 | name: Deploy a web application 48 | hosts: db_and_web_server 49 | tasks: 50 | - name: Install dependencies 51 | apt: name={{ item }} state=present 52 | with_items: 53 | - python 54 | - python-setuptools 55 | - python-dev 56 | - build-essential 57 | - python-pip 58 | - python-mysqldb 59 | 60 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_2/roles/flask_web/tasks/main.yml: -------------------------------------------------------------------------------- 1 | 2 | - name: Install Python Flask dependencies 3 | pip: 4 | name: '{{ item }}' 5 | state: present 6 | with_items: 7 | - flask 8 | - flask-mysql 9 | 10 | - name: Copy web-server code 11 | copy: src=app.py dest=/opt/app.py 12 | 13 | - name: Start web-application 14 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_2/roles/mysql_db/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: present 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_3/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_3/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | # Variable file for host db_and_web_server 2 | 3 | # ------------------------------ 4 | # BEFORE 5 | # ------------------------------ 6 | #ansible_ssh_pass: Passw0rd 7 | #ansible_host: 192.168.1.14 8 | #db_name: employee_db 9 | #db_user: db_user 10 | #db_password: Passw0rd 11 | # ------------------------------ 12 | # AFTER 13 | # ------------------------------ 14 | 15 | ansible_ssh_pass: Passw0rd 16 | ansible_host: 192.168.1.14 17 | db_name: employee_db 18 | db_user: db_user 19 | db_password: Passw0rd 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_3/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_and_web_server -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_3/playbook.yml: -------------------------------------------------------------------------------- 1 | # Let us also create a 3rd role called Python to install Python and its dependencies. 2 | # A command has been run to create a new role called python and the required files are in place. Check out the folder structure. 3 | # Now move the first task to "Install dependencies" into "roles/python/tasks/main.yml" file. 4 | # Remove the "tasks" directive from the play and leave it empty. 5 | # Note: We will add roles statement in a later Exercise 6 | # 7 | # 8 | ## ------------------------------ 9 | ## BEFORE 10 | ## ------------------------------ 11 | #- 12 | # name: Deploy a web application 13 | # hosts: db_and_web_server 14 | # tasks: 15 | # - name: Install dependencies 16 | # apt: name={{ item }} state=installed 17 | # with_items: 18 | # - python 19 | # - python-setuptools 20 | # - python-dev 21 | # - build-essential 22 | # - python-pip 23 | # - python-mysqldb 24 | 25 | # ------------------------------ 26 | # AFTER 27 | # ------------------------------ 28 | - 29 | name: Deploy a web application 30 | hosts: db_and_web_server 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_3/roles/flask_web/tasks/main.yml: -------------------------------------------------------------------------------- 1 | 2 | - name: Install Python Flask dependencies 3 | pip: 4 | name: '{{ item }}' 5 | state: present 6 | with_items: 7 | - flask 8 | - flask-mysql 9 | 10 | - name: Copy web-server code 11 | copy: src=app.py dest=/opt/app.py 12 | 13 | - name: Start web-application 14 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_3/roles/mysql_db/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: present 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_3/roles/python/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install dependencies 2 | apt: name={{ item }} state=present 3 | with_items: 4 | - python 5 | - python-setuptools 6 | - python-dev 7 | - build-essential 8 | - python-pip 9 | - python-mysqldb 10 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_4/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_4/host_vars/db_and_web_server.yml: -------------------------------------------------------------------------------- 1 | # Variable file for host db_and_web_server 2 | 3 | # ------------------------------ 4 | # BEFORE 5 | # ------------------------------ 6 | #ansible_ssh_pass: Passw0rd 7 | #ansible_host: 192.168.1.14 8 | #db_name: employee_db 9 | #db_user: db_user 10 | #db_password: Passw0rd 11 | # ------------------------------ 12 | # AFTER 13 | # ------------------------------ 14 | 15 | ansible_ssh_pass: Passw0rd 16 | ansible_host: 192.168.1.14 17 | db_name: employee_db 18 | db_user: db_user 19 | db_password: Passw0rd 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_4/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_and_web_server -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_4/playbook.yml: -------------------------------------------------------------------------------- 1 | # To tie it all together, add roles directive to the playbook and add the roles in the following order 2 | # - python 3 | # - mysql_db 4 | # - flask_web 5 | # 6 | ## ------------------------------ 7 | ## BEFORE 8 | ## ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: db_and_web_server 12 | 13 | # ------------------------------ 14 | # AFTER 15 | # ------------------------------ 16 | - 17 | name: Deploy a web application 18 | hosts: db_and_web_server 19 | roles: 20 | - python 21 | - mysql_db 22 | - flask_web 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_4/roles/flask_web/tasks/main.yml: -------------------------------------------------------------------------------- 1 | 2 | - name: Install Python Flask dependencies 3 | pip: 4 | name: '{{ item }}' 5 | state: present 6 | with_items: 7 | - flask 8 | - flask-mysql 9 | 10 | - name: Copy web-server code 11 | copy: src=app.py dest=/opt/app.py 12 | 13 | - name: Start web-application 14 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_4/roles/mysql_db/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: present 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_4/roles/python/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install dependencies 2 | apt: name={{ item }} state=present 3 | with_items: 4 | - python 5 | - python-setuptools 6 | - python-dev 7 | - build-essential 8 | - python-pip 9 | - python-mysqldb 10 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_5/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_5/host_vars/db_server.yml: -------------------------------------------------------------------------------- 1 | ansible_ssh_pass: Passw0rd 2 | ansible_host: 192.168.1.14 3 | db_name: employee_db 4 | db_user: db_user 5 | db_password: Passw0rd 6 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_5/host_vars/web_server.yml: -------------------------------------------------------------------------------- 1 | ansible_ssh_pass: Passw0rd 2 | ansible_host: 192.168.1.15 -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_5/inventory: -------------------------------------------------------------------------------- 1 | db_server 2 | web_server -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_5/playbook.yml: -------------------------------------------------------------------------------- 1 | We are now tasked to re-use our work to split the Architecture of our application from a monolithic (all-in-one) to distributed. 2 | Note that the inventory file is now updated with two separate servers - db_server and web_server. Also separate host_vars file have been created for each of them 3 | Task: Update the current play to target "db_server" host and apply roles "python" and "mysql_db" to it. 4 | Note: We need python dependencies on the sql server as well 5 | 6 | # ------------------------------ 7 | # BEFORE 8 | # ------------------------------ 9 | #- 10 | # name: Deploy a mysql DB 11 | # hosts: db_and_web_server 12 | # roles: 13 | # - python 14 | # - mysql_db 15 | # - flask_web 16 | # 17 | # 18 | # ------------------------------ 19 | # AFTER 20 | # ------------------------------ 21 | - 22 | name: Deploy a mysql DB 23 | hosts: db_server 24 | roles: 25 | - python 26 | - mysql_db 27 | 28 | 29 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_5/roles/flask_web/tasks/main.yml: -------------------------------------------------------------------------------- 1 | 2 | - name: Install Python Flask dependencies 3 | pip: 4 | name: '{{ item }}' 5 | state: present 6 | with_items: 7 | - flask 8 | - flask-mysql 9 | 10 | - name: Copy web-server code 11 | copy: src=app.py dest=/opt/app.py 12 | 13 | - name: Start web-application 14 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_5/roles/mysql_db/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: present 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_5/roles/python/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install dependencies 2 | apt: name={{ item }} state=present 3 | with_items: 4 | - python 5 | - python-setuptools 6 | - python-dev 7 | - build-essential 8 | - python-pip 9 | - python-mysqldb 10 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_6/app.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # 3 | # This is the web application code. DO NOT MODIFY 4 | # 5 | # ------------------------------------------------- 6 | 7 | from flask import Flask 8 | from flask.ext.mysql import MySQL 9 | app = Flask(__name__) 10 | 11 | mysql = MySQL() 12 | 13 | # MySQL configurations 14 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 16 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 18 | mysql.init_app(app) 19 | 20 | conn = mysql.connect() 21 | 22 | cursor = conn.cursor() 23 | 24 | @app.route("/") 25 | def main(): 26 | return "Welcome!" 27 | 28 | @app.route('/how are you') 29 | def hello(): 30 | return 'I am good, how about you?' 31 | 32 | @app.route('/read from database') 33 | def read(): 34 | cursor.execute("SELECT * FROM employees") 35 | row = cursor.fetchone() 36 | result = [] 37 | while row is not None: 38 | result.append(row[0]) 39 | row = cursor.fetchone() 40 | 41 | return ",".join(result) 42 | 43 | if __name__ == "__main__": 44 | app.run() -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_6/host_vars/db_server.yml: -------------------------------------------------------------------------------- 1 | ansible_ssh_pass: Passw0rd 2 | ansible_host: 192.168.1.14 3 | db_name: employee_db 4 | db_user: db_user 5 | db_password: Passw0rd 6 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_6/host_vars/web_server.yml: -------------------------------------------------------------------------------- 1 | ansible_ssh_pass: Passw0rd 2 | ansible_host: 192.168.1.15 -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_6/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_server 12 | web_server -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_6/playbook.yml: -------------------------------------------------------------------------------- 1 | # We only have db server right now. Let us create a new play to install and configure web application on the web server 2 | # Task: Create a new play called "Deploy a web server" and target host "web_server". Apply roles "python" and "flask_web" to it. 3 | # 4 | ## ------------------------------ 5 | ## BEFORE 6 | ## ------------------------------ 7 | #- 8 | # name: Deploy a mysql DB 9 | # hosts: db_server 10 | # roles: 11 | # - python 12 | # - mysql_db 13 | # 14 | # 15 | # ------------------------------ 16 | # AFTER 17 | # ------------------------------ 18 | - 19 | name: Deploy a mysql DB 20 | hosts: db_server 21 | roles: 22 | - python 23 | - mysql_db 24 | 25 | 26 | - 27 | name: Deploy a Web Server 28 | hosts: web_server 29 | roles: 30 | - python 31 | - flask_web 32 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_6/roles/flask_web/tasks/main.yml: -------------------------------------------------------------------------------- 1 | 2 | - name: Install Python Flask dependencies 3 | pip: 4 | name: '{{ item }}' 5 | state: present 6 | with_items: 7 | - flask 8 | - flask-mysql 9 | 10 | - name: Copy web-server code 11 | copy: src=app.py dest=/opt/app.py 12 | 13 | - name: Start web-application 14 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_6/roles/mysql_db/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install MySQL database 2 | apt: 3 | name: "{{ item }}" 4 | state: present 5 | with_items: 6 | - mysql-server 7 | - mysql-client 8 | 9 | - name: Start Mysql Service 10 | service: 11 | name: mysql 12 | state: started 13 | enabled: yes 14 | 15 | - name: Create Application Database 16 | mysql_db: name={{ db_name }} state=present 17 | 18 | - name: Create Application DB User 19 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 20 | -------------------------------------------------------------------------------- /Section_4_Roles/Exercise_6/roles/python/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install dependencies 2 | apt: name={{ item }} state=present 3 | with_items: 4 | - python 5 | - python-setuptools 6 | - python-dev 7 | - build-essential 8 | - python-pip 9 | - python-mysqldb 10 | -------------------------------------------------------------------------------- /Section_5_Async/Exercise_1/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_server 12 | web_server -------------------------------------------------------------------------------- /Section_5_Async/Exercise_1/playbook.yml: -------------------------------------------------------------------------------- 1 | # We have added a new play at the end of our playbook to monitor the web application for 6 minutes to ensure its running OK. 2 | # However, we don't want to hold the SSH Connection for the duration of this execution 3 | # Task: Make the monitoring task Asynchronous by adding async option to the task to wait for 6 minutes. 4 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_async.html 5 | # 6 | ## ------------------------------ 7 | ## BEFORE 8 | ## ------------------------------ 9 | #- 10 | # name: Deploy a mysql DB 11 | # hosts: db_server 12 | # roles: 13 | # - python 14 | # - mysql_db 15 | # 16 | # 17 | #- 18 | # name: Deploy a Web Server 19 | # hosts: web_server 20 | # roles: 21 | # - python 22 | # - flask_web 23 | # 24 | #- 25 | # name: Monitor Web Application for 6 Minutes 26 | # hosts: web_server 27 | # command: /opt/monitor_webapp.py 28 | 29 | 30 | # ------------------------------ 31 | # AFTER 32 | # ------------------------------ 33 | - 34 | name: Deploy a mysql DB 35 | hosts: db_server 36 | roles: 37 | - python 38 | - mysql_db 39 | 40 | - 41 | name: Deploy a Web Server 42 | hosts: web_server 43 | roles: 44 | - python 45 | - flask_web 46 | 47 | - 48 | name: Monitor Web Application for 6 Minutes 49 | hosts: web_server 50 | command: /opt/monitor_webapp.py 51 | async: 360 -------------------------------------------------------------------------------- /Section_5_Async/Exercise_2/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_server 12 | web_server -------------------------------------------------------------------------------- /Section_5_Async/Exercise_2/playbook.yml: -------------------------------------------------------------------------------- 1 | ## The default polling interval is 10 seconds. We think that is too often. 2 | ## Task: Change it to poll every 30 seconds. 3 | ## Documentation: http://docs.ansible.com/ansible/latest/playbooks_async.html 4 | # 5 | ## ------------------------------ 6 | ## BEFORE 7 | ## ------------------------------ 8 | #- 9 | # name: Deploy a mysql DB 10 | # hosts: db_server 11 | # roles: 12 | # - python 13 | # - mysql_db 14 | # 15 | #- 16 | # name: Deploy a Web Server 17 | # hosts: web_server 18 | # roles: 19 | # - python 20 | # - flask_web 21 | # 22 | #- 23 | # name: Monitor Web Application for 6 Minutes 24 | # hosts: web_server 25 | # command: /opt/monitor_webapp.py 26 | # async: 360 27 | 28 | # ------------------------------ 29 | # AFTER 30 | # ------------------------------ 31 | - 32 | name: Deploy a mysql DB 33 | hosts: db_server 34 | roles: 35 | - python 36 | - mysql_db 37 | 38 | - 39 | name: Deploy a Web Server 40 | hosts: web_server 41 | roles: 42 | - python 43 | - flask_web 44 | 45 | - 46 | name: Monitor Web Application for 6 Minutes 47 | hosts: web_server 48 | command: /opt/monitor_webapp.py 49 | async: 360 50 | poll: 30 -------------------------------------------------------------------------------- /Section_5_Async/Exercise_3/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_server 12 | web_server -------------------------------------------------------------------------------- /Section_5_Async/Exercise_3/playbook.yml: -------------------------------------------------------------------------------- 1 | ## We have now added a new monitoring play to monitor the database. However, our playbook spends 6 minutes monitoring web application and once that completes monitors database for 6 minutes. We would like to make this parallel. 2 | ## Task: Update poll value for both tasks to 0 to "fire and forget" the monitoring tasks. 3 | ## Documentation: http://docs.ansible.com/ansible/latest/playbooks_async.html 4 | # 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Deploy a mysql DB 10 | # hosts: db_server 11 | # roles: 12 | # - python 13 | # - mysql_db 14 | # 15 | #- 16 | # name: Deploy a Web Server 17 | # hosts: web_server 18 | # roles: 19 | # - python 20 | # - flask_web 21 | # 22 | #- 23 | # name: Monitor Web Application for 6 Minutes 24 | # hosts: web_server 25 | # command: /opt/monitor_webapp.py 26 | # async: 360 27 | # poll: 30 28 | # 29 | #- 30 | # name: Monitor Database for 6 Minutes 31 | # hosts: db_server 32 | # command: /opt/monitor_database.py 33 | # async: 360 34 | # poll: 30 35 | 36 | # ------------------------------ 37 | # AFTER 38 | # ------------------------------ 39 | - 40 | name: Deploy a mysql DB 41 | hosts: db_server 42 | roles: 43 | - python 44 | - mysql_db 45 | 46 | - 47 | name: Deploy a Web Server 48 | hosts: web_server 49 | roles: 50 | - python 51 | - flask_web 52 | 53 | - 54 | name: Monitor Web Application for 6 Minutes 55 | hosts: web_server 56 | command: /opt/monitor_webapp.py 57 | async: 360 58 | poll: 0 59 | 60 | - 61 | name: Monitor Database for 6 Minutes 62 | hosts: db_server 63 | command: /opt/monitor_database.py 64 | async: 360 65 | poll: 0 -------------------------------------------------------------------------------- /Section_5_Async/Exercise_4/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # BEFORE 3 | # ------------------------------ 4 | 5 | # db_and_web_server 6 | 7 | # ------------------------------ 8 | # AFTER 9 | # ------------------------------ 10 | 11 | db_server 12 | web_server -------------------------------------------------------------------------------- /Section_5_Async/Exercise_4/playbook.yml: -------------------------------------------------------------------------------- 1 | # We do not want to just "fire and forget", we would like to "check on it later" too. So register the results of the monitoring tasks into variables "webapp_result" and "database_result" 2 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_async.html 3 | # 4 | ## ------------------------------ 5 | ## BEFORE 6 | ## ------------------------------ 7 | #- 8 | # name: Deploy a mysql DB 9 | # hosts: db_server 10 | # roles: 11 | # - python 12 | # - mysql_db 13 | # 14 | #- 15 | # name: Deploy a Web Server 16 | # hosts: web_server 17 | # roles: 18 | # - python 19 | # - flask_web 20 | # 21 | #- 22 | # name: Monitor Web Application for 6 Minutes 23 | # hosts: web_server 24 | # command: /opt/monitor_webapp.py 25 | # async: 360 26 | # poll: 0 27 | # 28 | #- 29 | # name: Monitor Database for 6 Minutes 30 | # hosts: db_server 31 | # command: /opt/monitor_database.py 32 | # async: 360 33 | # poll: 0 34 | # ------------------------------ 35 | # AFTER 36 | # ------------------------------ 37 | - 38 | name: Deploy a mysql DB 39 | hosts: db_server 40 | roles: 41 | - python 42 | - mysql_db 43 | 44 | - 45 | name: Deploy a Web Server 46 | hosts: web_server 47 | roles: 48 | - python 49 | - flask_web 50 | 51 | - 52 | name: Monitor Web Application for 6 Minutes 53 | hosts: web_server 54 | command: /opt/monitor_webapp.py 55 | async: 360 56 | poll: 0 57 | register: webapp_result 58 | 59 | - 60 | name: Monitor Database for 6 Minutes 61 | hosts: db_server 62 | command: /opt/monitor_database.py 63 | async: 360 64 | poll: 0 65 | register: database_result -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_1/inventory: -------------------------------------------------------------------------------- 1 | [app_servers] 2 | app_server1 3 | app_server2 4 | app_server3 5 | app_server4 6 | app_server5 7 | app_server6 8 | app_server7 9 | app_server8 10 | app_server9 11 | app_server10 -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_1/playbook.yml: -------------------------------------------------------------------------------- 1 | # Here we have our initial playbook that we created to deploy a full stack web application on a single server in a monolithic fashion. 2 | # First we would like to run the same playbook on 10 different servers. Check the inventory file for list of servers. 3 | # Task: Update the playbook to run it against all 10 servers. Use the group name to target all servers in the group 4 | 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Deploy a web application 10 | # hosts: db_and_web_server 11 | # vars: 12 | # db_name: employee_db 13 | # db_user: db_user 14 | # db_password: Passw0rd 15 | # tasks: 16 | # - name: Install dependencies 17 | # apt: name={{ item }} state=installed 18 | # with_items: 19 | # - python 20 | # - python-setuptools 21 | # - python-dev 22 | # - build-essential 23 | # - python-pip 24 | # - python-mysqldb 25 | # 26 | # - name: Install MySQL database 27 | # apt: 28 | # name: "{{ item }}" 29 | # state: installed 30 | # with_items: 31 | # - mysql-server 32 | # - mysql-client 33 | # 34 | # - name: Start Mysql Service 35 | # service: 36 | # name: mysql 37 | # state: started 38 | # enabled: yes 39 | # 40 | # - name: Create Application Database 41 | # mysql_db: name={{ db_name }} state=present 42 | # 43 | # - name: Create Application DB User 44 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 45 | # 46 | # - name: Install Python Flask dependencies 47 | # pip: 48 | # name: '{{ item }}' 49 | # state: present 50 | # with_items: 51 | # - flask 52 | # - flask-mysql 53 | # 54 | # - name: Copy web-server code 55 | # copy: src=app.py dest=/opt/app.py 56 | # 57 | # - name: Start web-application 58 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 59 | 60 | # ------------------------------ 61 | # AFTER 62 | # ------------------------------ 63 | - 64 | name: Deploy a web application 65 | hosts: app_servers 66 | vars: 67 | db_name: employee_db 68 | db_user: db_user 69 | db_password: Passw0rd 70 | tasks: 71 | - name: Install dependencies 72 | apt: name={{ item }} state=installed 73 | with_items: 74 | - python 75 | - python-setuptools 76 | - python-dev 77 | - build-essential 78 | - python-pip 79 | - python-mysqldb 80 | 81 | - name: Install MySQL database 82 | apt: 83 | name: "{{ item }}" 84 | state: installed 85 | with_items: 86 | - mysql-server 87 | - mysql-client 88 | 89 | - name: Start Mysql Service 90 | service: 91 | name: mysql 92 | state: started 93 | enabled: yes 94 | 95 | - name: Create Application Database 96 | mysql_db: name={{ db_name }} state=present 97 | 98 | - name: Create Application DB User 99 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 100 | 101 | - name: Install Python Flask dependencies 102 | pip: 103 | name: '{{ item }}' 104 | state: present 105 | with_items: 106 | - flask 107 | - flask-mysql 108 | 109 | - name: Copy web-server code 110 | copy: src=app.py dest=/opt/app.py 111 | 112 | - name: Start web-application 113 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 114 | -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_2/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # AFTER 3 | # ------------------------------ 4 | 5 | [app_servers] 6 | app_server1 7 | app_server2 8 | app_server3 9 | app_server4 10 | app_server5 11 | app_server6 12 | app_server7 13 | app_server8 14 | app_server9 15 | app_server10 -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_2/playbook.yml: -------------------------------------------------------------------------------- 1 | # Currently Ansible runs tasks on all of them at once. We would like Ansible to limit execution on just 2 servers at once. 2 | # Task: Use the serial directive to run the tasks on only 2 servers at a time 3 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_async.html 4 | 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Deploy a web application 10 | # hosts: app_servers 11 | # vars: 12 | # db_name: employee_db 13 | # db_user: db_user 14 | # db_password: Passw0rd 15 | # tasks: 16 | # - name: Install dependencies 17 | # apt: name={{ item }} state=installed 18 | # with_items: 19 | # - python 20 | # - python-setuptools 21 | # - python-dev 22 | # - build-essential 23 | # - python-pip 24 | # - python-mysqldb 25 | # 26 | # - name: Install MySQL database 27 | # apt: 28 | # name: "{{ item }}" 29 | # state: installed 30 | # with_items: 31 | # - mysql-server 32 | # - mysql-client 33 | # 34 | # - name: Start Mysql Service 35 | # service: 36 | # name: mysql 37 | # state: started 38 | # enabled: yes 39 | # 40 | # - name: Create Application Database 41 | # mysql_db: name={{ db_name }} state=present 42 | # 43 | # - name: Create Application DB User 44 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 45 | # 46 | # - name: Install Python Flask dependencies 47 | # pip: 48 | # name: '{{ item }}' 49 | # state: present 50 | # with_items: 51 | # - flask 52 | # - flask-mysql 53 | # 54 | # - name: Copy web-server code 55 | # copy: src=app.py dest=/opt/app.py 56 | # 57 | # - name: Start web-application 58 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 59 | 60 | # ------------------------------ 61 | # AFTER 62 | # ------------------------------ 63 | - 64 | name: Deploy a web application 65 | hosts: app_servers 66 | serial: 2 67 | vars: 68 | db_name: employee_db 69 | db_user: db_user 70 | db_password: Passw0rd 71 | tasks: 72 | - name: Install dependencies 73 | apt: name={{ item }} state=installed 74 | with_items: 75 | - python 76 | - python-setuptools 77 | - python-dev 78 | - build-essential 79 | - python-pip 80 | - python-mysqldb 81 | 82 | - name: Install MySQL database 83 | apt: 84 | name: "{{ item }}" 85 | state: installed 86 | with_items: 87 | - mysql-server 88 | - mysql-client 89 | 90 | - name: Start Mysql Service 91 | service: 92 | name: mysql 93 | state: started 94 | enabled: yes 95 | 96 | - name: Create Application Database 97 | mysql_db: name={{ db_name }} state=present 98 | 99 | - name: Create Application DB User 100 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 101 | 102 | - name: Install Python Flask dependencies 103 | pip: 104 | name: '{{ item }}' 105 | state: present 106 | with_items: 107 | - flask 108 | - flask-mysql 109 | 110 | - name: Copy web-server code 111 | copy: src=app.py dest=/opt/app.py 112 | 113 | - name: Start web-application 114 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 115 | -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_3/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # AFTER 3 | # ------------------------------ 4 | 5 | [app_servers] 6 | app_server1 7 | app_server2 8 | app_server3 9 | app_server4 10 | app_server5 11 | app_server6 12 | app_server7 13 | app_server8 14 | app_server9 15 | app_server10 -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_3/playbook.yml: -------------------------------------------------------------------------------- 1 | # Let's take it one step further. The serial directive has some advanced use cases. 2 | # For example you could run the play against 2 servers first, then 3 in the next round and then the last 5 together by providing an array as input to the serial directive. This is really usefull for "Rolling Updates" 3 | # Task: Update serial directive to use an array instead of a value. Provide 2,3,5 as values. 4 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_async.html 5 | 6 | # ------------------------------ 7 | # BEFORE 8 | # ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: app_servers 12 | # serial: 2 13 | # vars: 14 | # db_name: employee_db 15 | # db_user: db_user 16 | # db_password: Passw0rd 17 | # tasks: 18 | # - name: Install dependencies 19 | # apt: name={{ item }} state=installed 20 | # with_items: 21 | # - python 22 | # - python-setuptools 23 | # - python-dev 24 | # - build-essential 25 | # - python-pip 26 | # - python-mysqldb 27 | # 28 | # - name: Install MySQL database 29 | # apt: 30 | # name: "{{ item }}" 31 | # state: installed 32 | # with_items: 33 | # - mysql-server 34 | # - mysql-client 35 | # 36 | # - name: Start Mysql Service 37 | # service: 38 | # name: mysql 39 | # state: started 40 | # enabled: yes 41 | # 42 | # - name: Create Application Database 43 | # mysql_db: name={{ db_name }} state=present 44 | # 45 | # - name: Create Application DB User 46 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 47 | # 48 | # - name: Install Python Flask dependencies 49 | # pip: 50 | # name: '{{ item }}' 51 | # state: present 52 | # with_items: 53 | # - flask 54 | # - flask-mysql 55 | # 56 | # - name: Copy web-server code 57 | # copy: src=app.py dest=/opt/app.py 58 | # 59 | # - name: Start web-application 60 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 61 | 62 | # ------------------------------ 63 | # AFTER 64 | # ------------------------------ 65 | - 66 | name: Deploy a web application 67 | hosts: app_servers 68 | serial: 69 | - 2 70 | - 3 71 | - 5 72 | vars: 73 | db_name: employee_db 74 | db_user: db_user 75 | db_password: Passw0rd 76 | tasks: 77 | - name: Install dependencies 78 | apt: name={{ item }} state=installed 79 | with_items: 80 | - python 81 | - python-setuptools 82 | - python-dev 83 | - build-essential 84 | - python-pip 85 | - python-mysqldb 86 | 87 | - name: Install MySQL database 88 | apt: 89 | name: "{{ item }}" 90 | state: installed 91 | with_items: 92 | - mysql-server 93 | - mysql-client 94 | 95 | - name: Start Mysql Service 96 | service: 97 | name: mysql 98 | state: started 99 | enabled: yes 100 | 101 | - name: Create Application Database 102 | mysql_db: name={{ db_name }} state=present 103 | 104 | - name: Create Application DB User 105 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 106 | 107 | - name: Install Python Flask dependencies 108 | pip: 109 | name: '{{ item }}' 110 | state: present 111 | with_items: 112 | - flask 113 | - flask-mysql 114 | 115 | - name: Copy web-server code 116 | copy: src=app.py dest=/opt/app.py 117 | 118 | - name: Start web-application 119 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 120 | -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_4/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # AFTER 3 | # ------------------------------ 4 | 5 | [app_servers] 6 | app_server1 7 | app_server2 8 | app_server3 9 | app_server4 10 | app_server5 11 | app_server6 12 | app_server7 13 | app_server8 14 | app_server9 15 | app_server10 -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_4/playbook.yml: -------------------------------------------------------------------------------- 1 | # Let's take it one step further. The serial directive has some advanced use cases. 2 | # For example you could specify a percentage to say execute the playbook against 20% of servers at a time. 3 | # Task: Update serial directive to use a percentage (20%) instead of a value. 4 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_async.html 5 | 6 | # ------------------------------ 7 | # BEFORE 8 | # ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: app_servers 12 | # serial: 2 13 | # vars: 14 | # db_name: employee_db 15 | # db_user: db_user 16 | # db_password: Passw0rd 17 | # tasks: 18 | # - name: Install dependencies 19 | # apt: name={{ item }} state=installed 20 | # with_items: 21 | # - python 22 | # - python-setuptools 23 | # - python-dev 24 | # - build-essential 25 | # - python-pip 26 | # - python-mysqldb 27 | # 28 | # - name: Install MySQL database 29 | # apt: 30 | # name: "{{ item }}" 31 | # state: installed 32 | # with_items: 33 | # - mysql-server 34 | # - mysql-client 35 | # 36 | # - name: Start Mysql Service 37 | # service: 38 | # name: mysql 39 | # state: started 40 | # enabled: yes 41 | # 42 | # - name: Create Application Database 43 | # mysql_db: name={{ db_name }} state=present 44 | # 45 | # - name: Create Application DB User 46 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 47 | # 48 | # - name: Install Python Flask dependencies 49 | # pip: 50 | # name: '{{ item }}' 51 | # state: present 52 | # with_items: 53 | # - flask 54 | # - flask-mysql 55 | # 56 | # - name: Copy web-server code 57 | # copy: src=app.py dest=/opt/app.py 58 | # 59 | # - name: Start web-application 60 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 61 | 62 | # ------------------------------ 63 | # AFTER 64 | # ------------------------------ 65 | - 66 | name: Deploy a web application 67 | hosts: app_servers 68 | serial: "20%" 69 | vars: 70 | db_name: employee_db 71 | db_user: db_user 72 | db_password: Passw0rd 73 | tasks: 74 | - name: Install dependencies 75 | apt: name={{ item }} state=installed 76 | with_items: 77 | - python 78 | - python-setuptools 79 | - python-dev 80 | - build-essential 81 | - python-pip 82 | - python-mysqldb 83 | 84 | - name: Install MySQL database 85 | apt: 86 | name: "{{ item }}" 87 | state: installed 88 | with_items: 89 | - mysql-server 90 | - mysql-client 91 | 92 | - name: Start Mysql Service 93 | service: 94 | name: mysql 95 | state: started 96 | enabled: yes 97 | 98 | - name: Create Application Database 99 | mysql_db: name={{ db_name }} state=present 100 | 101 | - name: Create Application DB User 102 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 103 | 104 | - name: Install Python Flask dependencies 105 | pip: 106 | name: '{{ item }}' 107 | state: present 108 | with_items: 109 | - flask 110 | - flask-mysql 111 | 112 | - name: Copy web-server code 113 | copy: src=app.py dest=/opt/app.py 114 | 115 | - name: Start web-application 116 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 117 | -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_5/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # AFTER 3 | # ------------------------------ 4 | 5 | [app_servers] 6 | app_server1 7 | app_server2 8 | app_server3 9 | app_server4 10 | app_server5 11 | app_server6 12 | app_server7 13 | app_server8 14 | app_server9 15 | app_server10 -------------------------------------------------------------------------------- /Section_6_Strategy/Exercise_5/playbook.yml: -------------------------------------------------------------------------------- 1 | #With the linear strategy all hosts will run each task before any host starts the next task. We would like eac host to run until the end of the play as fast as it can. 2 | #Task: Use free strategy to accomplish this task. 3 | #Documentation: http://docs.ansible.com/ansible/latest/playbooks_strategies.html 4 | # 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Deploy a web application 10 | # hosts: app_servers 11 | # vars: 12 | # db_name: employee_db 13 | # db_user: db_user 14 | # db_password: Passw0rd 15 | # tasks: 16 | # - name: Install dependencies 17 | # apt: name={{ item }} state=installed 18 | # with_items: 19 | # - python 20 | # - python-setuptools 21 | # - python-dev 22 | # - build-essential 23 | # - python-pip 24 | # - python-mysqldb 25 | # 26 | # - name: Install MySQL database 27 | # apt: 28 | # name: "{{ item }}" 29 | # state: installed 30 | # with_items: 31 | # - mysql-server 32 | # - mysql-client 33 | # 34 | # - name: Start Mysql Service 35 | # service: 36 | # name: mysql 37 | # state: started 38 | # enabled: yes 39 | # 40 | # - name: Create Application Database 41 | # mysql_db: name={{ db_name }} state=present 42 | # 43 | # - name: Create Application DB User 44 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 45 | # 46 | # - name: Install Python Flask dependencies 47 | # pip: 48 | # name: '{{ item }}' 49 | # state: present 50 | # with_items: 51 | # - flask 52 | # - flask-mysql 53 | # 54 | # - name: Copy web-server code 55 | # copy: src=app.py dest=/opt/app.py 56 | # 57 | # - name: Start web-application 58 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 59 | # 60 | # ------------------------------ 61 | # AFTER 62 | # ------------------------------ 63 | - 64 | name: Deploy a web application 65 | hosts: app_servers 66 | strategy: free 67 | vars: 68 | db_name: employee_db 69 | db_user: db_user 70 | db_password: Passw0rd 71 | tasks: 72 | - name: Install dependencies 73 | apt: name={{ item }} state=installed 74 | with_items: 75 | - python 76 | - python-setuptools 77 | - python-dev 78 | - build-essential 79 | - python-pip 80 | - python-mysqldb 81 | 82 | - name: Install MySQL database 83 | apt: 84 | name: "{{ item }}" 85 | state: installed 86 | with_items: 87 | - mysql-server 88 | - mysql-client 89 | 90 | - name: Start Mysql Service 91 | service: 92 | name: mysql 93 | state: started 94 | enabled: yes 95 | 96 | - name: Create Application Database 97 | mysql_db: name={{ db_name }} state=present 98 | 99 | - name: Create Application DB User 100 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 101 | 102 | - name: Install Python Flask dependencies 103 | pip: 104 | name: '{{ item }}' 105 | state: present 106 | with_items: 107 | - flask 108 | - flask-mysql 109 | 110 | - name: Copy web-server code 111 | copy: src=app.py dest=/opt/app.py 112 | 113 | - name: Start web-application 114 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 115 | -------------------------------------------------------------------------------- /Section_7_ErrorHandling/Exercise_1/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # AFTER 3 | # ------------------------------ 4 | 5 | [app_servers] 6 | app_server1 7 | app_server2 8 | app_server3 9 | app_server4 10 | app_server5 11 | app_server6 12 | app_server7 13 | app_server8 14 | app_server9 15 | app_server10 -------------------------------------------------------------------------------- /Section_7_ErrorHandling/Exercise_1/playbook.yml: -------------------------------------------------------------------------------- 1 | # When ever a host fails execution of a task, Ansible removes that host from the list and continues execution of playbook with the remaining hosts 2 | # We would like Ansible to stop execution of the entire playbook if a single server was to fail. 3 | # Task: Set the any_errors_fatal option to True for the play 4 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_error_handling.html 5 | 6 | # ------------------------------ 7 | # BEFORE 8 | # ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: app_servers 12 | # vars: 13 | # db_name: employee_db 14 | # db_user: db_user 15 | # db_password: Passw0rd 16 | # tasks: 17 | # - name: Install dependencies 18 | # apt: name={{ item }} state=installed 19 | # with_items: 20 | # - python 21 | # - python-setuptools 22 | # - python-dev 23 | # - build-essential 24 | # - python-pip 25 | # - python-mysqldb 26 | # 27 | # - name: Install MySQL database 28 | # apt: 29 | # name: "{{ item }}" 30 | # state: installed 31 | # with_items: 32 | # - mysql-server 33 | # - mysql-client 34 | # 35 | # - name: Start Mysql Service 36 | # service: 37 | # name: mysql 38 | # state: started 39 | # enabled: yes 40 | # 41 | # - name: Create Application Database 42 | # mysql_db: name={{ db_name }} state=present 43 | # 44 | # - name: Create Application DB User 45 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 46 | # 47 | # - name: Install Python Flask dependencies 48 | # pip: 49 | # name: '{{ item }}' 50 | # state: present 51 | # with_items: 52 | # - flask 53 | # - flask-mysql 54 | # 55 | # - name: Copy web-server code 56 | # copy: src=app.py dest=/opt/app.py 57 | # 58 | # - name: Start web-application 59 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 60 | 61 | # ------------------------------ 62 | # AFTER 63 | # ------------------------------ 64 | - 65 | name: Deploy a web application 66 | hosts: app_servers 67 | any_errors_fatal: true 68 | vars: 69 | db_name: employee_db 70 | db_user: db_user 71 | db_password: Passw0rd 72 | tasks: 73 | - name: Install dependencies 74 | apt: name={{ item }} state=installed 75 | with_items: 76 | - python 77 | - python-setuptools 78 | - python-dev 79 | - build-essential 80 | - python-pip 81 | - python-mysqldb 82 | 83 | - name: Install MySQL database 84 | apt: 85 | name: "{{ item }}" 86 | state: installed 87 | with_items: 88 | - mysql-server 89 | - mysql-client 90 | 91 | - name: Start Mysql Service 92 | service: 93 | name: mysql 94 | state: started 95 | enabled: yes 96 | 97 | - name: Create Application Database 98 | mysql_db: name={{ db_name }} state=present 99 | 100 | - name: Create Application DB User 101 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 102 | 103 | - name: Install Python Flask dependencies 104 | pip: 105 | name: '{{ item }}' 106 | state: present 107 | with_items: 108 | - flask 109 | - flask-mysql 110 | 111 | - name: Copy web-server code 112 | copy: src=app.py dest=/opt/app.py 113 | 114 | - name: Start web-application 115 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 116 | -------------------------------------------------------------------------------- /Section_7_ErrorHandling/Exercise_2/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # AFTER 3 | # ------------------------------ 4 | 5 | [app_servers] 6 | app_server1 7 | app_server2 8 | app_server3 9 | app_server4 10 | app_server5 11 | app_server6 12 | app_server7 13 | app_server8 14 | app_server9 15 | app_server10 -------------------------------------------------------------------------------- /Section_7_ErrorHandling/Exercise_2/playbook.yml: -------------------------------------------------------------------------------- 1 | # We have added a new task at the end to send a notification email, once all tasks are complete. However, the SMTP server is not very stable and these emails are not critical. 2 | # We would like Ansible to ignore even if the email task fails 3 | # Task: Set the ignore_errors option on the mail task to "yes" to ignore errors 4 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_error_handling.html 5 | 6 | # ------------------------------ 7 | # BEFORE 8 | # ------------------------------ 9 | #- 10 | # name: Deploy a web application 11 | # hosts: app_servers 12 | # any_errors_fatal: true 13 | # vars: 14 | # db_name: employee_db 15 | # db_user: db_user 16 | # db_password: Passw0rd 17 | # tasks: 18 | # - name: Install dependencies 19 | # apt: name={{ item }} state=installed 20 | # with_items: 21 | # - python 22 | # - python-setuptools 23 | # - python-dev 24 | # - build-essential 25 | # - python-pip 26 | # - python-mysqldb 27 | # 28 | # - name: Install MySQL database 29 | # apt: 30 | # name: "{{ item }}" 31 | # state: installed 32 | # with_items: 33 | # - mysql-server 34 | # - mysql-client 35 | # 36 | # - name: Start Mysql Service 37 | # service: 38 | # name: mysql 39 | # state: started 40 | # enabled: yes 41 | # 42 | # - name: Create Application Database 43 | # mysql_db: name={{ db_name }} state=present 44 | # 45 | # - name: Create Application DB User 46 | # mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 47 | # 48 | # - name: Install Python Flask dependencies 49 | # pip: 50 | # name: '{{ item }}' 51 | # state: present 52 | # with_items: 53 | # - flask 54 | # - flask-mysql 55 | # 56 | # - name: Copy web-server code 57 | # copy: src=app.py dest=/opt/app.py 58 | # 59 | # - name: Start web-application 60 | # shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 61 | # 62 | # - name: "Send notification email" 63 | # mail: 64 | # to: devops@corp.com 65 | # subject: Server Deployed! 66 | # body: Web Server Deployed Successfully 67 | # ------------------------------ 68 | # AFTER 69 | # ------------------------------ 70 | - 71 | name: Deploy a web application 72 | hosts: app_servers 73 | any_errors_fatal: true 74 | vars: 75 | db_name: employee_db 76 | db_user: db_user 77 | db_password: Passw0rd 78 | tasks: 79 | - name: Install dependencies 80 | apt: name={{ item }} state=installed 81 | with_items: 82 | - python 83 | - python-setuptools 84 | - python-dev 85 | - build-essential 86 | - python-pip 87 | - python-mysqldb 88 | 89 | - name: Install MySQL database 90 | apt: 91 | name: "{{ item }}" 92 | state: installed 93 | with_items: 94 | - mysql-server 95 | - mysql-client 96 | 97 | - name: Start Mysql Service 98 | service: 99 | name: mysql 100 | state: started 101 | enabled: yes 102 | 103 | - name: Create Application Database 104 | mysql_db: name={{ db_name }} state=present 105 | 106 | - name: Create Application DB User 107 | mysql_user: name={{ db_user }} password={{ db_password }} priv='*.*:ALL' host='%' state='present' 108 | 109 | - name: Install Python Flask dependencies 110 | pip: 111 | name: '{{ item }}' 112 | state: present 113 | with_items: 114 | - flask 115 | - flask-mysql 116 | 117 | - name: Copy web-server code 118 | copy: src=app.py dest=/opt/app.py 119 | 120 | - name: Start web-application 121 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 122 | 123 | - name: "Send notification email" 124 | mail: 125 | to: devops@corp.com 126 | subject: Server Deployed! 127 | body: Web Server Deployed Successfully 128 | ignore_errors: yes 129 | 130 | -------------------------------------------------------------------------------- /Section_8_Jinja2/Exercise_1/playbook.yml: -------------------------------------------------------------------------------- 1 | # Update the msg field in debug task to print - "The name is bond! james bond!" 2 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_filters.html#filters-for-formatting-data 3 | 4 | # ------------------------------ 5 | # BEFORE 6 | # ------------------------------ 7 | #- 8 | # name: Test Jinja2 Templating 9 | # hosts: localhost 10 | # vars: 11 | # first_name: James 12 | # last_name: Bond 13 | # tasks: 14 | # - debug: 15 | # msg: '' 16 | # ------------------------------ 17 | # AFTER 18 | # ------------------------------ 19 | - 20 | name: Test Jinja2 Templating 21 | hosts: localhost 22 | vars: 23 | first_name: james 24 | last_name: bond 25 | tasks: 26 | - debug: 27 | msg: 'The name is {{ last_name }}! {{ first_name }} {{ last_name }}!' 28 | -------------------------------------------------------------------------------- /Section_8_Jinja2/Exercise_2/playbook.yml: -------------------------------------------------------------------------------- 1 | # Update the msg field in debug task to print name in title case - "The name is Bond! James Bond!" 2 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_filters.html#filters-for-formatting-data 3 | 4 | # ------------------------------ 5 | # BEFORE 6 | # ------------------------------ 7 | #- 8 | # name: Test Jinja2 Templating 9 | # hosts: localhost 10 | # vars: 11 | # first_name: James 12 | # last_name: Bond 13 | # tasks: 14 | # - debug: 15 | # msg: '' 16 | # ------------------------------ 17 | # AFTER 18 | # ------------------------------ 19 | - 20 | name: Test Jinja2 Templating 21 | hosts: localhost 22 | vars: 23 | first_name: james 24 | last_name: bond 25 | tasks: 26 | - debug: 27 | msg: 'The name is {{ last_name | title }}! {{ first_name | title }} {{ last_name | title }}!' 28 | -------------------------------------------------------------------------------- /Section_8_Jinja2/Exercise_3/playbook.yml: -------------------------------------------------------------------------------- 1 | # Update the msg field in debug task to print the lowest number in the given list using jinja2 filter "min" - "Lowest = 6" 2 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_filters.html#filters-for-formatting-data 3 | 4 | # ------------------------------ 5 | # BEFORE 6 | # ------------------------------ 7 | #- 8 | # name: Test Jinja2 Templating 9 | # hosts: localhost 10 | # vars: 11 | # array_of_numbers: 12 | # - 12 13 | # - 34 14 | # - 06 15 | # - 34 16 | # tasks: 17 | # - debug: 18 | # msg: 'Lowest = ' 19 | 20 | # ------------------------------ 21 | # AFTER 22 | # ------------------------------ 23 | - 24 | name: Test Jinja2 Templating 25 | hosts: localhost 26 | vars: 27 | array_of_numbers: 28 | - 12 29 | - 34 30 | - 06 31 | - 34 32 | tasks: 33 | - debug: 34 | msg: 'Lowest = {{ array_of_numbers | min }}' 35 | -------------------------------------------------------------------------------- /Section_8_Jinja2/Exercise_4/playbook.yml: -------------------------------------------------------------------------------- 1 | # We are given two lists of dependent packages to install. A playbook has been written with a task to install dependencies 2 | # Task: Identify the unique packages from the two lists (web_dependencies and sql_dependencies) using jinja2 filter union and apply it in the given space under with_items. 3 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_filters.html#filters-for-formatting-data 4 | 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Install Dependencies 10 | # hosts: localhost 11 | # vars: 12 | # web_dependencies: 13 | # - python 14 | # - python-setuptools 15 | # - python-dev 16 | # - build-essential 17 | # - python-pip 18 | # - python-mysqldb 19 | # sql_dependencies: 20 | # - python 21 | # - python-mysqldb 22 | # tasks: 23 | # - name: Install dependencies 24 | # apt: name='{{ item }}' state=installed 25 | # with_items: '{{ }}' 26 | # 27 | 28 | # ------------------------------ 29 | # AFTER 30 | # ------------------------------ 31 | - 32 | name: Install Dependencies 33 | hosts: localhost 34 | vars: 35 | web_dependencies: 36 | - python 37 | - python-setuptools 38 | - python-dev 39 | - build-essential 40 | - python-pip 41 | - python-mysqldb 42 | sql_dependencies: 43 | - python 44 | - python-mysqldb 45 | tasks: 46 | - name: Install dependencies 47 | apt: name='{{ item }}' state=installed 48 | with_items: '{{ sql_dependencies | union(web_dependencies) }}' 49 | 50 | -------------------------------------------------------------------------------- /Section_8_Jinja2/Exercise_5/playbook.yml: -------------------------------------------------------------------------------- 1 | # We are trying to write a playbook to generate a file with random name everytime. The first name must be /tmp/random_file_ followed by a random number. eg: /tmp/random_1234 2 | # Task: Update the task to modify the file name to use a random number suffix anywhere from 0 to 1000. 3 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_filters.html#filters-for-formatting-data 4 | 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Generate random file name 10 | # hosts: localhost 11 | # tasks: 12 | # - name: Install dependencies 13 | # file: 14 | # path: /tmp/random_file 15 | # state: touch 16 | 17 | 18 | # ------------------------------ 19 | # AFTER 20 | # ------------------------------ 21 | - 22 | name: Generate random file name 23 | hosts: localhost 24 | tasks: 25 | - name: Create file 26 | file: 27 | path: /tmp/random_file"{{ 1000 | random }}" 28 | state: touch 29 | 30 | -------------------------------------------------------------------------------- /Section_8_Jinja2/Exercise_6/playbook.yml: -------------------------------------------------------------------------------- 1 | # Test if a given variable has a valid IP address. 2 | # Task: Apply the ipaddr filter to the ip_address in the msg field of debug task to test if IP is valid. It will return false if IP is invalid 3 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_filters.html#filters-for-formatting-data 4 | 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Test valid IP Address 10 | # hosts: localhost 11 | # vars: 12 | # ip_address: 192.168.1.s 13 | # tasks: 14 | # - name: Test IP Address 15 | # debug: 16 | # msg: IP Address = {{ ip_address }} 17 | 18 | # ------------------------------ 19 | # AFTER 20 | # ------------------------------ 21 | - 22 | name: Test valid IP Address 23 | hosts: localhost 24 | vars: 25 | ip_address: 192.168.1.6 26 | tasks: 27 | - name: Test IP Address 28 | debug: 29 | msg: IP Address = {{ ip_address | ipaddr }} 30 | 31 | -------------------------------------------------------------------------------- /Section_8_Jinja2/Exercise_7/playbook.yml: -------------------------------------------------------------------------------- 1 | # Retrieve the file name from a given path in linux 2 | # Task: Use the 'basename' filter to retrieve the file name 3 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_filters.html#filters-for-formatting-data 4 | 5 | # ------------------------------ 6 | # BEFORE 7 | # ------------------------------ 8 | #- 9 | # name: Get filename 10 | # hosts: localhost 11 | # vars: 12 | # file_path: /etc/hosts 13 | # tasks: 14 | # - name: Get filename 15 | # debug: 16 | # msg: File Name = {{file_path }} 17 | # ------------------------------ 18 | # AFTER 19 | # ------------------------------ 20 | - 21 | name: Get filename 22 | hosts: localhost 23 | vars: 24 | file_path: /etc/hosts 25 | tasks: 26 | - name: Get filename 27 | debug: 28 | msg: File Name = {{file_path | basename}} 29 | 30 | -------------------------------------------------------------------------------- /Section_9_Lookups/Exercise_1/credentials.csv: -------------------------------------------------------------------------------- 1 | # Credentials File 2 | Hostname,Password 3 | web_server,Passw0rd 4 | db_server,Passw0rd -------------------------------------------------------------------------------- /Section_9_Lookups/Exercise_1/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # AFTER 3 | # ------------------------------ 4 | 5 | db_server 6 | web_server -------------------------------------------------------------------------------- /Section_9_Lookups/Exercise_1/playbook.yml: -------------------------------------------------------------------------------- 1 | # We have moved the credentials for hosts out of the inventory file and into a separate csv file called credentials.csv. Check it out! 2 | # In the given playbook, the password for the host web_server is hardcoded into a variable ansible_ssh_pass. 3 | # Replace the Ansible Password field to use "lookup" plugin to lookup a "csvfile", the file is "credentials.csv" and the value to lookup is "web_server" 4 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_lookups.html 5 | 6 | # ------------------------------ 7 | # BEFORE 8 | # ------------------------------ 9 | #- 10 | # name: Test Connectivity 11 | # hosts: web_server 12 | # vars: 13 | # ansible_ssh_pass: "Passw0rd" 14 | # tasks: 15 | # - name: Ping target host 16 | # ping: 17 | # data: "Test" 18 | # ------------------------------ 19 | # AFTER 20 | # ------------------------------ 21 | - 22 | name: Test Connectivity 23 | hosts: web_server 24 | vars: 25 | ansible_ssh_pass: "{{ lookup('csvfile', 'web_server file=credentials.csv delimiter=,') }}" 26 | tasks: 27 | - name: Ping target host 28 | ping: 29 | data: "Test" 30 | 31 | -------------------------------------------------------------------------------- /Section_9_Lookups/Exercise_2/credentials.ini: -------------------------------------------------------------------------------- 1 | # Credentials File 2 | 3 | [web_server] 4 | password=Passw0rd 5 | 6 | [db_server] 7 | password=Passw0rd -------------------------------------------------------------------------------- /Section_9_Lookups/Exercise_2/inventory: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # AFTER 3 | # ------------------------------ 4 | 5 | db_server 6 | web_server -------------------------------------------------------------------------------- /Section_9_Lookups/Exercise_2/playbook.yml: -------------------------------------------------------------------------------- 1 | # Let us attempt the same operation if the credential file were to be in ini format 2 | # In the given playbook, the password for the host web_server is hardcoded into a variable ansible_ssh_pass. 3 | # Replace the Ansible Password field to use "lookup" plugin to lookup a "ini" file, the file is "credentials.ini" and the value to lookup is "password" and the section is "web_server" 4 | # Documentation: http://docs.ansible.com/ansible/latest/playbooks_lookups.html#the-ini-file-lookup 5 | 6 | # ------------------------------ 7 | # BEFORE 8 | # ------------------------------ 9 | #- 10 | # name: Test Connectivity 11 | # hosts: web_server 12 | # vars: 13 | # ansible_ssh_pass: "Passw0rd" 14 | # tasks: 15 | # - name: Ping target host 16 | # ping: 17 | # data: "Test" 18 | # ------------------------------ 19 | # AFTER 20 | # ------------------------------ 21 | - 22 | name: Test Connectivity 23 | hosts: web_server 24 | vars: 25 | ansible_ssh_pass: "{{ lookup('ini', 'password section=web_server file=credentials.ini') }}" 26 | tasks: 27 | - name: Ping target host 28 | ping: 29 | data: "Test" 30 | 31 | --------------------------------------------------------------------------------