I have a playbook with multiple hosts section. I would like to define a variable in this playbook.yml file that applies only within the file, for example:
vars:
my_global_var: 'hello'
- hosts: db
tasks:
-shell: echo {{my_global_var}}
- hosts: web
tasks:
-shell: echo {{my_global_var}}
The example above does not work. I have to either duplicate the variable for each host section (bad) or define it at higher level, for example in my group_vars/all
(not what I want, but works). I am also aware that variables files can be included, but this affects readibility. Any suggestion to get it in the right scope (e.g the playbook file itself)?
I prefer to keep global variables in the inventory file, where you keep the groups and names of your hosts.
For example:
my-hosts
:Run your playbook with:
The variable will now be defined for all hosts.
If you are using
ec2.py
or some other dynamic inventory, you can put the global variables in the filegroup_vars/all
to achieve the same result.The
set_fact
module will accomplish this if group_vars don't suit your needs.http://docs.ansible.com/ansible/set_fact_module.html