Search code examples
ansibleautomated-deploy

How to set params/vars globally for all hosts in Ansible's playbook?


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?


Solution

  • 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 !