Ansible Variable above hosts

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

    some_vars: "./Prometheus/roles/" # <---- THIS Variable?
- hosts: lxd1
    - Prometheus/vars/grafana_vars.yml
  become: true
    - '{{ some_vars }}Stouts.grafana'   <--- var usage
- hosts: lxd1
    - Prometheus/vars/exporters_common_vars.yml
  become: true
    - './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 '18 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 '18 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 '18 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

[production-servers]                # inventory for production servers

[allvars:children]                  # group vars to apply for all hosts

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

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

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

   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 agree to our terms of service, privacy policy and cookie policy

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