-include: {{ var }} not working

253 views
Skip to first unread message

Akos Vandra

unread,
Aug 17, 2014, 5:54:13 AM8/17/14
to ansible...@googlegroups.com
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 <module>
    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 <module>
    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

Michael DeHaan

unread,
Aug 17, 2014, 9:09:46 AM8/17/14
to ansible...@googlegroups.com
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




 


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4cb8ba86-a8be-4dc0-a73e-c0a98f4ba9f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Akos Vandra

unread,
Aug 17, 2014, 9:26:39 AM8/17/14
to ansible...@googlegroups.com

Michael DeHaan

unread,
Aug 18, 2014, 8:27:49 AM8/18/14
to ansible...@googlegroups.com
Excellent!  Thanks for testing!


Wolfgang Ziegler

unread,
Sep 4, 2014, 3:44:29 PM9/4/14
to ansible...@googlegroups.com
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"

Michael DeHaan

unread,
Sep 4, 2014, 9:32:58 PM9/4/14
to ansible...@googlegroups.com
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.






Wolfgang Ziegler

unread,
Sep 5, 2014, 5:37:44 AM9/5/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Sep 5, 2014, 3:12:59 PM9/5/14
to ansible...@googlegroups.com
"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.




Wolfgang Ziegler

unread,
Sep 7, 2014, 5:23:43 AM9/7/14
to ansible...@googlegroups.com

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.

 

Michael DeHaan

unread,
Sep 7, 2014, 8:31:52 AM9/7/14
to ansible...@googlegroups.com
"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.





Wolfgang Ziegler

unread,
Sep 19, 2014, 4:49:00 AM9/19/14
to ansible...@googlegroups.com
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.

 

Rushyendra Bandaru

unread,
Nov 17, 2015, 1:09:30 PM11/17/15
to Ansible Project, axo...@gmail.com
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

Reply all
Reply to author
Forward
0 new messages