I am gathering data using a Netapp module in Ansible. Thereafter using Python Inline Code to apply an operation on that list. Using that Python operation I try to find specific string values in a list. I am using the command
module to execute Python code inline.
---
- hosts: exec-node
collections:
- netapp.ontap
vars_files:
- secretvars.yaml
tasks:
- name: Gather volume info
tags: vol
netapp.ontap.na_ontap_rest_info:
gather_subset:
- storage/volumes
hostname: "nas3.test.com"
username: "{{ username }}"
password: "{{ password }}"
https: true
validate_certs: false
register: result
- debug: var=result['ontap_info']['storage/volumes']['records']
tags: vol
- name: create volume list
tags: vol
set_fact:
volume_list: "{{ volume_list|default([]) + [item.name] }}"
loop: "{{result['ontap_info']['storage/volumes']['records']}}"
- debug: var=volume_list
tags: vol
- name: Python inline code
command: python3
args:
stdin: |
finallist="{{ volume_list }}"
finallist1=[]
for i in finallist:
if i[0:2]=='yz':
finallist1.append(i)
print(finallist1)
register: results
- set_fact:
x: "{{ results.stdout }}"
I am expecting a list (finallist1
) of my required data like below.
['yz16', 'yz18', 'yz11', 'yz13', 'yz14', 'yz17', 'yz15', 'yz32']
My raw list (finallist
) is as below:
['yz16', 'yz18', 'yz11', 'yz13', 'yz14', 'yz17', 'yz15', 'yz32', 'test', 'test1']