-include: {{ var }} not working

Hello!

I am trying to include a file based on a variable name like so:

  • do preparation stuff
  • include: ‘{{ include_before_symlink }}’
    when: ‘{{ include_before_symlink != None }}’
  • do symlinking

This is to be able to do a yield, similar to this:

  • include: deploy_revision
    vars:
  • foo: bar
  • include_before_symlink: roles/myrole/tasks/before_symlink.yml

Which would execute the deploy_revison task up to the point where it would start executing the before_symlink task file, and then return and finish up the deploy_revision task. Emphasis is that the caller should be able to provide what to do BEFORE the symlinkink.

The error I get is:

akovanm0:water-playbook avandra$ ansible-playbook --private-key=~/.vagrant.d/insecure_private_key -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory site.yml -v
Traceback (most recent call last):
File “/usr/local/Cellar/ansible/1.6.10/libexec/bin/ansible-playbook”, line 5, in
pkg_resources.run_script(‘ansible==1.6.10’, ‘ansible-playbook’)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py”, line 489, in run_script
self.require(requires)[0].run_script(script_name, ns)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py”, line 1207, in run_script
execfile(script_filename, namespace, namespace)
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/EGG-INFO/scripts/ansible-playbook”, line 317, in
sys.exit(main(sys.argv[1:]))
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/EGG-INFO/scripts/ansible-playbook”, line 257, in main
pb.run()
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/init.py”, line 289, in run
play = Play(self, play_ds, play_basedir, vault_password=self.vault_password)
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/play.py”, line 152, in init
self._tasks = self._load_tasks(self._ds.get(‘tasks’, ), load_vars)
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/play.py”, line 588, in _load_tasks
loaded = self._load_tasks(data, mv, default_vars, included_sudo_vars, list(included_additional_conditions), original_file=include_filename, role_name=new_role)
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/play.py”, line 588, in _load_tasks
loaded = self._load_tasks(data, mv, default_vars, included_sudo_vars, list(included_additional_conditions), original_file=include_filename, role_name=new_role)
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/play.py”, line 576, in _load_tasks
(k,v) = t.split(“=”, 1)
ValueError: need more than 1 value to unpack
akovanm0:water-playbook avandra$

Thanks for your help,
Akos vandra

If you see that traceback in 1.7.1 (the latest release) please do file a bug in GitHub so we can improve the split or make a better error message.
Let us know!

As a sidenote, this syntax is not neccessary, so a quick primer on conditionals:

  • include: ‘{{ include_before_symlink }}’
    when: ‘{{ include_before_symlink != None }}’

At a basic level, you can drop the Jinja2 brackets, as that will result in a string value that you don’t want:

  • include: “{{ foo }}”
    when: include_before_symlink != None

OR (more simply, if you wish to only define it when it is to be used)

  • include: “{{ foo }}”
    when: include_before_symlink is defined

OR (if you just want to reply on the true/false value)

  • include: “{{ foo }}”
    when: include_before_symlink | default(False)

Defaults can also set in roles/rolename/defaults/main.yml, which make this even nicer:

  • include: “{{ foo }}”
    when: include_before_symlink

Working on 1.7.1

Excellent! Thanks for testing!

Looks like this is about task include. I did something similar with a playbook include and it used to work in <1.7. However, after updating to 1.7.1 it fails now (with the same error). Is the following playbook supposed to work?

`

  • vars:
  • playbook: “file”

include: “{{ playbook }}.yml”

`

No, it shouldn’t.

For one, the YAML is conceptually mixed up a bit:

  • vars:
  • playbook: “file”

include: “{{ playbook }}.yml”

Perhaps you meant a task include, but basically there’s a lot of made-up syntax here and I can’t make sense of it.

It may be easier to step back and ask what you are trying to do, maybe in a new thread, and we could help you model it.

Yeah, I was thinking that might be the case so I posted my general of what I want to achieve at https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/ansible-project/txSI-DE2-ts.

Perhaps you meant a task include, but basically there’s a lot of made-up syntax here and I can’t make sense of it.

I wanted to include a playbook based on the value of a variable which e.g. I could pass in via extra-vars. But as variables are at play-scope I tried defining it as above. Unfortunately, there is not much docs about playbook includes but the example in the docs is structured the same minus the vars part:

`

  • include: load_balancers.yml
  • include: webservers.yml
  • include: dbservers.yml

`

"I wanted to include a playbook based on the value of a variable which e.g. I could pass in via extra-vars. But as variables are at play-scope I tried defining it as above. "

Extra vars would be fine here.

Variables defined in a play can be used in task includes if passed to the include, but not to determine the filename.

Extra vars would be fine here.

Variables defined in a play can be used in task includes if passed to the include, but not to determine the filename.

I see - but is there a way to provide a default value for the extra-vars if it’s omitted? I’d dislike having the playbook require an extra-var to actually work at all. Having a vars_prompt that works on playbook level would be useful here.

“I see - but is there a way to provide a default value for the extra-vars if it’s omitted”

Yes, in your playbook where you use the variable, use this useful Jinja2 filter:

{{ variable_name | default(‘my_fault’) }}

“Having a vars_prompt that works on playbook level would be useful here.”

While vars_prompt appears nice, and it can be, it also reduces the ease at which a playbook can be run by other-automation or inserted into a script or continuous deployment workflow. As such, it’s better to avoid it if possible.

Unfortunately, it looks like it’s not possible to use {{ variable_name | default(‘my_fault’) }} for playbook-level includes :-/

“Having a vars_prompt that works on playbook level would be useful here.”

While vars_prompt appears nice, and it can be, it also reduces the ease at which a playbook can be run by other-automation or inserted into a script or continuous deployment workflow. As such, it’s better to avoid it if possible.

Yep, I’m aware of this - thanks. I just use for required variables which usually are passed via extra-vars. When the playbook is invoked manually, the prompt is nicer though than the extra vars syntax.

Hello All,

I’ve define one role/tasks/main.yml as below

  • name: Include module variables

include_vars: ‘{{ module }}.yml’

  • include: prereq-{{ module }}-status.yml

I’ve defined each yml (prereq-cq-status.yml, prereq-dmgr-status.yml,prereq-ihs-status.yml) in same role/tasks

and files are mentioned in roles/vars (cq.yml,dmgr.yml,ihs.yml)

Note :module defined in inventory

got syntax error, ERROR: file could not read: (role/tasks/prereq-{{module}}-status.yml)

Can some one help me please.how to fix this