1

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 '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

2 Answers2

2

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.

Zoredache
  • 130,897
  • 41
  • 276
  • 420
0

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
Alexandre Roux
  • 470
  • 1
  • 6
  • 20