1

For now I have a playbook looks like

- hosts: hws
  gather_facts: no
  vars_files:
    - variables.yml
  roles:
    - role: hypervisor

- hosts: vms
  gather_facts: no
  vars_files:
    - variables.yml
  roles:
    - role: web-server

and it looks ugly. How to set

  gather_facts: no
  vars_files:
    - variables.yml

globally for every hosts in entire file?

HeroFromEarth
  • 73
  • 1
  • 9

1 Answers1

1

You can use the ansible directory layout to group variables based on hostgroups or on hosts.

ansible.cfg
hosts
group_vars/
    hws.yml   #variables for hws hosts
    vms.yml   #variables for vms hosts
    all  #here you can put variables commont to both hws + vms
roles/
    hypervisor
    web-server
playbook.yml

This way you do not need to include the variable file in your playbook.

You can also read here more information about the ansible directory structure : http://docs.ansible.com/ansible/playbooks_best_practices.html

And here about variable precedence : http://docs.ansible.com/ansible/playbooks_variables.html

Good luck !

  • Many thanks for your answer! I moved my common vars file to **group_vars/all** and add string `gathering = explicit` to **ansible.cfg** and get exactly what I want! – HeroFromEarth Jun 22 '17 at 08:50