I have the following GCP inventory source configured:
我配置了以下GCP库存来源:
---
plugin: google.cloud.gcp_compute
projects:
- project1
- project2
- project3
filters:
#- status = RUNNING
#- scheduling.automaticRestart = true AND status = RUNNING
keyed_groups:
# Create groups from GCE labels
- prefix: gcp
key: labels
- prefix: gcp_project
key: project
hostnames:
# List host by name instead of the default public ip
- name
compose:
ansible_host: name ~ "." ~ labels.domainmembership ~ ".local"
Which gives the following value for ansible_host
when a VM named server1
has a domainmembership
label with the value domain1
:
当名为server1的VM具有值为domain1的domainmembership标签时,它为ansible_host提供以下值:
ansible_host: "server1.domain1.local"
How would I use omit
to set ansible_host
to networkInterfaces[0].networkIP
if labels.domainmembership
is empty or not present?
如果labels.mainmembership为空或不存在,我如何使用省略将ansible_host设置为networkInterfaces[0].networkIP?
更多回答
优秀答案推荐
Disclaimer: I don't have a GCP account at hand to test this.
免责声明:我手头没有GCP账户来测试这个。
omit
is not what you are looking for here, as it will make the ansible_host
variable optional, so, in short, ansible_host
could be undefined.
这里不需要省略,因为它会使ansible_host变量成为可选变量,所以简而言之,ansible/host可能是未定义的。
What you really want is to offer a default
, and since you want to default also when the value is defined but empty, you want to set its second parameter to true
.
您真正想要的是提供一个默认值,并且由于您也希望在值已定义但为空时进行默认,因此您希望将其第二个参数设置为true。
So, this should do:
因此,这应该做到:
compose:
ansible_host: >-
name ~ "." ~ labels.domainmembership ~ ".local"
| default(networkInterfaces[0].networkIP, true)
更多回答
我是一名优秀的程序员,十分优秀!