set, read, and use vars from csv or csv2yml file

26 views
Skip to first unread message

gerard moisset

unread,
Aug 4, 2019, 10:13:31 AM8/4/19
to Ansible Project
i'm trying to write playbook to downlaod packages from repo.

here is my try.

csv data
===========================

cat {{ansibleDir}}/temp/csv_data:

module;goroco;repo
tzdata-26d;G01R01C01;dnc
ansible-26d;G00R00C00;dnc-unstable
webadmi-26d;G01R01C02;dnc-unstable


csv2yml file python script
============================

cat {{ansibleDir}}/csv2yml.py:


#!/usr/bin/env python

import csv
import sys
import yaml

csv_data = []
with open(sys.argv[1]) as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        csv_data.append(row)

with open(sys.argv[1] + '.yml', 'w') as outfile:
    outfile.write(yaml.dump({'csv_data': csv_data}))



ymlFromCsv file
================================

cat {{ansibleDir}}/temp/csv_data.yml:

csv_data:
- {module;goroco;repo: tzdata-26d;G01R01C01;dnc}
- {module;goroco;repo: ansible-26d;G00R00C00;dnc-unstable}
- {module;goroco;repo: webadmi-26d;G01R01C02;dnc-unstable}


playbook
=================================

try do downlaod package from repo


---
- hosts: localhost
  connection: local
  become: false
  gather_facts: false


  vars_files:
    - /home/ansible/temp/csv_data.yml
  tasks:
    - name: parse csv and make vars file
      command: "python /home/ansible/scripts/csv2yml.py /home/ansible/temp/csv_data"
    - debug: var=csv_data



    - name: define vars
      include_vars:
        file: "/home/ansible/temp/csv_data.yml"
       

missing task
====================================

       
       
       
i need task to be defined either from csv_data, or csv_data.yml, iterating on,

curl -v --remote-name --proxy {{ proxy }} --proxy-user {{ credentials }} {{ downloadUrl }}/{{ delivery.repo }}/{{ delivery.module }}/{{ delivery.goroco }}/PL-{{ delivery.module }}-{{delivery.goroco }}.tar

but i've not yet found the way to reuse my vars.

any help is welcome.


Andrew Feller

unread,
Aug 4, 2019, 11:40:08 AM8/4/19
to ansible...@googlegroups.com
Consider creating a custom vars plugin
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/822689ba-6059-4401-b500-ddf3c1ee7b98%40googlegroups.com.

Dan Linder

unread,
Aug 4, 2019, 1:08:25 PM8/4/19
to Ansible Project
What is the exact error message you're getting?

I only slightly agree with Mr. Fellers response ("create a custom vars plugin"), but in your case that might be overkill.  I've seen teams that have a developer write some custom code that breaks soon after he/she leaves and then they are stuck with a bigger mess than if they had tried to work with Ansible included modules.

Andrew Feller

unread,
Aug 4, 2019, 1:22:49 PM8/4/19
to ansible...@googlegroups.com
If you’re having to write Python code already, you’re most of the way to having a custom plugin.  You’re simply doing it in a way that natively hooks into how Ansible works.

Sent from my iPhone
--
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.
Reply all
Reply to author
Forward
0 new messages