# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---

- name: Mounts the file systems specified in the metadata network_storage key
  hosts: localhost
  become: true
  vars:
    meta_key: "network_storage"
    url: "http://metadata.google.internal/computeMetadata/v1/instance/attributes"
  tasks:
  - name: Read metadata network_storage information
    ansible.builtin.uri:
      url: "{{ url }}/{{ meta_key }}"
      method: GET
      headers:
        Metadata-Flavor: "Google"
    register: storage
  - name: Mount file systems
    ansible.posix.mount:
      src: "{{ item.server_ip }}:/{{ item.remote_mount }}"
      path: "{{ item.local_mount }}"
      opts: "{{ item.mount_options }}"
      boot: true
      fstype: "{{ item.fs_type }}"
      state: "mounted"
    loop: "{{ storage.json }}"
