I have this list from a shell output.
- 192.168.20.76:3260,1054 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.21.126:3260,1056 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.21.125:3260,1055 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.20.75:3260,1053 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.22.20:3260,1052 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.22.19:3260,1051 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
I want to get both the IP and the iqn.xxxxx from the list when the IP is in the same vlan as the host.
I am able to create a list of the correct IP's but can't figure out how to also get the corresponding iqn.
Code:
- hosts: test_server
gather_facts: yes
tasks:
- name:
set_facts:
iscsi_vlan: '192.168.20.64/28'
- name: iscsiadm discovery
command: /usr/sbin/iscsiadm iscsiadm --mode discovery --type sendtargets --portal {{ storage_fqdn }}
register: iscsiadm_out
- name: facts
set_fact:
target_list: "{{ target_list + item | regex_findall('(^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})') | ipaddr( iscsi_vlan ) }}"
loop: "{{ iscsiadm_out.stdout_lines }}"
- name: target_list
debug:
var: target_list
target_list contains this. It matches the correct subnet using the ip_addr function.
ok: [test-server] =>
target_list:
- 192.168.20.76
- 192.168.20.75
Basically I end up with trying to search in a list with the contents of another list.
How can I either use this list to extract the iqn from iscsiadm_out or maybe in a totally different way?
I understand I can use commands like grep and awk in the command module, but I don't like that option.
In my testing I have searched around here and elswhere on the Internet for hours. I'm a novice at Ansible and a total noob at python and jinja2, but I tried to investigate those paths too to no avail.
There are other ways to acheive the same result but you can build-up on the below concepts. I'm simply extracting your expected info at once in a dict list (each dict containing an ip/iqn pair) and looping over it to filter out the expected elements.
The demo playbook:
---
- name: Extract IP and IQN
hosts: localhost
gather_facts: false
vars:
# This is what you get from your command
iscsiadm:
stdout_lines:
- 192.168.20.76:3260,1054 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.21.126:3260,1056 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.21.125:3260,1055 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.20.75:3260,1053 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.22.20:3260,1052 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
- 192.168.22.19:3260,1051 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6
iscsi_vlan: '192.168.20.64/28'
tasks:
- name: "Create a list of {ip: x, iqn: y} dicts from the command result"
vars:
current_entry:
- ip: >-
{{ item | regex_replace('(^(\d{1,3}\.){3}\d{1,3}).*$', '\g<1>') }}
iqn: >-
{{ item | regex_replace('^.* (iqn\.\d*-\d*.com.netapp).*$', '\g<1>') }}
set_fact:
ipqn_list: "{{ ipqn_list | default([]) + current_entry }}"
loop: "{{ iscsiadm.stdout_lines }}"
- name: Debug element when ip is in our subnet (change to whatever is relevant)
debug:
msg: "IP {{ item.ip }} with IQN {{ item.iqn }} is in our subnet"
loop: "{{ ipqn_list }}"
when: item.ip | ipaddr(iscsi_vlan)
gives the result:
PLAY [Extract IP and IQN] **************************************************************************************************************************************************************************************************************
TASK [Create a list of {ip: x, iqn: y} dicts from the command result] ******************************************************************************************************************************************************************
ok: [localhost] => (item=192.168.20.76:3260,1054 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6)
ok: [localhost] => (item=192.168.21.126:3260,1056 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6)
ok: [localhost] => (item=192.168.21.125:3260,1055 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6)
ok: [localhost] => (item=192.168.20.75:3260,1053 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6)
ok: [localhost] => (item=192.168.22.20:3260,1052 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6)
ok: [localhost] => (item=192.168.22.19:3260,1051 iqn.1992-08.com.netapp:sn.f4bc76f43e2611e883e200a098d2174a:vs.6)
TASK [Debug element when ip is in our subnet (change to whatever is relevant)] *********************************************************************************************************************************************************
ok: [localhost] => (item={'ip': '192.168.20.76', 'iqn': 'iqn.1992-08.com.netapp'}) => {
"msg": "IP 192.168.20.76 with IQN iqn.1992-08.com.netapp is in our subnet"
}
skipping: [localhost] => (item={'ip': '192.168.21.126', 'iqn': 'iqn.1992-08.com.netapp'})
skipping: [localhost] => (item={'ip': '192.168.21.125', 'iqn': 'iqn.1992-08.com.netapp'})
ok: [localhost] => (item={'ip': '192.168.20.75', 'iqn': 'iqn.1992-08.com.netapp'}) => {
"msg": "IP 192.168.20.75 with IQN iqn.1992-08.com.netapp is in our subnet"
}
skipping: [localhost] => (item={'ip': '192.168.22.20', 'iqn': 'iqn.1992-08.com.netapp'})
skipping: [localhost] => (item={'ip': '192.168.22.19', 'iqn': 'iqn.1992-08.com.netapp'})
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With