Coder Social home page Coder Social logo

norjuliana / ansible-role-includecsv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mkouhei/ansible-role-includecsv

0.0 3.0 0.0 22 KB

Load variables from a CSV file, dynamically within a task.

License: GNU General Public License v3.0

Python 100.00%

ansible-role-includecsv's Introduction

Ansible Role: include_csv

This role contains no tasks, but provides the include_csv module which loads data from a CSV file.

Ansible Galaxy Page: https://galaxy.ansible.com/mkouhei/include_csv/

install

$ virtualenv /path/to/venv
$ /path/to/venv/bin/pip install ansible
$ cd /path/to/your_playbook
$ /path/to/venv/bin/ansible-galaxy install -p ./library mkouhei.include_csv

usages

Path -M library/mkouhei.include_csv/library option to ansible or ansible-playbook command.

include_csv module

Example task:

- include_csv: src=path/to/fruits.csv

The first line of the CSV file must be a header containing a list of field names, for example:

id,name,price
0,apple,100
1,banana,200
2,cherry,300
3,durian,400
  • include_csv reads the data into a list of dictionaries, one dictionary per line.
  • The dictionary keys are the field names.
  • The data is stored in a variable whose name is the CSV file name without the extension.

For example, the data for the above CSV file can be accessed as follows:

- debug: msg="{{ fruits }}"
ok: [localhost] => {
  "msg": "[{u'price': u'100', u'id': u'0', u'name': u'apple'}, {u'price': u'200', u'id': u'1', u'name': u'banana'}, {u'price': u'300', u'id': u'2', u'name': u'cherry'}, {u'price': u'400', u'id': u'3', u'name': u'durian'}]"
  }

Options

parameter required default choices comments
src yes Specify the CSV file path. The path can be absolute or relative. (Detection of files under roles/foo/files is not supported.)
delimiter no ' Single-character field separator.
quotechar no " Single-character quote character.

Examples

Use include_vars as follows:

- include_csv: src=foo.csv
- include_csv: src=bar.csv delimiter="|" quotechar="'"

Load locally

In the previous examples the CSV file is loaded on every remote node targeted by the play.

To load CSV data from a file on the Ansible control server, use local_action or connection: local, for example:

- include:csv: src=bar.csv
  connection: local
  sudo: no

Load locally, only once

There are two potential problems with the previous example (depending on the desired behaviour):

  • the local file is loaded multiple times; once for each target node
  • ansible-playbook output will appear to show the task running against every target node

To load CSV data only once from a file on the control server, use a separate play within your playbook which targets localhost. Subsequent plays can access the data via hostvars, for example:

- hosts: localhost
  roles:
    - role: mkouhei.include_csv
  tasks:
    - include_csv: src=fruits.csv

- hosts: t1, t2
  tasks:
  - debug: msg="{{item.name}}"
    with_items: "{{hostvars.localhost.fruits}}"

Requirements

None.

Role variables

None.

Dependencies

None.

License

GPLv3

Author Information

Kouhei Maeda

ansible-role-includecsv's People

Contributors

mkouhei avatar lqueryvg avatar

Watchers

James Cloos avatar Norjuliana avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.