Hi Tanisha,

Check this one may helpful.

File: DBA_Total_server_list_modified.csv

HOST_NAME
zch89vsoedb106
TX11FPSDB01V
TX11FPSDB02V
tx11fpsdb99v


-----------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------
Playbook
---
- hosts: localhost
  gather_facts: false
  vars:
    host_check: false
  vars_prompt:
    - name: server_name
      prompt: "Enter a Server Name:"
      private: no

  tasks:
     - name: Read CSV file and store that as data
       read_csv:
         path: DBA_Total_server_list_modified.csv
       register: data

     - name: Find out hostname in the list or not
       set_fact:
         host_check: true
       when: item['HOST_NAME'] == server_name
       with_items:
         - "{{ data.list }}"

     - debug:
         msg: "Server name {{ server_name }} is matched with
DBA_Total_server_list_modified.csv"
       when: host_check == true



Output:
*1. When Input is "Testhostname"*

Enter a Server Name:: Testhostname

PLAY [localhost]
*********************************************************************************************************************************************************

TASK [Read CSV file and store that as data]
******************************************************************************************************************************
ok: [localhost]

TASK [Find out host name in the list or not]
*****************************************************************************************************************************
skipping: [localhost] => (item={u'HOST_NAME': u'zch89vsoedb106'})
skipping: [localhost] => (item={u'HOST_NAME': u'TX11FPSDB01V'})
skipping: [localhost] => (item={u'HOST_NAME': u'TX11FPSDB02V'})
skipping: [localhost] => (item={u'HOST_NAME': u'tx11fpsdb99v'})

TASK [debug]
*************************************************************************************************************************************************************
skipping: [localhost]

TASK [debug]
*************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Oops server name not match !!!!!!"
}

PLAY RECAP
***************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

*2. When Input is "TX11FPSDB01V"*

Enter a Server Name:: TX11FPSDB01V

PLAY [localhost]
*********************************************************************************************************************************************************

TASK [Read CSV file and store that as data]
******************************************************************************************************************************
ok: [localhost]

TASK [Find out host name in the list or not]
*****************************************************************************************************************************
skipping: [localhost] => (item={u'HOST_NAME': u'zch89vsoedb106'})
ok: [localhost] => (item={u'HOST_NAME': u'TX11FPSDB01V'})
skipping: [localhost] => (item={u'HOST_NAME': u'TX11FPSDB02V'})

skipping: [localhost] => (item={u'HOST_NAME': u'tx11fpsdb99v'})

TASK [debug]
*************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Server name TX11FPSDB01V is matched with
DBA_Total_server_list_modified.csv"
}

TASK [debug]
*************************************************************************************************************************************************************
skipping: [localhost]

PLAY RECAP
***************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0


-----------------------------------------------------------------------------------------------------------------------------------------------------------------
*Get This read_csv module from*
*https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/files/read_csv.py
<https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/files/read_csv.py>*


*Regards,*
*Indian Velumani*

On Thu, Jun 6, 2019 at 2:06 AM Tanisha Mishra <
[email protected]> wrote:

> Please dont suggest 'lookup' and default ansible modules.
>
>
> On Thursday, June 6, 2019 at 1:58:52 AM UTC+5:30, Tanisha Mishra wrote:
>>
>> I am using csv python module for importing a csv file
>> The csv is importing but unable to fetch the values properly.
>> My main aim is the prompt that takes as server name needs to check from
>> the csv list whether it exists or not...
>> I have to add 'when' condition also, but unable to separate HOST_NAME
>> from actual hostname(s)...
>> I need help for 'when' condition and 'looping' of csv contents..
>>
>> -----------------------------------------------------------------------------------------------------------
>> File: DBA_Total_server_list_modified.csv
>>
>> HOST_NAME
>> zch89vsoedb106
>> TX11FPSDB01V
>> TX11FPSDB02V
>> tx11fpsdb99v
>>
>> -----------------------------------------------------------------------------------------------------------
>>
>>
>> -----------------------------------------------------------------------------------------------------------
>> Playbook
>> ---
>> - name: "Play"
>>   hosts: localhost
>>   vars_prompt:
>>     - name: server_name
>>       prompt: "Enter a Server Name:"
>>       private: no
>>
>>   tasks:
>>
>>     - name: Define values from csv
>>       csv_facts:
>>         src: "./DBA_Total_server_list_modified.csv"
>>
>>     - name: Display imported csv
>>       debug:
>>         var: hostvars[inventory_hostname].csv
>>
>>
>> -----------------------------------------------------------------------------------------------------------
>>
>>
>> -----------------------------------------------------------------------------------------------------------
>> Output
>> TASK [Display imported csv]
>> ***************************************************************
>> ok: [localhost] => {
>>     "hostvars[inventory_hostname].csv": [
>>         {
>>             "HOST_NAME": "zch89vsoedb106"
>>         },
>>         {
>>             "HOST_NAME": "TX11FPSDB01V"
>>         },
>>         {
>>             "HOST_NAME": "TX11FPSDB02V"
>>         },
>>         {
>>             "HOST_NAME": "tx11fpsdb99v"
>>         }
>>     ]
>> }
>>
>> --
> 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 post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/f35ed89b-c484-4ed1-8750-4c32a980d45d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/f35ed89b-c484-4ed1-8750-4c32a980d45d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAAcjD-Vib%2B1rAvnTsVS2cABaA%2BVmkU0Oijew%3D95%2Bz9kzK0MGRw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to