Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
671b0454
Commit
671b0454
authored
Jan 14, 2014
by
jctanner
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4987 from jpmens/lookup-csvfile
New lookup plugin csvreader
parents
9ed379b7
252a51be
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
0 deletions
+82
-0
lib/ansible/runner/lookup_plugins/csvfile.py
+82
-0
No files found.
lib/ansible/runner/lookup_plugins/csvfile.py
0 → 100644
View file @
671b0454
# (c) 2013, Jan-Piet Mens <jpmens(at)gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from
ansible
import
utils
,
errors
import
os
import
codecs
import
csv
class
LookupModule
(
object
):
def
__init__
(
self
,
basedir
=
None
,
**
kwargs
):
self
.
basedir
=
basedir
def
read_csv
(
self
,
filename
,
key
,
delimiter
,
dflt
=
None
,
col
=
1
):
try
:
f
=
codecs
.
open
(
filename
,
'r'
,
encoding
=
'utf-8'
)
creader
=
csv
.
reader
(
f
,
delimiter
=
delimiter
)
for
row
in
creader
:
if
row
[
0
]
==
key
:
return
row
[
int
(
col
)]
except
Exception
,
e
:
raise
errors
.
AnsibleError
(
"csvfile:
%
s"
%
str
(
e
))
return
dflt
def
run
(
self
,
terms
,
inject
=
None
,
**
kwargs
):
terms
=
utils
.
listify_lookup_plugin_terms
(
terms
,
self
.
basedir
,
inject
)
if
isinstance
(
terms
,
basestring
):
terms
=
[
terms
]
ret
=
[]
for
term
in
terms
:
params
=
term
.
split
()
key
=
params
[
0
]
paramvals
=
{
'file'
:
'ansible.csv'
,
'default'
:
None
,
'delimiter'
:
"TAB"
,
'col'
:
"1"
,
# column to return
}
# parameters specified?
try
:
for
param
in
params
[
1
:]:
name
,
value
=
param
.
split
(
'='
)
assert
(
name
in
paramvals
)
paramvals
[
name
]
=
value
except
(
ValueError
,
AssertionError
)
as
e
:
raise
errors
.
AnsibleError
(
e
)
if
paramvals
[
'delimiter'
]
==
'TAB'
:
paramvals
[
'delimiter'
]
=
"
\t
"
path
=
utils
.
path_dwim
(
self
.
basedir
,
paramvals
[
'file'
])
var
=
self
.
read_csv
(
path
,
key
,
paramvals
[
'delimiter'
],
paramvals
[
'default'
],
paramvals
[
'col'
])
if
var
is
not
None
:
if
type
(
var
)
is
list
:
for
v
in
var
:
ret
.
append
(
v
)
else
:
ret
.
append
(
var
)
return
ret
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment