Answers: <blockquote>
  <p><em>Meaning that somehow the square brackets sneak into my variable, which I got from a list.</em></p>
  </blockquote>
  <p>Right, that</p>
  <pre><code>cannot create 'tank/subtank/[mom]': invalid character '[' in name
  </code></pre>
  <p>is the expected behavior according the code snippet you've provided.</p>
  <blockquote>
  <p><em>Any idea on how can I get my variable without the square brackets?</em></p>
  </blockquote>
  <p>To get the list element in your loop you need just to reference it via <code>item</code>.</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: Create the clusters's filesystem
    zfs:
      name: &quot;{{ zfs_pool_name }}/{{ zfs_first_fs }}/{{ item }}&quot;
      state: present
      extra_zfs_properties:
        volsize: &quot;{{ clients_fs_size }}&quot;
    loop: &quot;{{ zfs_clients }}&quot;
    loop_control:
      pause: 5
  </code></pre>
  <p>A  minimal example which shows it more focused</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: Loop over sample list
    debug:
      msg: &quot;{{ item }}&quot;
    loop: ['A', '1', 'B', '2']
    loop_control:
      extended: true
      label: &quot;{{ ansible_loop.index0 }}&quot;
  </code></pre>
  <p>will result into an output of</p>
  <pre class="lang-yaml prettyprint-override"><code>TASK [Loop over sample list] ******
  ok: [localhost] =&gt; (item=0) =&gt;
    msg: A
  ok: [localhost] =&gt; (item=1) =&gt;
    msg: '1'
  ok: [localhost] =&gt; (item=2) =&gt;
    msg: B
  ok: [localhost] =&gt; (item=3) =&gt;
    msg: '2'
  </code></pre>
  <p><strong>Further Documentation</strong></p>
  <ul>
  <li><a href="https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#iterating-over-a-simple-list" rel="nofollow noreferrer">Iterating over a simple list</a></li>
  <li><a href="https://stackoverflow.com/questions/3489071/">In Python, when to use a Dictionary, List or Set?</a>, for some background information</li>
  </ul>
  
Answers: CC BY-SA 4.0
Answers: 2023-02-02T11:17:59.537Z
Answers: 2023-02-02T11:23:25.807Z
Answers: 2023-02-02T11:23:25.807Z
Answers: U880D
Body: <p>I'm writing a role where I need to iterate over a list using the <code>loop</code> option. I'm using the ZFS module <a href="https://github.com/aisbergg/ansible-role-zfs/" rel="nofollow noreferrer">aisbergg.zfs</a>.</p>
  <p>The role:</p>
  <pre class="lang-yaml prettyprint-override"><code>    - hosts: localhost
        vars:
          zfs_pool_name: tank
          zfs_first_fs: subtank
          zfs_clients: 
            - mom
          clients_fs_size: 1T
  
        tasks:
          - name: Create the clusters's filesystem
            zfs:
              name: &quot;{{ zfs_pool_name }}/{{ zfs_first_fs }}/{{ zfs_clients }}&quot;
              state: present
              extra_zfs_properties:
                volsize: &quot;{{ clients_fs_size }}&quot;
            loop: &quot;{{ zfs_clients }}&quot;
            loop_control:
              pause: 5
  </code></pre>
  <p>When I run the playbook I got the following error:</p>
  <blockquote>
  <p>TASK [Create the clusters's filesystem]
  ********************************************************************************************************************** failed: [localhost] (item=mom) =&gt; {&quot;ansible_loop_var&quot;: &quot;item&quot;,
  &quot;changed&quot;: false, &quot;item&quot;: &quot;mom&quot;, &quot;msg&quot;: &quot;cannot create
  'tank/subtank/[mom]': invalid character '[' in name\n&quot;}</p>
  </blockquote>
  <p>Meaning that somehow the square brackets sneak into my variable, which I got from a list.</p>
  <p><strong>Any idea on how can I get my variable without the square brackets?</strong></p>
  
Tags: <ansible>
Title: Why is ansible loop adding square brackets ("[]") into my role?
UserDisplayName: k.Cyborg
ThreadText: # Why is ansible loop adding square brackets ("[]") into my role?
  
  (Asked by: username_0 on 2023-02-02)
  
  I'm writing a role where I need to iterate over a list using the `loop` option. I'm using the ZFS module [aisbergg.zfs](https://github.com/aisbergg/ansible-role-zfs/).
  
  The role:
  
  ```yaml
      - hosts: localhost
        vars:
          zfs_pool_name: tank
          zfs_first_fs: subtank
          zfs_clients: 
            - mom
          clients_fs_size: 1T
  
        tasks:
          - name: Create the clusters's filesystem
            zfs:
              name: "{{ zfs_pool_name }}/{{ zfs_first_fs }}/{{ zfs_clients }}"
              state: present
              extra_zfs_properties:
                volsize: "{{ clients_fs_size }}"
            loop: "{{ zfs_clients }}"
            loop_control:
              pause: 5
  
  ```
  
  When I run the playbook I got the following error:
  
  > TASK [Create the clusters's filesystem]
  > \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* failed: [localhost] (item=mom) => {"ansible\_loop\_var": "item",
  > "changed": false, "item": "mom", "msg": "cannot create
  > 'tank/subtank/[mom]': invalid character '[' in name\n"}
  
  Meaning that somehow the square brackets sneak into my variable, which I got from a list.
  
  **Any idea on how can I get my variable without the square brackets?**
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-02-02. Score: 1 | Accepted answer
  
  > *Meaning that somehow the square brackets sneak into my variable, which I got from a list.*
  
  Right, that
  
  ```
  cannot create 'tank/subtank/[mom]': invalid character '[' in name
  
  ```
  
  is the expected behavior according the code snippet you've provided.
  
  > *Any idea on how can I get my variable without the square brackets?*
  
  To get the list element in your loop you need just to reference it via `item`.
  
  ```yaml
  - name: Create the clusters's filesystem
    zfs:
      name: "{{ zfs_pool_name }}/{{ zfs_first_fs }}/{{ item }}"
      state: present
      extra_zfs_properties:
        volsize: "{{ clients_fs_size }}"
    loop: "{{ zfs_clients }}"
    loop_control:
      pause: 5
  
  ```
  
  A minimal example which shows it more focused
  
  ```yaml
  - name: Loop over sample list
    debug:
      msg: "{{ item }}"
    loop: ['A', '1', 'B', '2']
    loop_control:
      extended: true
      label: "{{ ansible_loop.index0 }}"
  
  ```
  
  will result into an output of
  
  ```yaml
  TASK [Loop over sample list] ******
  ok: [localhost] => (item=0) =>
    msg: A
  ok: [localhost] => (item=1) =>
    msg: '1'
  ok: [localhost] => (item=2) =>
    msg: B
  ok: [localhost] => (item=3) =>
    msg: '2'
  
  ```
  
  **Further Documentation**
  
  * [Iterating over a simple list](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#iterating-over-a-simple-list)
  * [In Python, when to use a Dictionary, List or Set?](https://stackoverflow.com/questions/3489071/), for some background information
  
  
  ---
  
  