Deploying a VM in Yandex.Cloud and deploying a Python application using ansible
-
Ansible must be installed on the server from which ansible-playbook will be launched, how to do this is described here - https://docs.ansible.com/ansible/latest/installation_guide/installation_distros.html
-
To deploy the server itself in Yandex.Cloud, a module was used - https://github.com/arenadata/ansible-module-yandex-cloud, for which you need to install https://github.com/yandex-cloud/python- sdk
pip install --user git+https://github.com/yandex-cloud/python-sdk
-
Here and below, all paths and commands are indicated based on the fact that the current directory is the root directory of the project.
-
First, you need to edit the file
create_vm/vars/main.yml
, in it save all the variables that need to be transferred from Yandex.Cloud after registering with the service and creating folders and the internal network:token: how to get here https://cloud.yandex.ru/docs/iam/concepts/authorization/oauth-token user: The user that will be created when the server is deployed. folder_id: cloud folder identifier image_id: if you try to manually create a VM in Yandex.Cloud, there will be several operating systems of different versions available to choose from; when you select any OS, you can find out its image_id subnet_id: subnet identifier
-
Next, if necessary, you can edit the VM configuration file -
create_vm/tasks/main.yml
, specifying the required number of vcpu, vram, disk size, etc. The public IP address is assigned automatically, and it is dynamic. If you need a static one, then it must be reserved in the cloud web interface after creating the VM.
VM Deployment
-
To start VM deployment, use the command:
ansible-playbook playbook.yml --tags create_vm
Setting up the server and launching the bot
-
If necessary, you can specify the Postgresql version in the
postgres/vars/main.yml
file. -
In the file
deploy/vars/main.yml
we set the domain name, the root directory of the bot and the database password. -
The
deploy/files
directory contains the bot files, and thedeploy/files/certs
directory contains certificates issued earlier. If necessary, it will be possible to re-release it after deployment, having previously linked the server IP to the domain name. -
The
deploy/files/.env
file also contains important variables, such as the bot token, DATABASE_URL, etc. -
The
deploy/templates
directory contains templates for future systemd daemons. -
File with tasks for the deploy role -
deploy/tasks/main.yml
. -
To start the deployment, use the following command:
ansible-playbook -i inventory.ini playbook.yml --tags postgres,deploy