When condition and fetching values

22 views
Skip to first unread message

Tanisha Mishra

unread,
Jun 5, 2019, 4:28:52 PM6/5/19
to Ansible Project
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"
        }
    ]
}

Tanisha Mishra

unread,
Jun 5, 2019, 4:36:45 PM6/5/19
to Ansible Project
Please dont suggest 'lookup' and default ansible modules.

Indian Velumani

unread,
Jun 6, 2019, 9:29:42 AM6/6/19
to ansible...@googlegroups.com
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


Regards,
Indian Velumani

--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f35ed89b-c484-4ed1-8750-4c32a980d45d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages