I’ve read the docs, several topics in this group, the examples github, plus several experiments on some testing vms, but I can’t seem to make it click.
The precedence vars take is this: (I think…)
- vars passed in from the commandline
- vars directly in the playbook
- vars in host_vars/hostname
- vars in group_vars/group
- vars in roles/rolename/vars (how does this work with more than one role defining a var?)
- vars in group_vars/all
I also have been having issues referencing vars. I thought you could do use ${var}, {{ var }}, or $var (see: http://www.ansibleworks.com/docs/playbooks.html#vars-section), but when I do something like:
template: src=blah.j2 dest=/etc/blah/{{ var }}
It creates a file named {{ var }} rather than what the variable is supposed to be. Same if I used ${var}
I also have a task in a role that I want to run only if the host has the correct variable defined. As in host_vars/alpha.blah.org has ‘vhost’ defined, but host_vars/beta.blah.org does not. So the task would run for alpha.blah.org, but not for beta.blah.org. I tried:
- name: blah
template: src=blah dest=blah
when: vhost is defined
where only host_vars/alpha.blah.org had vhost defined. The result was no tasks were run for alpha.blah.org, but the other two hosts I’m testing on had tasks run…
I guess I need these questions answered:
-
What do I have wrong in what I wrote above?
-
How do I correctly reference vars?
-
How do I make sure var files are loaded correctly?
-
As in vars for alpha.blah.org are only used for alpha.blah.org, vars for beta.blah.org are only used for beta.blah.org, and so on.1. How do I run tasks only when the correct variable is defined?
The way I’ve been testing this, is by trying to create apache vhost files. Here are what I think are the relevant files:
file: roles/webtier/tasks/main.yml
- include: php-apache-stack.yml
- include: apache2-vhosts.yml
file: roles/webtier/tasks/apache2-vhosts.yml
- name: create and enable vhosts
when: apache2_vhosts is defined
template: src=http-vhost.j2 dest=/etc/apache2/sites-available/${item.server_name}
with_items:- apache2_vhosts
notify:- restart apache2
file: host_vars/vm.blankalpha.lab
testvars:
- test1: “THis is a test with bad grammar.”
test2: “TEST TWO”apache2_vhosts:
- server_name: vm.blankalpha.lab
server_admin: webservices@blah.org
server_aliases: done.blankalpha.lab
docroot: /var/www/
When I did not have the when condition, it created a file named /etc/apache2/sites-available/${item.server_name} that contained my template, without any values substituted into it.