代碼:
- name: Set category info as fact
set_fact:
category_info: "{{ category_info|default([]) + [ {
'category_name': item | json_query('category_name'),
'category_description': item | json_query('category_description'),
'category_cardinality': item | json_query('category_cardinality'),
'category_associable_types': item | json_query('category_associable_types') | join (',')
} ] }}"
with_items: '{{ tag_category_jsondata.tag_category_info }}'
register: result_tag_category_info
---
- name: Create tag categories from source variables
community.vmware.vmware_category:
validate_certs: false
hostname: "{{ vc_server }}"
username: "{{ vc_user }}"
password: "{{ vc_pass }}"
category_name: '{{ item.category_name }}'
category_description: '{{ item.category_description }}'
category_cardinality: '{{ item.category_cardinality | lower }}'
associable_object_types:
- "{{ item.category_associable_types | split(',') }}"
state: present
delegate_to: localhost
when: item.category_associable_types|length > 0
with_items: '{{ category_info }}'
register: result_import_tag_category
大批:
"category_associable_types": [
"Datastore",
"Opaque Network",
"Storage Pod",
"Virtual App",
"Vmware Distributed Virtual Switch",
"Distributed Virtual Switch",
"Host Network",
"Network",
"Cluster Compute Resource",
"Distributed Virtual Portgroup",
"Folder",
"Host System",
"Resource Pool",
"Virtual Machine",
"Datacenter"
],
問題:
"associable_object_types": [
"['Datastore', 'Opaque Network', 'Storage Pod', 'Virtual App', 'Vmware Distributed Virtual Switch', 'Distributed Virtual Switch', 'Host Network', 'Network', 'Cluster Compute Resource', 'Distributed Virtual Portgroup', 'Folder', 'Host System', 'Resource Pool', 'Virtual Machine', 'Datacenter']"
],
Ansible 似乎抱怨由於拆分添加了額外的括號而出現了雙括號。我一生都不知道如何擺脫它們。我通常只是將 [0] 添加到變數或 json_query 的末尾,但不確定如何在拆分後刪除括號 ([])。任何幫助表示讚賞。
更新:使用循環產生相同的結果:
"msg": "value of associable_object_types must be one or more of: All objects, Cluster, Content Library, Datacenter, Datastore, Datastore Cluster, Distributed Port Group, Distributed Switch, Folder, Host, Library item, Network, Host Network, Opaque Network, Resource Pool, vApp, Virtual Machine. Got no match for: Datastore,Opaque Network,Storage Pod,Virtual App,Vmware Distributed Virtual Switch,Distributed Virtual Switch,Host Network,Network,Cluster Compute Resource,Distributed Virtual Portgroup,Folder,Host System,Resource Pool,Virtual Machine,Datacenter",
"invocation": {
"module_args": {
"validate_certs": false,
"hostname": "vcenter-t430.brilliantitsolutions.com",
"username": "[email protected]",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"category_name": "Ansible",
"category_description": "",
"category_cardinality": "single",
"associable_object_types": [
"Datastore,Opaque Network,Storage Pod,Virtual App,Vmware Distributed Virtual Switch,Distributed Virtual Switch,Host Network,Network,Cluster Compute Resource,Distributed Virtual Portgroup,Folder,Host System,Resource Pool,Virtual Machine,Datacenter"
],
實際上,我需要這個:
- name: Create tag categories from source variables
community.vmware.vmware_category:
validate_certs: false
hostname: "{{ vc_server }}"
username: "{{ vc_user }}"
password: "{{ vc_pass }}"
category_name: '{{ item.category_name }}'
category_description: '{{ item.category_description }}'
category_cardinality: '{{ item.category_cardinality | lower }}'
associable_object_types:
- Datastore
- Opaque Network
- Storage Pod
- Virtual App
- Vmware Distributed Virtual Switch
- Distributed Virtual Switch
- Host Network
- Network
- Cluster Compute Resource
- Distributed Virtual Portgroup
- Folder
- Host System
- Resource Pool
- Virtual Machine
- Datacenter
state: present
答案1
問:“拆分添加額外的括號。如何擺脫它們。”
簡短回答:去掉清單中項目周圍的引號
"associable_object_types": [['Datastore', 'Opaque Network', ...]]
詳細資訊:標題為「從 Ansible 字串中刪除括號 ([])」。由於引號,清單中的項目關聯物件類型是一個字串。測試一下
- debug:
var: associable_object_types.0|type_debug
給出
associable_object_types.0|type_debug: AnsibleUnicode
因為該字串是有效的 YAML 列表,所以您可以將字串轉換為列表
- debug:
var: item|from_yaml
loop: "{{ associable_object_types }}"
給出(刪節)
item|from_yaml:
- Datastore
- Opaque Network
- Storage Pod
- Virtual App
- Vmware Distributed Virtual Switch
- Distributed Virtual Switch
- Host Network
- Network
- Cluster Compute Resource
- Distributed Virtual Portgroup
- Folder
- Host System
- Resource Pool
- Virtual Machine
- Datacenter
此選項需要一個字串列表。首先測試它是循環,例如
- debug:
msg: |
associable_object_types: {{ item.category_associable_types|from_yaml }}
when: item.category_associable_types|length > 0
loop: '{{ category_info }}'
筆記:
- 您的輸出格式是 JSON,因為您(很可能)使用預設回調插件。 JSON 標準要求字串用雙引號引起來。我的答案的輸出是 YAML 因為我使用yaml回調插件。嘗試一下
shell> ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook playbook.yml
- 用於測試的完整劇本範例
- hosts: localhost
vars:
l1: [a-b-c, d-e-f, g-h-j]
l2: "{{ l1|map('split', '-') }}"
l3: "{{ l2|flatten }}"
associable_object_types: ["['Datastore', 'Opaque Network', 'Storage Pod', 'Virtual App', 'Vmware Distributed Virtual Switch', 'Distributed Virtual Switch', 'Host Network', 'Network', 'Cluster Compute Resource', 'Distributed Virtual Portgroup', 'Folder', 'Host System', 'Resource Pool', 'Virtual Machine', 'Datacenter']"]
tasks:
- debug:
var: l1|to_yaml
- debug:
var: l2|to_yaml
- debug:
var: l2
- debug:
var: l3|to_yaml
- debug:
var: associable_object_types
- debug:
var: associable_object_types.0|type_debug
- debug:
var: item|from_yaml
loop: "{{ associable_object_types }}"
- 修復此哈希值。這應該是一個清單。嘗試刪除加入篩選
'category_associable_types': item | json_query('category_associable_types') | join (',')
然後,你就不必分割它
associable_object_types:
- "{{ item.category_associable_types | split(',') }}"
- 當您拆分清單的項目時,結果將是清單的清單。例如,給定列表
l1: [a-b-c, d-e-f, g-h-j]
拆分清單中的項目
l2: "{{ l1|map('split', '-') }}"
給出 YAML 中的列表列表
l2:
- [a, b, c]
- [d, e, f]
- [g, h, j]
,或 JSON 格式
{
"l2": [
["a", "b", "c"],
["d", "e", "f"],
["g", "h", "j"]
]
}
你可以壓扁列表(去掉額外的括號)
l3: "{{ l2|flatten }}"
給出
l3: [a, b, c, d, e, f, g, h, j]