'Ansible looping list of shell commands and passing variables to the command?

I am trying to figure out how to loop of shell commands but the commands contain variables. I am using this link as reference https://github.com/VerosK/ansible-iscsi-target/blob/master/tasks/implementations/lio.yml

The code is to loop thru the list of commands for each disk.

Here is the working code long way:::

---
-
  become: true
  gather_facts: false
  hosts: isci_ansible_centos01
  vars:
    iscsi_target_base: iqn.2017-11.local.centos7
    iscsi_targets:
      - wwn: iqn.2019-8.local.centos7
        disks:
        - name: 'iscsidisk1'
          device: '/dev/sdb'
          vgname: 'vg.iscsi1'
          lvname: 'lv.iscsi1'
          mode: 'block'
        - name: iscsidisk2
          device: /dev/sdc
          vgname: vg.iscsi2
          lvname: lv.iscsi2
          mode: block
  tasks:

    #targetcli '/backstores/block' create scsids1 /dev/vg-iscsi/lv-iscsi
    - name: Create backstore
      shell: targetcli '/backstores/{{ item.1.mode }}' create {{ item.1.name }} /dev/{{ item.1.vgname }}/{{ item.1.lvname }}
      with_subelements:
        - "{{ iscsi_targets }}"
        - disks
      ignore_errors: yes

    #targetcli '/iscsi' create iqn.2017-11.local.centos7:disk1
    - name: Create target
      shell: targetcli '/iscsi' create {{ iscsi_target_base }}:{{item.1.name}}
      with_subelements:
        - "{{ iscsi_targets }}"
        - disks
      ignore_errors: yes

    # targetcli '/iscsi/iqn.2017-11.local.centos7:disk1/tpg1/acls' create iqn.2017-11.local.centos7:node1
    - name: Create nodes
      shell: targetcli '/iscsi/{{ iscsi_target_base }}:{{item.1.name}}/tpg1/acls' create {{ iscsi_target_base }}:node{{item.1.name}}
      with_subelements:
        - "{{ iscsi_targets }}"
        - disks

    # targetcli '/iscsi/iqn.2017-11.local.centos7:disk1/tpg1' set attribute authentication=0 demo_mode_write_protect=0
    - name: Set Authentication
      shell: targetcli '/iscsi/{{ iscsi_target_base }}:{{item.1.name}}/tpg1' set attribute authentication=0 demo_mode_write_protect=0
      with_subelements:
        - "{{ iscsi_targets }}"
        - disks

    # targetcli '/iscsi/iqn.2017-11.local.centos7:disk1/tpg1' set attribute generate_node_acls=1
    - name: Set geenerate_node_acls
      shell: targetcli '/iscsi/{{ iscsi_target_base }}:{{item.1.name}}/tpg1' set attribute generate_node_acls=1
      with_subelements:
        - "{{ iscsi_targets }}"
        - disks

    # targetcli '/iscsi/iqn.2017-11.local.centos7:disk1/tpg1/luns' create /backstores/block/scsids
    - name: Create backing_store
      shell: targetcli '/iscsi/{{ iscsi_target_base }}:{{item.1.name}}/tpg1/luns' create /backstores/block/{{ item.1.name }}
      with_subelements:
        - "{{ iscsi_targets }}"
        - disks

Here is what I want to do is covert it into::: but i think the variable item is getting confused. Is this method supported??? I Just want to know for future reference.

- name: Configure ISCSI using SHELL LOOP
  shell: {{ item }}
  loop:
    - targetcli '/backstores/{{ item.1.mode }}' create {{ item.1.name }} /dev/{{ item.1.vgname }}/{{ item.1.lvname }}
    - targetcli '/iscsi' create {{ iscsi_target_base }}:{{item.1.name}}
    - targetcli '/iscsi/{{ iscsi_target_base }}:{{item.1.name}}/tpg1/acls' create {{ iscsi_target_base }}:node{{item.1.name}}
    - targetcli '/iscsi/{{ iscsi_target_base }}:{{item.1.name}}/tpg1' set attribute authentication=0 demo_mode_write_protect=0
    - targetcli '/iscsi/{{ iscsi_target_base }}:{{item.1.name}}/tpg1' set attribute generate_node_acls=1
    - targetcli '/iscsi/{{ iscsi_target_base }}:{{item.1.name}}/tpg1/luns' create /backstores/block/{{ item.1.name }}
  with_subelements:
     - "{{ iscsi_targets }}"
     - disks

BTW I had to use with_subelements to access items inside iscsi_targets. For some reason it wont let me use with_items and access using {{item.device}}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source