从 Ansible 字符串中删除括号([])

从 Ansible 字符串中删除括号([])

代码:

            - 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 似乎抱怨由于拆分添加了额外的括号而出现了双括号。我终其一生都无法弄清楚如何摆脱它们。我通常只是在变量或 json_query 的末尾添加 [0],但不确定如何在拆分后删除括号 ([])。任何帮助都值得感激。

更新:使用循环产生相同的结果:

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

相关内容