-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoci_instance_manage.yml
More file actions
126 lines (113 loc) · 4.94 KB
/
Copy pathoci_instance_manage.yml
File metadata and controls
126 lines (113 loc) · 4.94 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
- name: Excluir e recriar instância OCI
hosts: localhost
connection: local
gather_facts: no
collections:
- oracle.oci
vars:
# Define o caminho do arquivo de configuração da OCI.
config_file: "{{ lookup('env','HOME') + '/.oci/config' }}"
profile: DEFAULT
# Variáveis para a criação da instância:
compartment_id: "ocid1.tenancy.oc1..aaaaaaaaz7uk3wkn677lp5u5esw76tcogwy3les5fulelf2h7hya74icjola"
# display_name: "InstanciaAlwaysFree"
shape: "VM.Standard.E2.1.Micro" # Geralmente o shape da always free
# image_id: "ocid1.image.oc1.sa-saopaulo-1.aaaaaaaakjohycxtogead7pigb7isau535wv5ge66cowirigqtyuvx4h6yva" # Ex: Ubuntu LTS
#"ocid1.image.oc1.sa-saopaulo-1.aaaaaaaaauivm4ncxj2ompvenq6kvdm4cf2r2jh2siqng4u6du5v5qmg32lq"
image_id: "ocid1.image.oc1.sa-saopaulo-1.aaaaaaaadwxporzjrx24ul2gub5fsoqgaekd6uxwurpqsvzpwcqutyovzk2a"
# image_id: "ocid1.image.oc1.sa-saopaulo-1.aaaaaaaaauivm4ncxj2ompvenq6kvdm4cf2r2jh2siqng4u6du5v5qmg32lq"
subnet_id: "ocid1.subnet.oc1.sa-saopaulo-1.aaaaaaaa25iq462rmvklxfj367l4irvjag22d2r2mn57xrqntp4cm3icf6pa"
# subnet_id: "ocid1.vcn.oc1.sa-saopaulo-1.amaaaaaaj5yni5yaj2srxlvoxhpw62acv4qojmqp4snzx3zppftoorb2smca"
availability_domain: "FzDv:SA-SAOPAULO-1-AD-1"
tasks:
- name: Listar instâncias existentes no compartimento
oracle.oci.oci_compute_instance_facts:
config_file_location: "{{ config_file }}"
config_profile_name: "{{ profile }}"
compartment_id: "{{ compartment_id }}"
register: instances_info
tags:
- listar
- excluir
- name: Exibir instâncias encontradas (display_name e OCID)
debug:
msg: "{{ instances_info.instances | map(attribute='display_name') | zip(instances_info.instances | map(attribute='id')) | list }}"
tags:
- listar
- excluir
- name: Informar o OCID da instância a ser excluída (sem aspas)
pause:
prompt: "Digite o OCID da instância a ser excluída"
register: instance_ocid_input
tags:
- excluir
- name: Definir o instance_id com base no input do usuário
set_fact:
instance_id: "{{ instance_ocid_input.user_input }}"
tags:
- excluir
- name: Exibir o instance_id que será utilizado para exclusão
debug:
msg: "A instância a ser excluída possui o OCID: {{ instance_id }}"
tags:
- excluir
- name: Excluir a instância selecionada
oracle.oci.oci_compute_instance:
config_file_location: "{{ config_file }}"
config_profile_name: "{{ profile }}"
state: absent
instance_id: "{{ instance_id }}"
preserve_boot_volume: no
tags:
- excluir
# ------------------------------------------------
- name: Criar instância
tags: criar
block:
- name: Solicitar o nome da instância a ser criada
block:
- name: Entrar com o nome da instância
pause:
prompt: "Digite o nome da instância a ser criada"
register: input_display
- name: Definir display_name a partir da entrada do usuário
set_fact:
display_name: "{{ input_display.user_input }}"
when: display_name is not defined or display_name == ""
- name: Perguntar ao usuário o caminho da chave pública SSH
pause:
prompt: "Digite o caminho onde se localiza a sua chave pública SSH (padrão: ~/.ssh/id_ed25519.pub)"
register: ssh_key_input
- name: Definir variável para chave SSH
set_fact:
# ssh_key_path: "{{ ssh_key_input.user_input | default(lookup('env', 'HOME') + '/.ssh/id_ed25519.pub') }}"
ssh_key_path: "{{ ssh_key_input.user_input | default(lookup('env', 'HOME') ~ '/.ssh/id_ed25519.pub', true) }}"
- name: Nome da instância
debug:
msg: "{{ display_name }}"
- name: Local da chave privada
debug:
msg: "{{ ssh_key_path }}"
- name: Criar nova instância OCI
oracle.oci.oci_compute_instance:
config_file_location: "{{ config_file }}"
config_profile_name: "{{ profile }}"
state: present
compartment_id: "{{ compartment_id }}"
display_name: "{{ display_name }}"
shape: "{{ shape }}"
availability_domain: "{{ availability_domain }}"
source_details:
source_type: "image"
image_id: "{{ image_id }}"
subnet_id: "{{ subnet_id }}"
metadata:
ssh_authorized_keys: "{{ lookup('file', ssh_key_path) }}"
# ssh_authorized_keys: "{{ lookup('env','HOME') + 'ssh_key_path' }}"
# ssh_authorized_keys: "{{ lookup('file', ssh_key_path) }}"
register: nova_instancia
- name: Exibir detalhes da nova instância
debug:
var: nova_instancia
# ------------------------------------------------