I like to use json_query to search a JSON list. This saves "looping" in 
ansible. I find it faster. The result is a list, but you can always select 
your_list[0] for the first one.

  # query json list for data center and region match
  - set_fact:
      my_isilons: "{{ isilon_cluster_output.list | json_query('[?`Data 
Center`==`'+primary_data_center+'` && `Dedicated region` ==`'+region+'`]') | 
list }}"

  # display entire result of query
  - debug:
      msg: "{{ my_isilons }}'

  # display first item if query returned 1 or more
  - debug:
      msg: "{{ my_isilons[0] }}'
    when: my_isilons is iterable and len(my_isilons) > 0

The added benefit of this method is you can now test in later tasks whether the 
query result is interable and has a length greater than zero to determine 
whether you found a match.

  # get cluster name and ip address if query returned 1 or more
  - set_fact:
      isilon_cluster_name_p: "{{ my_isilons[0]['Cluster Name'] }}"
      isilon_ip_address_p: "{{ my_isilons[0]['IP Address'] }}"
    when: my_isilons is iterable and len(my_isilons) > 0

You also can act on more than one item if the json query matched multiple items.

  # do some operation on all the returned results
  - set_fact:
      isilon_cluster_name_p: "{{ item['Cluster Name'] }}"
      isilon_ip_address_p: "{{ item['IP Address'] }}"
    loop: "{{ my_isilons }}"
    when: my_isilons is iterable and len(my_isilons) > 0

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Jan 24, 2023, at 5:38 AM, javed khan <[email protected]> wrote:

Yes, that was same , i also got the perfect list of dictionaries , but it 
giving bad error

On Tuesday, January 24, 2023 at 3:38:37 AM UTC+5:30 
[email protected]<http://gmail.com/> wrote:
That is strange, because I copy-n-pasted you play and data, and it worked for 
me. Does your output before your last step look like mine?

$ ansible-playbook isilon.yml

PLAY [Isilon tests] 
****************************************************************************

TASK [First Read isilon cluster csv file from mount point for login] 
***************************
ok: [localhost]

TASK [Print The inventry Full File in List format] 
*********************************************
ok: [localhost] => {
    "msg": [
        {
            "Business Purpose": "KPHC",
            "Cluster Name": "cnnndcisip-hc01",
            "Data Center": "NDC",
            "Dedicated region": "SC",
            "Environment": "Production",
            "IP Address": "172.19.94.62",
            "Serial No.": "CF2ZJ203800133"
        },
        {
            "Business Purpose": "KPHC",
            "Cluster Name": "",
            "Data Center": "NDC",
            "Dedicated region": "",
            "Environment": "Production",
            "IP Address": "172.19.94.63",
            "Serial No.": "CF2ZJ204300024"
        },
        {
            "Business Purpose": "KPHC",
            "Cluster Name": "",
            "Data Center": "NDC",
            "Dedicated region": "",
            "Environment": "Production",
            "IP Address": "172.19.94.64",
            "Serial No.": "CF2ZJ204300026"
        },
        {
            "Business Purpose": "KPHC",
            "Cluster Name": "massdcisip-hc01",
            "Data Center": "SSDC",
            "Dedicated region": "MA",
            "Environment": "Production",
            "IP Address": "172.30.146.231",
            "Serial No.": "JACNT205160062"
        },
        {
            "Business Purpose": "KPHC",
            "Cluster Name": "",
            "Data Center": "SSDC",
            "Dedicated region": "",
            "Environment": "Production",
            "IP Address": "172.30.146.232",
            "Serial No.": "JACNT210260055"
        },
        {
            "Business Purpose": "KPHC",
            "Cluster Name": "",
            "Data Center": "SSDC",
            "Dedicated region": "",
            "Environment": "Production",
            "IP Address": "172.30.146.233",
            "Serial No.": "JACNT210260073"
        },
        {
            "Business Purpose": "KPHC",
            "Cluster Name": "",
            "Data Center": "SSDC",
            "Dedicated region": "",
            "Environment": "Production",
            "IP Address": "172.30.146.234",
            "Serial No.": "JACNT210260083"
        }
    ]
}

TASK [Get isilon host from isilon cluster file.] 
***********************************************
skipping: [localhost] => (item={'Data Center': 'NDC', 'Environment': 
'Production', 'Business Purpose': 'KPHC', 'Serial No.': 'CF2ZJ203800133', 
'Cluster Name': 'cnnndcisip-hc01', 'Dedicated region': 'SC', 'IP Address': 
'172.19.94.62'})
skipping: [localhost] => (item={'Data Center': 'NDC', 'Environment': 
'Production', 'Business Purpose': 'KPHC', 'Serial No.': 'CF2ZJ204300024', 
'Cluster Name': '', 'Dedicated region': '', 'IP Address': '172.19.94.63'})
skipping: [localhost] => (item={'Data Center': 'NDC', 'Environment': 
'Production', 'Business Purpose': 'KPHC', 'Serial No.': 'CF2ZJ204300026', 
'Cluster Name': '', 'Dedicated region': '', 'IP Address': '172.19.94.64'})
ok: [localhost] => (item={'Data Center': 'SSDC', 'Environment': 'Production', 
'Business Purpose': 'KPHC', 'Serial No.': 'JACNT205160062', 'Cluster Name': 
'massdcisip-hc01', 'Dedicated region': 'MA', 'IP Address': '172.30.146.231'})
skipping: [localhost] => (item={'Data Center': 'SSDC', 'Environment': 
'Production', 'Business Purpose': 'KPHC', 'Serial No.': 'JACNT210260055', 
'Cluster Name': '', 'Dedicated region': '', 'IP Address': '172.30.146.232'})
skipping: [localhost] => (item={'Data Center': 'SSDC', 'Environment': 
'Production', 'Business Purpose': 'KPHC', 'Serial No.': 'JACNT210260073', 
'Cluster Name': '', 'Dedicated region': '', 'IP Address': '172.30.146.233'})
skipping: [localhost] => (item={'Data Center': 'SSDC', 'Environment': 
'Production', 'Business Purpose': 'KPHC', 'Serial No.': 'JACNT210260083', 
'Cluster Name': '', 'Dedicated region': '', 'IP Address': '172.30.146.234'})

TASK [Print The File System Name] 
**************************************************************
ok: [localhost] => {
    "msg": [
        "massdcisip-hc01",
        "172.30.146.231"
    ]
}

PLAY RECAP 
*************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0

On Monday, January 23, 2023 at 9:42:02 AM UTC-5 [email protected] wrote:
Hi Team,

how i will get rid of dictionary or only grab first match in loop output with 
satisfying when condition.



==============================================================
files/isilon-allocation-KPHC.csv
===============================================================
Data Center,Environment,Business Purpose,Serial No.,Cluster Name,Dedicated 
region,IP Address
NDC,Production,KPHC,CF2ZJ203800133,cnnndcisip-hc01,SC,172.19.94.62
NDC,Production,KPHC,CF2ZJ204300024,,,172.19.94.63
NDC,Production,KPHC,CF2ZJ204300026,,,172.19.94.64
SSDC,Production,KPHC,JACNT205160062,massdcisip-hc01,MA,172.30.146.231
SSDC,Production,KPHC,JACNT210260055,,,172.30.146.232
SSDC,Production,KPHC,JACNT210260073,,,172.30.146.233
SSDC,Production,KPHC,JACNT210260083,,,172.30.146.234





===============================================================
default/main.yml
===============================================================
region: MA
primary_data_center: SSDC





===============================================================
task/main.yml
===============================================================

- name: "First Read isilon cluster csv file from mount point for login"
  community.general.read_csv:
    path: "{{ role_path }}/files/isilon-allocation-KPHC.csv"
  register: isilon_cluster_output


- name: "Print The inventry Full File in List format"
  ansible.builtin.debug:
    msg: "{{ isilon_cluster_output.list }}"


- name: "Get isilon host from isilon cluster file."
  ansible.builtin.set_fact:
    isilon_cluster_name_p: "{{ item['Cluster Name'] }}"
    isilon_ip_address_p: "{{ item['IP Address'] }}"
  when: (item['Data Center'] == primary_data_center) and (item['Dedicated 
region'] == region)
  loop: "{{ isilon_cluster_output.list }}"


- name: "Print The File System Name"
  ansible.builtin.debug:
    msg:
      - "{{ isilon_cluster_name_p }}"
      - "{{ isilon_ip_address_p }}"



==========================
Error
==========================


TASK [isilon_allocation_for_KPHP_environment : Get isilon host from isilon 
cluster file.] *************************************
task path: 
/home/ansiblecontroller/Desktop/PROJECT/NAS/roles/isilon_allocation_for_KPHP_environment/tasks/naming_standard_creation.yml:13
fatal: [localhost]: FAILED! => {
    "msg": "The conditional check '(item['Data Center'] == primary_data_center) 
and (item['Dedicated region'] == region)' failed. The error was: error while 
evaluating conditional ((item['Data Center'] == primary_data_center) and 
(item['Dedicated region'] == region)): 'dict object' has no attribute 'Data 
Center'. 'dict object' has no attribute 'Data Center'\n\nThe error appears to 
be in 
'/home/ansiblecontroller/Desktop/PROJECT/NAS/roles/isilon_allocation_for_KPHP_environment/tasks/naming_standard_creation.yml':
 line 13, column 3, but may\nbe elsewhere in the file depending on the exact 
syntax problem.\n\nThe offending line appears to be:\n\n\n- name: \"Get isilon 
host from isilon cluster file.\"\n  ^ here\n"
}


--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e63f1b39-494b-4f28-a256-2bf4509d5c75n%40googlegroups.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2Fe63f1b39-494b-4f28-a256-2bf4509d5c75n%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C1f8b462bb26e4ba6353b08dafdf76a5d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638101536358934866%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=dt0Zw0ML9b%2B5wKXcZbH2G6Gl6rBl2kHJc0Ae6PMF1Ec%3D&reserved=0>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5E4E2BC6-307E-4BC0-87D2-82BC497FF1DD%40nist.gov.

Reply via email to