使用 GCE 元数据作为可靠的事实或变量

Using GCE metadata as ansible facts or variables

我需要以 ansible facts/variables 的形式访问 Google Compute Engine 实例元数据(实例名称、区域、项目名称、机器类型、自定义元数据、外部 IP 等)。例如,我希望能够使用像这样的 variable/fact:{{ gce_fact_instance_name }} 当 ansible 从每个主机收集事实时,它会从实例中收集(因为所有实例都可以访问到他们自己的元数据)。

我找到了这个模块,但它目前似乎还没有开发,并且在尝试使用它时会产生错误。 https://github.com/br0ziliy/ansible-module-gce-facts

上面列出的模块的作者修复了错误,他的模块非常有用,完全满足我的需要。下面的示例代码:

- name: Gather instance GCE facts
  action: gce_facts

- name: Update GCE labels
  local_action:
    module: gce_labels
    project_id: "{{ ansible_gce.project.projectId }}"
    resource_type: instances
    resource_name: "{{ ansible_gce.instance.name }}"
    resource_location: "{{ ansible_gce.instance.zone }}"
    #ld_ variables are custom variables we use for system management
    labels:
      env: "{{ ld_env }}"
      product: "{{ ld_product }}"
      role: "{{ ld_role }}"