DGB
DGB

Reputation: 53

Using Ansible, how does one temporarily shut down a GCP VM instance, as opposed to deleting it?

Using the gcp_compute_instance command in ansible, how does one achieve the same as 'shut down' on the GCP console?

Upvotes: 1

Views: 726

Answers (2)

DGB
DGB

Reputation: 53

Thanks @Zeitounator

status: TERMINATED does indeed work, but then one must be sure to set the deletion_protection parameter, else it complains, fatally.

   - name: Shut down a GCP instance temporarily
      gcp_compute_instance:
        name: "{{ inventory_hostname_short }}"
        deletion_protection: no
        machine_type: "{{ gcp_ce_machine_type }}"
        disks:
          - auto_delete: 'false'
            boot: 'true'
            source: "{{ disk }}"
        network_interfaces:
        - network:
          access_configs:
          - name: External NAT
            nat_ip:
            type: ONE_TO_ONE_NAT
        zone: "{{ gcp_ce_zone }}"
        project: "{{ gcp_ce_project_name }}"
        auth_kind: serviceaccount
        service_account_file: "{{ gcp_ce_service_account_keyfile }}"
        status: TERMINATED
      delegate_to: localhost

Upvotes: 2

DGB
DGB

Reputation: 53

One could send a 'shutdown now' command after safely shutting down the database and other services that might not like a hard shut down:

- name: Send shutdown now command vi ssh
  command: /usr/bin/ssh "{{ ansible_ssh_user }}"@"{{ host-ip-address }}" -C "sudo shutdown now"
  delegate_to: localhost

Upvotes: 1

Related Questions