CSV item validation through loop

34 views
Skip to first unread message

Rakesh Parida

unread,
Jul 1, 2019, 9:14:44 AM7/1/19
to Ansible Project

Hi,

I have a csv file which has all the ip address and the mac address in column (as shown below):

VM1 TBD x.x.x.x 02:07:C0:A8:06:04  x1.x1.x1.x1  02:07:0A:AE:CC:54  x2.x2.x2.x2 02:07:0A:91:10:BB  x3.x3.x3.x3  02:07:AC:10:04:03
VM2 TBD y.y.y.y 02:07:C0:A8:06:05  y1.y1.y1.y1   02:07:0A:AE:CC:55  y2.y2.y2.y202:07:0A:91:10:BC  y.3.y3.y3.y3 02:07:AC:10:04:04
VM3 TBD z.z.z.z 02:07:C0:A8:06:06  z1.z1.z1.z1  02:07:0A:AE:CC:56  z2.z2.z2.z2 02:07:0A:91:10:BD  z3.z3.z3.z3 02:07:AC:10:04:05

Now i want to check the ip and mac combinations of mentioned in CSV, My playbook is as follows. Want to iterate through all the ip and mac combination and display.
but my iteration is unsuccessful. Pls help

- hosts: all
  tasks:
    - name: All IPV4 address in system
      setup: filter="ansible_all_ipv4_addresses"
      register: all_ipv4_address
    - debug:
        var: all_ipv4_address

    - name: Ip vs Mac Inside CSV
      debug:
        msg: "The MAC address of  {{ item }} is {{ lookup('csvfile', '{{ item }} file={{ inventory_data }} delimiter=,') }}"
      with_items:
        "{{ all_ipv4_address }}"

Tanisha Mishra

unread,
Jul 1, 2019, 10:55:07 AM7/1/19
to Ansible Project
Can you share the error ?

Rakesh Parida

unread,
Jul 1, 2019, 11:45:55 PM7/1/19
to Ansible Project
Error as below(Removed actual ips): I guess the loop is not iterated as desired from CSV items. Pls help

sudo ansible-playbook mac-test1.yml -D -C --limit VM[1] -i ../../lab-inventories/LAB2/hosts

PLAY [all] **************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [VM1]

TASK [All IPV4 address in system] ***************************************************************************************************
ok: [VM1]

TASK [debug] ************************************************************************************************************************
ok: [VM1] => {
    "all_ipv4_address": {
        "ansible_facts": {
            "ansible_all_ipv4_addresses": [
                "x.x.x.x", 
                "y.y.y.y", 
                "z.z.z.z"
            ]
        }, 
        "changed": false, 
        "failed": false
    }
}

TASK [Ip vs Mac Inside CSV] *********************************************************************************************************
ok: [VM1] => (item=failed) => {
    "msg": "The MAC address of  failed is []"
}
ok: [VM1] => (item=changed) => {
    "msg": "The MAC address of  changed is []"
}
ok: [VM1] => (item=ansible_facts) => {
    "msg": "The MAC address of  ansible_facts is []"
}

PLAY RECAP **************************************************************************************************************************
VM1                   : ok=4    changed=0    unreachable=0    failed=0   


With Regards
Rakesh

Rakesh Parida

unread,
Jul 2, 2019, 12:00:01 AM7/2/19
to Ansible Project
Want to iterate through the items and print teh corressponding mac address.


On Tuesday, July 2, 2019 at 9:15:55 AM UTC+5:30, Rakesh Parida wrote:
Error as below(Removed actual ips): I guess the loop is not iterated as desired from CSV items. Pls help

sudo ansible-playbook mac-test1.yml -D -C --limit VM[1] -i ../../lab-inventories/LAB2/hosts

PLAY [all] **************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [VM1]

TASK [All IPV4 address in system] ***************************************************************************************************
ok: [VM1]

TASK [debug] ************************************************************************************************************************
ok: [VM1] => {
    "all_ipv4_address": {
        "ansible_facts": {
            "ansible_all_ipv4_addresses": [
                "x.x.x.x", 
                "x1.x1.x1.x1", 
                "x2.x2.x2.x2"
Reply all
Reply to author
Forward
0 new messages