Set playbook var using from_json

1,042 views
Skip to first unread message

Bruno Galindro da Costa

unread,
Jun 19, 2015, 1:06:40 PM6/19/15
to ansible...@googlegroups.com
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.

---
- hosts: tag_v3_elasticsearch_prod_True
  vars
:
   
- ssh_private_key_file: "{{ ec2_tag_ssh | from_json }}"
   
- ssh_port: "{{ ec2_tag_ssh | from_json }}"
   
- ssh_user: "{{ ec2_tag_ssh | from_json }}"
  tasks
:
   
- debug: var=ssh_private_key_file
   
- debug: var=ssh_port
   
- debug: var=ssh_user


But, a warning message is displayed to me. It seems that from_json filter isn't recognized in playbook vars section.

How can I solve my problem?

[WARNING]: non fatal error while trying to template play variables: Failed to
template {{ ec2_tag_ssh | from_json }}: an unexpected type error occurred.
Error was expected string or buffer


PLAY
[tag_v3_elasticsearch_prod_True] *****************************************

GATHERING FACTS
***************************************************************
ok
: [x.x.x.xxx]

TASK
: [debug var=ssh_private_key_file] ****************************************
ok
: [x.x.x.xxx] => {
   
"var": {
       
"ssh_private_key_file": {
           
"ssh_key": "prod.pem",
           
"ssh_port": "22",
           
"ssh_user": "ansible"
       
}
   
}
}

TASK
: [debug var=ssh_port] ****************************************************
ok
: [x.x.x.xxx] => {
   
"var": {
       
"ssh_port": {
           
"ssh_key": "prod.pem",
           
"ssh_port": "22",
           
"ssh_user": "ansible"
       
}
   
}
}

TASK
: [debug var=ssh_user] ****************************************************
ok
: [x.x.x.xxx] => {
   
"var": {
       
"ssh_user": {
           
"ssh_key": "prod.pem",
           
"ssh_port": "22",
           
"ssh_user": "ansible"
       
}
   
}
}

PLAY RECAP
********************************************************************
x
.x.x.xxx             : ok=4    changed=0    unreachable=0    failed=0  




Brian Coca

unread,
Jun 19, 2015, 5:08:22 PM6/19/15
to ansible...@googlegroups.com
try w/o a filter
> --
> 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/f83550e9-8017-4170-b871-98be886e72a9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Bruno Galindro da Costa

unread,
Jun 19, 2015, 6:00:29 PM6/19/15
to ansible...@googlegroups.com
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 "<template>", 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  





--
Brian Coca

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/zr_vdVki_EM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Att.
Bruno Galindro da Costa
Reply all
Reply to author
Forward
0 new messages