Score:0

Removing brackets ([]) from Ansible string

cn flag

Code:

            - 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

Array:

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

Issue:

 "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 seems to complain about what appears to be double brackets as a result of the split adding additional brackets. For the life of me I cannot figure out how to get rid of them. I usually just add [0] to the end of the variable or json_query, but not sure how to remove the brackets ([]) after a split. Any help is appreciated.

UPDATE: Using loop produces same results:

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

EFFECTIVELY, I need this:

        - 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
br flag
Try `associable_object_types: "{{ item.category_associable_types.0|split(',') }}"`
cn flag
Still only iterates first value (Datastore) but not the rest.
cn flag
Just going to say WOW and apologize for wasting your time. Issue is with the module. The VMware collection has two modules for working with Tag categories. One for gathering/exporting the information and another for setting/importing. Evidently, the export module writes the variables with slightly differing names than what the import expects. So I think I merely have to regex the export variable file to correct the syntax, as I've tested this manually and it worked. I'll post an update once finished. Thanks again for your time, Vladimir.
cn flag
Sure enough, the module stopped iterating after the first value because the second value (exported from a related module) didn't match the expected syntax. Once I resolved that, the imported worked with my original play. There was another 'bug' I ran into. Note in the following link, all associable_object_types parameters are title case, except for 'Library item': https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_category_module.html
Score:2
br flag

Q: "Split adding additional brackets. How to get rid of them."

Short answer: Get rid of the quotes around the item on the list

  "associable_object_types": [['Datastore', 'Opaque Network', ...]]

Details: The title says "Removing brackets ([]) from Ansible string". Because of the quotes, the item on the list associable_object_types is a string. Test it

    - debug:
        var: associable_object_types.0|type_debug

gives

  associable_object_types.0|type_debug: AnsibleUnicode

Because the string is a valid YAML list you can convert the string to list

    - debug:
        var: item|from_yaml
      loop: "{{ associable_object_types }}"

gives (abridged)

  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

This option needs a list of strings. Test it is the loop first, e.g.

  - debug:
      msg: |
        associable_object_types: {{ item.category_associable_types|from_yaml }}
    when: item.category_associable_types|length > 0
    loop: '{{ category_info }}'

Notes:

  • The format of your output is JSON because you (very probably) use the default callback plugin. The JSON standard requires the strings to be double-quoted. The output of my answer is YAML because I use the yaml callback plugin. Try it
shell> ANSIBLE_STDOUT_CALLBACK=yaml  ansible-playbook playbook.yml
  • Example of a complete playbook for testing
- 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 }}"
  • Fix the value of this hash. This should be a list. Try and remove the join filter
  'category_associable_types': item | json_query('category_associable_types') | join (',')

Then, you don't have to split it

  associable_object_types:
    - "{{ item.category_associable_types | split(',') }}"
  • The result will be a list of lists when you split items of a list. For example, given the list
  l1: [a-b-c, d-e-f, g-h-j]

Split the items on the list

  l2: "{{ l1|map('split', '-') }}"

gives the list of lists in YAML

  l2:
    - [a, b, c]
    - [d, e, f]
    - [g, h, j]

, or in JSON

{
    "l2": [
        ["a", "b", "c"],
        ["d", "e", "f"],
        ["g", "h", "j"]
    ]
}

You can flatten the list (get rid of additional brackets)

  l3: "{{ l2|flatten }}"

gives

  l3: [a, b, c, d, e, f, g, h, j]
cn flag
Thanks Vladimir. I had already tried it without join and split, but the module only accepted the first value. I then tried for several hours to figure out how to iterate using loop/nest to no avail. Your suggestion to map(from_yaml) moved the brackets but the module is now complaining it cannot accept value,value,etc. I have verified the module works when the values are on separate lines but I cannot seem to get the data there. For me, associable_object_types|map('from_yaml')|list: produces value,value,etc. and I effectively need it to be exactly as you illustrated under that line above.
cn flag
I ran your playbook and associable_object_types|map('from_yaml')|list: produces exactly as you said and what I need. But AWX seems to be flattening the list for some reason. I'll look into that.
br flag
Try the updated loop.
cn flag
I tried the update loop with same result (please see update in my original post).
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.