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' 
2
  • Can you tell us more about why you want to do this? Perhaps we can provide an alternative that may work? Commented Oct 12, 2018 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 Commented Oct 16, 2018 at 12:38

2 Answers 2

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.

1
  • 1
    Or in the vars directory alongside the play. By default, vars/main.yml will be included. Commented Oct 13, 2018 at 14:07
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 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.