diff --git a/.gitignore b/.gitignore index 2fa7ce7c417ccfd994ebfb295684680ad4447d62..8028b73e8db38f69bdc363441f5113451701d101 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ config.ini +Ansible/abir.pem +Ansible/instance_ids.txt +Ansible/instance_ip.ini + diff --git a/Part2/Ansible/1.create_instance.yml b/Part2/Ansible/1.create_instance.yml new file mode 100644 index 0000000000000000000000000000000000000000..0ed25cb1959715a4775f4a2d8ed94ae8d2a05d4a --- /dev/null +++ b/Part2/Ansible/1.create_instance.yml @@ -0,0 +1,37 @@ +--- +- name: Create EC2 instance + hosts: localhost + vars_files: + - config.yml + gather_facts: false + become: False + vars: + #instance_type: t4g.micro + instance_type: t2.micro + instance_count: 1 + + tasks: + - name: EC2 instances creation + amazon.aws.ec2_instance: + name: "{{ instance_name }}" + key_name: "{{ key_name }}" + image_id: "{{ ami_id }}" + instance_type: "{{ instance_type }}" + network: + assign_public_ip: true + security_group: "{{ security_group }}" + count: "{{ instance_count }}" + wait: true + register: ec2 + + - name: Save IP addreesses in "instance_ip.ini" file + copy: + content: "[my_hosts]\n{{ ec2.instances | json_query('[].public_ip_address') | join('\n') }}" + dest: instance_ip.ini + + # We need instances ID to delete them at the end of the exercise. + - name: Saving the instances ID in the instance_ids.txt file + copy: + content: "{{ ec2.instances | map(attribute='instance_id') | join('\n') }}" + dest: ./instance_ids.txt + diff --git a/Part2/Ansible/2.deploy.yml b/Part2/Ansible/2.deploy.yml new file mode 100644 index 0000000000000000000000000000000000000000..f7fce35aa0462279092299351b442436da44551e --- /dev/null +++ b/Part2/Ansible/2.deploy.yml @@ -0,0 +1,28 @@ +--- +- name: Run Streamlit Application + hosts: my_hosts + gather_facts: no + become: false + vars: + virtual_env_path: /home/ubuntu/chatbotlab/bin/activate + application_path: /home/ubuntu/chatbot-lab/Part2 + config_template: ./../config.ini + + tasks: + + - name: copy the config.ini file + copy: + src: "{{ config_template }}" + dest: "{{ application_path }}/config.ini" + become_user: ubuntu + notify: streamlit + tags: config + + + - name: Source the virtual environment and run Streamlit + shell: source {{ virtual_env_path }} && cd {{ application_path }} && streamlit run chatbot.py & + args: + executable: /bin/bash + + + \ No newline at end of file diff --git a/Part2/Ansible/3.terminate_instance.yml b/Part2/Ansible/3.terminate_instance.yml new file mode 100644 index 0000000000000000000000000000000000000000..e51c774881d6859c684e6a3dd3d8da839cab22a0 --- /dev/null +++ b/Part2/Ansible/3.terminate_instance.yml @@ -0,0 +1,23 @@ +--- +- name: Delete EC2 instances + hosts: localhost + gather_facts: no + become: False + tasks: + - name: Read instance identifiers from the file + ansible.builtin.slurp: + src: ./instance_ids.txt + register: instance_ids_raw + + - name: Convert instance identifiers to a list + set_fact: + instance_ids: "{{ instance_ids_raw['content'] | b64decode | split('\n') | select('match', '^i-[a-zA-Z0-9]+') | list }}" + + - name: Show public IP + debug: + var: instance_ids + + - name: Terminate EC2 instances + amazon.aws.ec2_instance: + instance_ids: "{{ instance_ids }}" + state: absent diff --git a/Part2/Ansible/ansible.cfg b/Part2/Ansible/ansible.cfg new file mode 100644 index 0000000000000000000000000000000000000000..5ec7f3b289fe0ec10318ac5983b49581ec938e89 --- /dev/null +++ b/Part2/Ansible/ansible.cfg @@ -0,0 +1,10 @@ + +[defaults] +inventory = ./instance_ip.ini +remote_user = ubuntu +private_key_file = abir.pem +host_key_checking = False +retry_files_enabled = False +deprecation_warnings = False +pipelining = True + diff --git a/Part2/Ansible/config.yml b/Part2/Ansible/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..a81b0c3ca58d66b2ca07f5e1882b24a0f0981ecc --- /dev/null +++ b/Part2/Ansible/config.yml @@ -0,0 +1,4 @@ +key_name: "abir" +ami_id: "ami-08919ae65ab65be94" +security_group: "gr14-lab1" +instance_name: "chatbot"