Role default vars not being read?

111 views
Skip to first unread message

Jerry Steele

unread,
May 7, 2015, 12:01:42 PM5/7/15
to ansible...@googlegroups.com
I've got a role I'm using for testing, but I seem to be stumbling at the first hurdle.

My embryonic playbook looks like this:

---
- hosts: localhost
  sudo
: yes
  vars
:
   shinken_hosts
:
     
- {name: hostname1, address: 1.2.3.4, check_command: check-host-alive }
     
- {name: hostname2, address: 5.6.7.8, check_command: check-ssh, ssh_port: 2242, contacts: 'jerry,other' }
  roles
:
   
- role: shinkenconfig



The shinkenconfig role has a defaults/main.yml:

---
ssh_port
: "22"
contacts
: "jerry"
check_command
: "check-host-alive"



 
..plus a template to chuck everything into for the actual config:

{% for host in shinken_hosts %}
define host
{
 
use         generic-host
  host_name  
{{ host.name }}
  address    
{{ host.address }}
 
alias        {{ host.name }}
  check_command    
{{ host.check_command }}
  _SSHPORT    
{{ host.ssh_port }}
  contacts    
{{ host.contacts }}
}
{% endfor %}



The main task just grabs everything using the template module to write the file out.

If all the vars are populated in the main playbook, everything works fine, but I want some default values to populate via the role if they're not defined. If I take some of the vars out of the playbook (as in the first "hostname1" line above), ansible-playbook throws this:

fatal: [127.0.0.1] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict object' has no attribute 'ssh_port'", 'failed': True}
fatal
: [127.0.0.1] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict object' has no attribute 'ssh_port'", 'failed': True}

FATAL
: all hosts have already failed -- aborting



...but shouldn't it be pulling the vars out of defaults/main.yml, if they're not in the playbook? I'm willing to believe I got the YAML syntax wrong in the defaults/main.yml - if so, would someone be able to correct it?

Any ideas?

Thanks

Brian Coca

unread,
May 7, 2015, 12:06:40 PM5/7/15
to ansible...@googlegroups.com
not sure why you expect those vars to be part of host, your definition
makes them accessible like this:

{% for host in shinken_hosts %}
define host{
use generic-host
host_name {{ host.name }}
address {{ host.address }}
alias {{ host.name }}
check_command {{ check_command }}
_SSHPORT {{ ssh_port }}
contacts {{ contacts }}
}
{% endfor %}

which is different from how you define them in the shinken_hosts

I think you want:
{% for host in shinken_hosts %}
define host{
use generic-host
host_name {{ host.name }}
address {{ host.address }}
alias {{ host.name }}
check_command {{ host.check_command|default(check_command) }}
_SSHPORT {{ host.ssh_port|default(ssh_port) }}
contacts {{ host.contacts|default(contacts) }}
}
{% endfor %}


--
Brian Coca

Jerry Steele

unread,
May 8, 2015, 7:48:13 AM5/8/15
to ansible...@googlegroups.com
That's the puppy!

Thanks for the insight. That template syntax doesn't seem to be documented anywhere that I can see. Might be useful if it was...

Thanks

Jerry

Brian Coca

unread,
May 8, 2015, 10:42:27 AM5/8/15
to ansible...@googlegroups.com
The templates are jinja2, you can find the documentations at
http://jinja.pocoo.org/docs/dev/


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages