Set playbook var using from_json

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.

`

try w/o a filter

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

`