Reputation: 479
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}}
Upvotes: 1
Views: 2285
Reputation: 67994
- shell: "{{ item }}"
loop:
- "AAA {{ item.1.name }} {{ item.1.device }}"
- "BBB {{ item.1.name }} {{ item.1.device }}"
with_subelements:
- "{{ targets }}"
- disks
Q: "Is this method supported?"
A: No. It's not possible to put 2 loops into one task.
Instead, it is possible to create a file with commands
shell> cat commands.yml
- debug:
msg: "{{ cmd_item }}"
loop:
- "AAA {{ item.1.name }} {{ item.1.device }}"
- "BBB {{ item.1.name }} {{ item.1.device }}"
loop_control:
loop_var: cmd_item
and iterate(loop) include_tasks. The play below
- hosts: localhost
vars:
targets:
- wwn: 2019.centos7
disks:
- name: 'disk1'
device: '/dev/sdb'
- name: 'disk2'
device: '/dev/sdc'
tasks:
- include_tasks: commands.yml
loop: "{{ targets|subelements('disks') }}"
give
"msg": "AAA disk1 /dev/sdb"
"msg": "BBB disk1 /dev/sdb"
"msg": "AAA disk2 /dev/sdc"
"msg": "BBB disk2 /dev/sdc"
Upvotes: 1