Ansible 和 GCP 使用事实 GCP 文件存储模块

Ansible and GCP Using facts GCP filestore module

编辑:我可以使用 gcloud 但看不到如何在 var.

中获取 ip
gcloud filestore instances describe nfsxxxd --project=dxxxxt-2xxx --zone=xxxx-xx-b --format='get(networks.ipAddresses)'

['1xx.x.x.1']

我正在尝试创建文件存储并将其安装到实例中。 我在尝试获取这个新文件存储的 ipadress 时遇到问题。

我正在使用 ansible 模块,在 ansible 命令中使用 -v 时我可以看到输出。

Ansible 模块文件存储:

- name: get info on an instance
  gcp_filestore_instance_info:
    zone: xxxxx-xxxx-b
    project: dxxxxx-xxxxxx
    auth_kind: serviceaccount
    service_account_file: "/root/dxxxt-xxxxxxx.json"

Ansible 输出:

ok: [xxxxxx-xxxxxx] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": false, "resources": [{"createTime": "2021-03-12T13:40:36.438598373Z", "fileShares": [{"capacityGb": "1024", "name": "nfxxxxx"}], "name": "projects/xxx-xxxxx/locations/xxxxxxx-b/instances/xxxxx-xxx", "networks": [{"ipAddresses": ["1xx.x.x.x"], "modes": ["MODE_IPV4"], "network": "admin", "reservedIpRange": "1xx.x.x.x/29"}], "state": "READY", "tier": "BASIC_HDD"}, {"createTime": "2021-03-10T11:13:00.111631131Z", "fileShares": [{"capacityGb": "1024", "name": "nfsnxxxxx", "nfsExportOptions": [{"accessMode": "READ_WRITE", "ipRanges": ["xxx.xx.xx.xxx"], "squashMode": "NO_ROOT_SQUASH"}]}], "name": "projects/dxxx-xxxxx/locations/xxxxx/instances/innxxxx", "networks": [{"ipAddresses": ["x.x.x.x."], ...

我已经试过了,但没有用。 Ansible 任务:

- name: print fact filestore
  debug:
     msg: "{{ansible_facts.resources.createTime}}"

fatal: [nxxxxxxx]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'resources'\n\nThe error appears to be in '/root/xxxxxxx/tasks/main.yml': line 11, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print fact filestore\n  ^ here\n"}

谢谢

如果我相信您回答的示例输出,信息将返回到任务结果的 resources 键中。我无法亲自测试,但我相信以下内容应该符合您的期望。

请注意 resources 是一个字典列表。在下面的示例中,我将从列表的第一个元素访问信息。如果您需要其他东西(例如所有 createTime... 的列表)或遍历这些对象,您可以从这个例子扩展。

- name: get info on an instance
  gcp_filestore_instance_info:
    zone: xxxxx-xxxx-b
    project: dxxxxx-xxxxxx
    auth_kind: serviceaccount
    service_account_file: "/root/dxxxt-xxxxxxx.json"
  register: instance_info

- name: show create time for first resource
  debug:
    msg: "{{ instance_info.resources.0.createTime }}"

- name: show first ip of first network of first resource
  debug:
    msg: "{{ instance_info.resources.0.networks.0.ipAddresses.0 }}"