【问题标题】:Ansible and GCP Using facts GCP filestore moduleAnsible 和 GCP 使用 Facts GCP 文件存储模块
【发布时间】:2021-03-13 22:21:40
【问题描述】:

编辑:我可以使用 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"}

谢谢

【问题讨论】:

    标签: amazon-web-services amazon-s3 google-cloud-platform ansible bucket


    【解决方案1】:

    如果我相信您回答的示例输出,则该信息将在您的任务结果中以 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 }}"
    

    【讨论】:

    • 完美,亲爱的,它工作正常,但我试图获取 ipAddresses 但它不起作用。请问你知道怎么获取吗? “{{ instance_info.resources.0.networks }} 工作正常,但试过这个不起作用“{{ instance_info.resources.0.networks.ipAddresses }}”
    • 请注意我的示例,但最重要的是注意您的数据。 networks 是一个列表,ipAddresses 也是。
    • 完美,您应该编辑 Ansible 的官方文档:D。谢谢你帮了我很多。
    • you should edit official doc of Ansible => 为什么是我?您可以自己为此提出 PR。毕竟你是使用模块的人而不是我;)
    • 因为你是绝地大师而我只是一个简单的学徒:)
    猜你喜欢
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多