# Set playbook var using from\_json

**URL:** <https://forum.ansible.com/t/set-playbook-var-using-from-json/18552>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [June 19, 2015, 5:06pm UTC](https://forum.ansible.com/t/set-playbook-var-using-from-json/18552 "2015-06-19T17:06:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruno\_Galindro\_da\_Co](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Bruno\_Galindro\_da\_Co](https://forum.ansible.com/u/Bruno_Galindro_da_Co)\
**Post date:** [June 19, 2015, 5:06pm UTC](https://forum.ansible.com/t/set-playbook-var-using-from-json/18552/1 "2015-06-19T17:06:40Z")

</div>

I need to configure ansible to make ssh connections to my EC2 instances based on parameters configure in ec2 tags. I’ve successfully done this using ec2.py script to build dynamic inventory. With this great functionality, I’ve setup 3 tags that contains all what I need to make ssh connections to my ec2 hosts, like this example:

`

- hosts: tag\_v3\_elasticsearch\_prod\_True  
vars:
- ansible\_ssh\_private\_key\_file: “{{ ec2\_tag\_ssh\_key | default(‘sv3.key’) }}”
- ansible\_ssh\_port: “{{ ec2\_tag\_ssh\_port | default(22) }}”
- ansible\_ssh\_user: “{{ ec2\_tag\_ssh\_user | default(‘ubuntu’) }}”  
tasks:
- SOME TASKS…

`

But, amazon has a limit of 10 tags by ec2 instances (I use many other tags associated with my ec2 instances). So, I need to join some tags into one to avoid pass this limit. Them, I’ve an idea to join those 3 ssh tags into one using json format, like this:

ssh: {“ssh\_key”: “prod.key”, “ssh\_port”: “22”, “ssh\_user”: “ansible”}

And them, I’ve configured my playbook to parse this variable with from\_json filter.

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 19, 2015, 9:08pm UTC](https://forum.ansible.com/t/set-playbook-var-using-from-json/18552/2 "2015-06-19T21:08:14Z")

</div>

try w/o a filter

---

<div class="post-metadata">

**Author:** ![Bruno\_Galindro\_da\_Co](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Bruno\_Galindro\_da\_Co](https://forum.ansible.com/u/Bruno_Galindro_da_Co)\
**Post date:** [June 19, 2015, 10:00pm UTC](https://forum.ansible.com/t/set-playbook-var-using-from-json/18552/3 "2015-06-19T22:00:26Z")

</div>

Brian, thanks for cooperation. I already tried without the from\_json filter, but no success:

`

- hosts: tag\_v3\_elasticsearch\_prod\_True  
vars:
- ansible\_ssh\_private\_key\_file: “{{ ec2\_tag\_ssh.ssh\_key }}”
- ansible\_ssh\_port: “{{ ec2\_tag\_ssh.ssh\_port }}”
- ansible\_ssh\_user: “{{ ec2\_tag\_ssh.ssh\_user }}”  
tasks:
- debug: var=ansible\_ssh\_private\_key\_file
- debug: var=ansible\_ssh\_port
- debug: var=ansible\_ssh\_user

`

`  
PLAY [tag\_v3\_elasticsearch\_prod\_True] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [`x.x.x.xxx`] =\> Traceback (most recent call last):  
File “/usr/local/lib/python2.7/dist-packages/ansible/runner/ **init**.py”, line 582, in \_executor  
exec\_rc = self.\_executor\_internal(host, new\_stdin)  
File “/usr/local/lib/python2.7/dist-packages/ansible/runner/ **init**.py”, line 785, in \_executor\_internal  
return self.\_executor\_internal\_inner(host, self.module\_name, self.module\_args, inject, port, complex\_args=complex\_args)  
File “/usr/local/lib/python2.7/dist-packages/ansible/runner/ **init**.py”, line 885, in \_executor\_internal\_inner  
actual\_private\_key\_file = template.template(self.basedir, actual\_private\_key\_file, inject, fail\_on\_undefined=True)  
File “/usr/local/lib/python2.7/dist-packages/ansible/utils/template.py”, line 122, in template  
varname = template\_from\_string(basedir, varname, templatevars, fail\_on\_undefined)  
File “/usr/local/lib/python2.7/dist-packages/ansible/utils/template.py”, line 371, in template\_from\_string  
res = jinja2.utils.concat(rf)  
File “”, line 8, in root  
File “/usr/local/lib/python2.7/dist-packages/Jinja2-2.7.3-py2.7.egg/jinja2/runtime.py”, line 485, in \_fail\_with\_undefined\_error  
raise self.\_undefined\_exception(hint)  
UndefinedError: ‘unicode object’ has no attribute ‘ssh\_key’

TASK: [debug var=ansible\_ssh\_private\_key\_file] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
FATAL: no hosts matched or all hosts have already failed – aborting

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
to retry, use: --limit @/home/galindro/play.retry

x.x.x.xxx : ok=0 changed=0 unreachable=1 failed=0

`
