I assume that search.yml corresponds to your include 03-search-profile.yml?
Since you have already moved the search to a separate yml file, you can include it multiple times, setting the variable serial_nbr differently.
In list.yml you can define the following tasks:
- read_csv:
path: /home/ansible/host_list.csv
key: serialnumber
register: vms
delegate_to: localhost
- name: List Of Task
include_tasks: 03-search-profile.yml
vars:
serial_nbr: 2J201024GR
loop: " {{ vms.dict|dict2items }}"
- debug:
msg: "Found! Server {{ server_name }} for serial {{ SN }}"
when: server_found
- name: List Of Task
include_tasks: 03-search-profile.yml
vars:
serial_nbr: something_unknown
loop: " {{ vms.dict|dict2items }}"
- debug:
msg: "Found! Server {{ server_name }} for serial {{ SN }}"
when: server_found
- debug:
msg: "Server NOT found!"
when: not server_found
The file 03-search-profile.yml (search.yml?) you could extend as follows. I added a variable server_found, which is first set to no, if the serial number is found, it is set to yes. This makes it easier to query later.
- name: Set server_found to no
set_fact:
server_found: no
- name: List Of Hardware Info
no_log: True
set_fact:
server_name: "{{ item.name }}"
SN: "{{ item.serialNumber }}"
profileuri: "{{ item.serverProfileUri }}"
serverHardwareUri : "{{ item.uri }}"
server_found: yes
with_items: "{{ server_hardware_result.json.members | json_query('[*]') }}"
when: "serial_nbr in item.serialNumber"
- name: Collection of Variables
debug:
msg: >
The Name is {{ server_name }}, The serialnumber is {{ SN }},
Serverhardware uri is {{ serverHardwareUri }} and
serverprofileuri is {{ profileuri }}
I hope this helps you with your problem.
Note: In your debug task you have a very long string with Jinja expressions. If you write this as one line, as you did, you have to use quotes. Alternatively, you can write this using folded notation with >.
Addition:
If you read your CSV file with read_csv you get a dict about the serial numbers, with { vms.dict | list }} you get a list of all your serial numbers you have read.
However, I'm not sure what exactly your goal is, and what you wanted to achieve with your loop. Also, you don't write anything about the variable server_hardware_result.
Assuming that the variable server_hardware_result looks like Vladimir Botka mentioned, here are some examples of how you can work with the data:
Data structure of server_hardware_result
server_hardware_result:
json:
members:
- name: alice
serialNumber: 2J201024GR
serverProfileUri: 10.1.0.11/profile/uri
uri: 10.1.0.11/uri
- name: bob
serialNumber: 2J201012GR
serverProfileUri: 10.1.0.12/profile/uri
uri: 10.1.0.12/uri
- name: eve
serialNumber: 2J201032GR
serverProfileUri: 10.1.0.13/profile/uri
uri: 10.1.0.13/uri
- name: mallory
serialNumber: 2J201008GR
serverProfileUri: 10.1.0.14/profile/uri
uri: 10.1.0.14/uri
Content of host_list.csv
serialnumber
2J201024GR
2J201012GR
2J201048GR
2J201032GR
2J201008GR
2J201009GR
A set of tasks to show examples in handling the data:
- read_csv:
path: host_list.csv
key: serialnumber
register: vms
delegate_to: localhost
- name: Data structure of vms
debug:
var: vms
- name: Read serials as a list
debug:
msg: "All serials from csv file: {{ vms.dict | list }}"
- name: Make dict by serial from server_hardware_result members
set_fact:
server_hardware_dict: "{{ dict( keys | zip(members) ) }}"
vars:
members: "{{ server_hardware_result.json.members }}"
keys: "{{ members | map(attribute='serialNumber') }}"
- name: Data structure of server_hardware_dict
debug:
var: server_hardware_dict
- name: Check if csv serials are in server_hardware_dict
debug:
msg: "Is serial {{ item }} in server_hardware_dict? {{ item in server_hardware_dict }}"
with_items:
- "{{ vms.dict | list }}"
- name: Print name of found server_hardware
debug:
msg: "{{ server_hardware_dict[item].name }}"
when: item in server_hardware_dict
with_items:
- "{{ vms.dict | list }}"
Results and Explanation
Task read_csv should be clear.
TASK [read_csv] **************************************************************
ok: [localhost]
The data structure of vms registered from read_csv
TASK [Data structure of vms] *************************************************
ok: [localhost] => {
"vms": {
"changed": false,
"dict": {
"2J201008GR": {
"serialnumber": "2J201008GR"
},
"2J201009GR": {
"serialnumber": "2J201009GR"
},
"2J201012GR": {
"serialnumber": "2J201012GR"
},
"2J201024GR": {
"serialnumber": "2J201024GR"
},
"2J201032GR": {
"serialnumber": "2J201032GR"
},
"2J201048GR": {
"serialnumber": "2J201048GR"
}
},
"failed": false,
"list": []
}
}
{{ vms.dict | list }} delivers you a list of all keys from the dict.
TASK [Read serials as a list] ************************************************
ok: [localhost] => {
"msg": "All serials from csv file: ['2J201024GR', '2J201012GR', '2J201048GR', '2J201032GR', '2J201008GR', '2J201009GR']"
}
server_hardware_result.json.members is a list, but you can transform it into a dict, keyed by serialNumber.
TASK [Make dict by serial from server_hardware_result members] ***************
ok: [localhost]
After creating the server_hardware_dict, the whole thing looks like follows. Now you can simple work with it by using the serial number.
TASK [Data structure of server_hardware_dict] ********************************
ok: [localhost] => {
"server_hardware_dict": {
"2J201008GR": {
"name": "mallory",
"serialNumber": "2J201008GR",
"serverProfileUri": "10.1.0.14/profile/uri",
"uri": "10.1.0.14/uri"
},
"2J201012GR": {
"name": "bob",
"serialNumber": "2J201012GR",
"serverProfileUri": "10.1.0.12/profile/uri",
"uri": "10.1.0.12/uri"
},
"2J201024GR": {
"name": "alice",
"serialNumber": "2J201024GR",
"serverProfileUri": "10.1.0.11/profile/uri",
"uri": "10.1.0.11/uri"
},
"2J201032GR": {
"name": "eve",
"serialNumber": "2J201032GR",
"serverProfileUri": "10.1.0.13/profile/uri",
"uri": "10.1.0.13/uri"
}
}
}
{{ item in server_hardware_dict }} returns True or False, depending on whether the serial number is in server_hardware_dict or not.
TASK [Check if csv serials are in server_hardware_dict] **********************
ok: [localhost] => (item=2J201024GR) => {
"msg": "Is serial 2J201024GR in server_hardware_dict? True"
}
ok: [localhost] => (item=2J201012GR) => {
"msg": "Is serial 2J201012GR in server_hardware_dict? True"
}
ok: [localhost] => (item=2J201048GR) => {
"msg": "Is serial 2J201048GR in server_hardware_dict? False"
}
ok: [localhost] => (item=2J201032GR) => {
"msg": "Is serial 2J201032GR in server_hardware_dict? True"
}
ok: [localhost] => (item=2J201008GR) => {
"msg": "Is serial 2J201008GR in server_hardware_dict? True"
}
ok: [localhost] => (item=2J201009GR) => {
"msg": "Is serial 2J201009GR in server_hardware_dict? False"
}
You can print out name of all serials, read from csv, but skip that one, which are not in server_hardware_dict, work with with_items and when.
TASK [Print name of found server_hardware] ***********************************
ok: [localhost] => (item=2J201024GR) => {
"msg": "alice"
}
ok: [localhost] => (item=2J201012GR) => {
"msg": "bob"
}
skipping: [localhost] => (item=2J201048GR)
ok: [localhost] => (item=2J201032GR) => {
"msg": "eve"
}
ok: [localhost] => (item=2J201008GR) => {
"msg": "mallory"
}
skipping: [localhost] => (item=2J201009GR)
I hope that my examples will help you.