Ansible Variable above hosts

hello its posible to create a variable for multi hosts like this?

  vars:
    some_vars: "./Prometheus/roles/" # <---- THIS Variable?
- hosts: lxd1
  vars_files:
    - Prometheus/vars/grafana_vars.yml
  become: true
  roles:
    - '{{ some_vars }}Stouts.grafana'   <--- var usage
- hosts: lxd1
  vars_files:
    - Prometheus/vars/exporters_common_vars.yml
  become: true
  roles:
    - './Prometheus/roles/ansible-prometheus-exporters-common'
  • Can you tell us more about why you want to do this? Perhaps we can provide an alternative that may work? – Zoredache Oct 12 at 22:03
  • playbook is for specific client and other parts like roles vars and other are submodules so path to file can change and i dont wanna make 30-40 change in path on playbook to run playbook for specific client and second problem jenkins run this playbook – Robert Fil Oct 16 at 12:38

No that is not valid syntax in a playbook. There is no way within a playbook to include vars that will apply for all plays.

If you want variables applied to multiple plays, and so multiple hosts hosts define them in your inventory or group_vars.

You could also pass variables from the command line, if you need variables to apply to many plays using the -e option.

  • 1
    Or in the vars directory alongside the play. By default, vars/main.yml will be included. – John Mahowald Oct 13 at 14:07

As mention in @Zoredache post I would use group_vars in order to define a group to apply vars to all host.

In order to do that I would use a following inventory file :

[dev-servers]                       # inventory for dev servers
dev1.example.com
dev2.example.com

[production-servers]                # inventory for production servers
prod1.example.com
prod2.example.com

[allvars:children]                  # group vars to apply for all hosts
dev-servers
production-servers

[productionvars:children]           # group vars to apply for only production
production-servers

[devvars:children]                  # group vars to apply for only dev
dev-servers

Then you will have to create a dedicate group_vars file for allvars group, please find here an example of directory layout :

inventory_host                       # inventory file

group_vars/
   allvars.yml             # here we assign variables to all hosts
   productionvars.yml      # here we assign variables to only prod hosts
   devvars.yml             # here we assign variables to only dev hosts

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.