1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Parameterized include statements can see top level variables and also be passed specific variables!

Code needs cleanup, but works
This commit is contained in:
Michael DeHaan 2012-03-05 22:00:22 -05:00
parent fb3bfa1c51
commit 4ee4ddcd7c
3 changed files with 18 additions and 3 deletions

View file

@ -3,3 +3,5 @@
action: command /usr/sbin/setenforce 0 action: command /usr/sbin/setenforce 0
- name: no iptables - name: no iptables
action: service name=iptables state=stopped action: service name=iptables state=stopped
- name: this is just to show variables work here, favcolor={{ favcolor }}
action: command /bin/true

View file

@ -5,7 +5,7 @@
http_port: 80 http_port: 80
max_clients: 200 max_clients: 200
tasks: tasks:
- include: base.yml - include: base.yml favcolor=blue
- name: write the apache config file using vars set above - name: write the apache config file using vars set above
action: template src=/srv/httpd.j2 dest=/etc/httpd.conf action: template src=/srv/httpd.j2 dest=/etc/httpd.conf
notify: notify:

View file

@ -23,6 +23,7 @@ from ansible.utils import *
import yaml import yaml
import shlex import shlex
import os import os
import jinja2
SETUP_CACHE={ 'foo' : {} } SETUP_CACHE={ 'foo' : {} }
@ -92,8 +93,20 @@ class PlayBook(object):
new_tasks = [] new_tasks = []
for task in tasks: for task in tasks:
if 'include' in task: if 'include' in task:
path = path_dwim(dirname, task['include']) # FIXME: refactor
included = yaml.load(file(path).read()) # an include line looks like:
# include: some.yml a=2 b=3 c=4
include_tokens = task['include'].split()
path = path_dwim(dirname, include_tokens[0])
inject_vars = play.get('vars', {})
for i,x in enumerate(include_tokens):
if x.find("=") != -1:
(k,v) = x.split("=")
inject_vars[k] = v
included = file(path).read()
template = jinja2.Template(included)
included = template.render(inject_vars)
included = yaml.load(included)
for x in included: for x in included:
new_tasks.append(x) new_tasks.append(x)
else: else: