我正在尝试使用Ansible 运行动态库存列表
ansible-inventory --list -i gcp.yaml
---
plugin: gcp_compute
projects:
- <project name>
auth_kind: serviceaccount
service_account_file: /opt/ansible/inventory/peak-equator-SA.json
filters:
- labels|list = "ansible"
获取以下错误
[WARNING]: * Failed to parse /opt/ansible/inventory/gcp.yaml with auto plugin: [{'message': 'Invalid value for field 'filter': 'labels|list = "ansible"'. Invalid
list filter expression.', 'domain': 'global', 'reason': 'invalid'}]
[WARNING]: * Failed to parse /opt/ansible/inventory/gcp.yaml with yaml plugin: Plugin configuration YAML file, not YAML inventory
[WARNING]: * Failed to parse /opt/ansible/inventory/gcp.yaml with ini plugin: Invalid host pattern '---' supplied, '---' is normally a sign this is a YAML file.
[WARNING]: Unable to parse /opt/ansible/inventory/gcp.yaml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
}
}
我刚才遇到这个问题后;过滤器";不是Ansible过滤器,而是特定于Google Cloud API的过滤器:https://cloud.google.com/compute/docs/reference/rest/v1/instances/aggregatedList(注意:没有in
操作员。(
我可以使用labels.ansible != ""
作为一种变通方法。完整的动态库存配置供参考(GCP有史以来最糟糕的Ansible文档,所以我知道这对外面的人很有用(:
---
plugin: gcp_compute
projects: PROJECT_NAME_HERE
# auth using "gcloud auth application-default login"...
# you don't need a service account this way
auth_kind: application
keyed_groups:
- key: labels
prefix: label
- key: zone
prefix: zone
# drop any servers without an "ansible" label
# (ie. gke managed nodes)
filters:
- labels.ansible != ""
# hostname shown in inventory
hostnames:
- name
# ssh to hosts using private IP address
compose:
ansible_host: networkInterfaces[0].networkIP
我从来没有使用过这个库存模块,但根据模块页面,过滤器应该是这样的:
filters:
- labels = ansible
可以试试这个:
filters:
- labels.key=value AND labels.key=value AND labels.key=value AND labels.key=value