-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy_vm.yml
73 lines (65 loc) · 2.17 KB
/
deploy_vm.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
- name: Create GCP VM instance with Spot option and generate host inventory
hosts: localhost
gather_facts: no
vars:
gcp_project: abdtp-421121
gcp_cred_file: ascn-grupo39-key.json
gcp_zone: us-east1-b
inventory_template: host_template.j2
tasks:
- name: Create a GCP VM
google.cloud.gcp_compute_instance:
state: present
name: "abd-vm-spot-miguel"
machine_type: "n2-standard-8"
disks:
- auto_delete: true
boot: true
initialize_params:
disk_size_gb: 500
disk_type: "pd-ssd"
source_image: "projects/ubuntu-os-cloud/global/images/family/ubuntu-2204-lts"
network_interfaces:
- access_configs:
- name: External NAT
type: ONE_TO_ONE_NAT
scheduling:
on_host_maintenance: "TERMINATE"
automatic_restart: false
preemptible: true
zone: "{{ gcp_zone }}"
project: "{{ gcp_project }}"
auth_kind: serviceaccount
service_account_file: "{{ gcp_cred_file }}"
register: gcp_vm
- name: Output VM info
debug:
msg: "VM created with IP: {{ gcp_vm.networkInterfaces[0].accessConfigs[0].natIP }}"
when: gcp_vm is defined # Only display when IP is available
- name: Generate host inventory file
template:
src: "{{ inventory_template }}"
dest: hosts.inv
delegate_to: localhost
when: gcp_vm is defined # Only generate inventory when IP is available
- name: Example Playbook
hosts: my_vm # Replace with the hostname or IP address of your remote machine
become: yes # Enable privilege escalation, if necessary
tasks:
- name: Ensure Postgres is installed
apt:
name: postgresql
state: present
- name: Ensure Postgres is running
service:
name: postgresql
state: started
enabled: yes
- name: Install Git
apt:
name: git
state: present
- name: Clone Git repository using SSH key authentication
git:
repo: "git@github.com:jpgoncalves13/TP_ABD.git" # SSH URL of the repository
dest: "/home/miguel/TP_ABD"