Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Passing evaluated variables to includes

Tags:

ansible

I have an Ansible Playbook that makes a call to another playbook passing in parameters. This works just fine, but when I try to pass in variables, Ansible blows up. Here is my playbook:

---
- name: This is the toplevel play for orchestration
  hosts: nogroup
  vars:
    customer: myCustomer
  sudo: yes
- include: amazon.yml application=FooServer instance_type=t2.micro instance_count=1 customer={{ customer }}
- include: amazon.yml application=BarServer instance_type=t2.micro instance_count=1 customer={{ customer }}

I try to run:

ansible-playbook orchestration.yml -e "customer=otherCustomer"

and I get this error:

Traceback (most recent call last):
  File "/usr/local/Cellar/ansible/1.7.2/libexec/bin/ansible-playbook", line 5, in <module>
    pkg_resources.run_script('ansible==1.7.2', 'ansible-playbook')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 492, 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 1350, in run_script
    execfile(script_filename, namespace, namespace)
  File "/usr/local/Cellar/ansible/1.7.2/lib/python2.7/site-packages/ansible-1.7.2-py2.7.egg/EGG-INFO/scripts/ansible-playbook", line 309, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/usr/local/Cellar/ansible/1.7.2/lib/python2.7/site-packages/ansible-1.7.2-py2.7.egg/EGG-INFO/scripts/ansible-playbook", line 191, in main
    force_handlers=options.force_handlers
  File "/usr/local/Cellar/ansible/1.7.2/lib/python2.7/site-packages/ansible-1.7.2-py2.7.egg/ansible/playbook/__init__.py", line 180, in __init__
    (self.playbook, self.play_basedirs) = self._load_playbook_from_file(playbook, vars)
  File "/usr/local/Cellar/ansible/1.7.2/lib/python2.7/site-packages/ansible-1.7.2-py2.7.egg/ansible/playbook/__init__.py", line 271, in _load_playbook_from_file
    inc_vars, inc_path = self._get_include_info(play, basedir, play_vars)
  File "/usr/local/Cellar/ansible/1.7.2/lib/python2.7/site-packages/ansible-1.7.2-py2.7.egg/ansible/playbook/__init__.py", line 210, in _get_include_info
    (k,v) = t.split("=", 1)
ValueError: need more than 1 value to unpack

Any ideas on how to allow me to pass a variable I set on the command line down through multiple levels of includes in my playbook?

like image 871
Xanxir Avatar asked Nov 06 '14 16:11

Xanxir


People also ask

How do you pass variables in Ansible?

The easiest way to pass Pass Variables value to Ansible Playbook in the command line is using the extra variables parameter of the “ansible-playbook” command. This is very useful to combine your Ansible Playbook with some pre-existent automation or script.

How do you pass multiple extra variables in Ansible playbook?

To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.


1 Answers

Got there with the same problem. Resolved as follows:

---
- include: common.yml
- include: redis.yml
    redis_port="{{ airflow_redis_port }}"
    redis_bind="{{ airflow_redis_host }}"
like image 94
Ikar Pohorský Avatar answered Nov 23 '22 15:11

Ikar Pohorský