简体   繁体   中英

Ansible and GCP Using facts GCP filestore module

EDIT: 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']

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

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

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

Thanks

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM