本文介绍了Ansible和GCP使用事实GCP文件存储模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用gcloud,但是看不到如何在var中获取IP.

I can use gcloud but cannot see how to get ip in var.

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

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

我正在尝试创建文件存储并将其挂载到实例中.尝试获取此新文件存储的ipadress时遇到问题.

I'am tring to create filestore and mount it in instance.I facing an issue when trying to get ipadress of this new filestore.

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

I'am using ansible module and I can see output when using -v in ansible command.

Ansible模块文件存储:

Ansible module filestore:

- 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输出:

Ansible output:

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."], ...

我已经尝试过了,但是没有用.繁重的任务:

I have tried this but it doesn't works.Ansible tasks:

- 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 键返回.我无法自我测试,但我相信以下内容应符合您的期望.

If I believe the example output from your answer, the info is returned in a resources key in your task result. I cannot test myself, but I believe the following should meet your expectation.

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

Please note that resources is a list of dicts. In my below example I will access the info from the first element of the list. If you need someting else (e.g. list of all createTime...) or to loop over those objects, you can extend from this example.

- 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 }}"

这篇关于Ansible和GCP使用事实GCP文件存储模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:20