Skip to content
Snippets Groups Projects
Commit 7cf6b363 authored by abir.chebbi's avatar abir.chebbi
Browse files

Add ansible

parent cc487ffa
Branches Ansible
No related tags found
No related merge requests found
config.ini
Ansible/abir.pem
Ansible/instance_ids.txt
Ansible/instance_ip.ini
---
- 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
---
- 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
---
- 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
[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
key_name: "abir"
ami_id: "ami-08919ae65ab65be94"
security_group: "gr14-lab1"
instance_name: "chatbot"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment