1

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.

The role:

    - 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?

1 Answer 1

1

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.

- 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

- 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.