mirror of https://github.com/ansible/ansible.git
Merge pull request #1522 from dhozac/LOOKUP-templating
Add $LOOKUP(<lookup plugin>,<data>) as a templating optionpull/1648/head
commit
5e024243a8
@ -0,0 +1,30 @@
|
||||
# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.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
|
||||
|
||||
class LookupModule(object):
|
||||
|
||||
def __init__(self, basedir=None, **kwargs):
|
||||
self.basedir = basedir
|
||||
|
||||
def run(self, terms, **kwargs):
|
||||
path = utils.path_dwim(self.basedir, terms)
|
||||
if not os.path.exists(path):
|
||||
raise errors.AnsibleError("%s does not exist" % path)
|
||||
return [open(path).read().rstrip()]
|
||||
@ -0,0 +1,32 @@
|
||||
# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.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/>.
|
||||
|
||||
import subprocess
|
||||
from ansible import utils, errors
|
||||
|
||||
class LookupModule(object):
|
||||
|
||||
def __init__(self, basedir=None, **kwargs):
|
||||
self.basedir = basedir
|
||||
|
||||
def run(self, terms, **kwargs):
|
||||
p = subprocess.Popen(terms, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
(stdout, stderr) = p.communicate()
|
||||
if p.returncode == 0:
|
||||
return stdout.splitlines()
|
||||
else:
|
||||
raise errors.AnsibleError("lookup_plugin.lines(%s) returned %d" % (terms, p.returncode))
|
||||
@ -0,0 +1,32 @@
|
||||
# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.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/>.
|
||||
|
||||
import subprocess
|
||||
from ansible import utils, errors
|
||||
|
||||
class LookupModule(object):
|
||||
|
||||
def __init__(self, basedir=None, **kwargs):
|
||||
self.basedir = basedir
|
||||
|
||||
def run(self, terms, **kwargs):
|
||||
p = subprocess.Popen(terms, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
(stdout, stderr) = p.communicate()
|
||||
if p.returncode == 0:
|
||||
return [stdout.rstrip()]
|
||||
else:
|
||||
raise errors.AnsibleError("lookup_plugin.pipe(%s) returned %d" % (terms, p.returncode))
|
||||
@ -0,0 +1,37 @@
|
||||
# simple test of lookup plugins in with_*
|
||||
---
|
||||
- hosts: all
|
||||
vars:
|
||||
empty_list: []
|
||||
tasks:
|
||||
- name: test with_items
|
||||
action: command true
|
||||
with_items:
|
||||
- 1
|
||||
- 2
|
||||
- 3
|
||||
- name: test with_items with empty list
|
||||
action: command true
|
||||
with_items: $empty_list
|
||||
|
||||
- name: test with_file and FILE
|
||||
action: command test "$item" = "$FILE(sample.j2)"
|
||||
with_file: sample.j2
|
||||
|
||||
- name: test with_pipe
|
||||
action: command test "$item" = "$PIPE(cat sample.j2)"
|
||||
with_pipe: cat sample.j2
|
||||
|
||||
- name: test LOOKUP and PIPE
|
||||
action: command test "$LOOKUP(pipe, cat sample.j2)" = "$PIPE(cat sample.j2)"
|
||||
|
||||
- name: ensure test file doesnt exist
|
||||
# command because file will return differently
|
||||
action: command rm -f /tmp/ansible-test-with_lines-data
|
||||
- name: test with_lines
|
||||
action: shell echo "$item" >> /tmp/ansible-test-with_lines-data
|
||||
with_lines: cat sample.j2
|
||||
- name: verify with_lines
|
||||
action: copy src=sample.j2 dest=/tmp/ansible-test-with_lines-data
|
||||
- name: cleanup test file
|
||||
action: file path=/tmp/ansible-test-with_lines-data state=absent
|
||||
Loading…
Reference in New Issue