Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCP instance templates should look for the metadata under the "properties" key #56793

Open
wants to merge 2 commits into
base: devel
from

Conversation

Projects
None yet
3 participants
@zwebb
Copy link

commented May 22, 2019

SUMMARY

The gcp_compute_instance_template documentation specifies that metadata for instance templates should be included under the properties key. However, the metadata encode/decode functions in gcp_compute_instance_template.py look for metadata on the root of the request/response instead. This PR changes the encode/decode functions to instead look under properties.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

gcp_compute_instance_template

ADDITIONAL INFORMATION

The following playbook illustrates the problem:

- name: Test playbook
  hosts: localhost
  gather_facts: no
  connection: local
  vars:
    gcp_project: ...
    gcp_cred_kind: serviceaccount
    gcp_cred_file: ...
    region: "us-west1"
    zone: "us-west1-b"

  tasks:
  - name: delete the existing instance template
    gcp_compute_instance_template:
      name: 'my-instance-template'
      project: "{{ gcp_project }}"
      auth_kind: "{{ gcp_cred_kind }}"
      service_account_file: "{{ gcp_cred_file }}"
      state: absent
  - name: create the instance template
    gcp_compute_instance_template:
      name: 'my-instance-template'
      project: "{{ gcp_project }}"
      auth_kind: "{{ gcp_cred_kind }}"
      service_account_file: "{{ gcp_cred_file }}"
      properties:
        disks:
        - auto_delete: yes
          boot: true
          initialize_params:
            source_image: "projects/centos-cloud/global/images/centos-7-v20190213"
        machine_type: n1-standard-1
        metadata:
          startup-script: "#!/bin/bash\necho hello"
        network_interfaces:
        - network:
            selfLink: "global/networks/default"
      state: present
    register: disk

Running this playbook with the verbose flag shows the response, which does not have the metadata on it (it only has the kind and fingerprint keys, which are supposed to be filtered out by decode_metadata):

$ ansible-playbook -v gwdev-sdpibc.yml
[WARNING]: log file at /var/log/ansible is not writeable and we cannot create it, aborting

Using /etc/ansible/ansible.cfg as config file
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'


PLAY [Test playbook] *******************************************************************************************************

TASK [delete the existing instance template] *******************************************************************************
ok: [localhost] => {"changed": false}

TASK [create the instance template] ****************************************************************************************
changed: [localhost] => {"changed": true, "creationTimestamp": "2019-05-22T07:35:07.950-07:00", "description": "", "id": "4078127546305667316", "kind": "compute#instanceTemplate", "name": "my-instance-template", "properties": {"disks": [{"autoDelete": true, "boot": true, "deviceName": "persistent-disk-0", "index": 0, "initializeParams": {"sourceImage": "projects/centos-cloud/global/images/centos-7-v20190213"}, "kind": "compute#attachedDisk", "mode": "READ_WRITE", "type": "PERSISTENT"}], "machineType": "n1-standard-1", "metadata": {"fingerprint": "4OFH1G7MRBA=", "kind": "compute#metadata"}, "networkInterfaces": [{"kind": "compute#networkInterface", "network": "https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/global/networks/default"}]}, "selfLink": "https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/global/instanceTemplates/my-instance-template"}

PLAY RECAP *****************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Checking the object in the GCP console similarly shows that there is no metadata.

When the playbook is run against this PR's branch, however, the response shows that metadata exists on the instance template:

$ ansible-playbook -v gwdev-sdpibc.yml
[WARNING]: log file at /var/log/ansible is not writeable and we cannot create it, aborting

Using /etc/ansible/ansible.cfg as config file
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'


PLAY [Test playbook] *******************************************************************************************************

TASK [delete the existing instance template] *******************************************************************************
changed: [localhost] => {"changed": true}

TASK [create the instance template] ****************************************************************************************
changed: [localhost] => {"changed": true, "creationTimestamp": "2019-05-22T07:38:08.063-07:00", "description": "", "id": "2215106857994346559", "kind": "compute#instanceTemplate", "name": "my-instance-template", "properties": {"disks": [{"autoDelete": true, "boot": true, "deviceName": "persistent-disk-0", "index": 0, "initializeParams": {"sourceImage": "projects/centos-cloud/global/images/centos-7-v20190213"}, "kind": "compute#attachedDisk", "mode": "READ_WRITE", "type": "PERSISTENT"}], "machineType": "n1-standard-1", "metadata": {"startup-script": "#!/bin/bash\necho hello"}, "networkInterfaces": [{"kind": "compute#networkInterface", "network": "https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/global/networks/default"}]}, "selfLink": "https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/global/instanceTemplates/my-instance-template"}

PLAY RECAP *****************************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Checking in the GCP console now shows that there is metadata attached.

@ansibot

This comment has been minimized.

@rambleraptor

This comment has been minimized.

Copy link
Contributor

commented May 22, 2019

This is great, thank you!

ship_it

@zwebb

This comment has been minimized.

Copy link
Author

commented May 28, 2019

@rambleraptor sorry, I'm new to this process--is there something else I need to do before this can be merged?

@rambleraptor

This comment has been minimized.

Copy link
Contributor

commented May 28, 2019

Nope. The bot is supposed to read the ship_it command and merge the PR. Let me try one more time.

@rambleraptor

This comment has been minimized.

Copy link
Contributor

commented May 28, 2019

ship_it

@rambleraptor

This comment has been minimized.

Copy link
Contributor

commented May 28, 2019

I keep misspelling it! One more time.

shipit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.