Building inventory from csv file

24 views
Skip to first unread message

Abdul Rahim

unread,
Nov 13, 2018, 6:36:52 PM11/13/18
to ansible...@googlegroups.com
Hi there , 

I am new to ansible , looking to see if I can import variables from csv file and either build an inventory file or use them in a playbook  , Appreciate your feedback.

Basic playbook I tried:

root@ansibile-launch:~/ansible/tasks/csv-file# cat test2.yml
---
- hosts: localhost
  connection: local
  become: false

  tasks:
  - name: efine Values From CSV File
  set_fact:
    tenant: "{{ lookup('csvfile', ' file=/root/ansible/tasks/csv-file/example.csv delimiter=, col=1') }}
    debug:
      msg: " Print tenant {{tenant}}"
  delegate_to: localhost


root@ansibile-launch:~/ansible/tasks/csv-file# cat example.csv
tenant-name,vrf-name,app-name,bd-name,bd-subnet,epg-name,vlan-id,status_is
tenant_telstra_tenant,vrf_tenant,anp_tenant,bd_net2,172.16.2.252/24,epg_net2,2102,"created,modified"
tenant_telstra_tenant,vrf_tenant,anp_tenant,bd_net3,172.16.3.252/24,epg_net3,2103,"created,modified"
tenant_telstra_tenant,vrf_tenant,anp_tenant,bd_net4,172.16.4.252/24,epg_net4,2104,"created,modified"

Error I am getting


root@ansibile-launch:~/ansible/tasks/csv-file# ansible-playbook test2.yml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

ERROR! Syntax Error while loading YAML.
  did not find expected key

The error appears to have been in '/root/ansible/tasks/csv-file/test2.yml': line 11, column 14, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    debug:
      msg: " Print tenant {{tenant}}"
             ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

Regards,
AR

Mohan L

unread,
Nov 14, 2018, 3:05:22 AM11/14/18
to Ansible Project


  tasks:
  - name: efine Values From CSV File
  set_fact:
    tenant: "{{ lookup('csvfile', ' file=/root/ansible/tasks/csv-file/example.csv delimiter=, col=1') }}
 

Your error seems due to missing closing double quote in the above line.  
Reply all
Reply to author
Forward
0 new messages