Ansible Playbook – How to Define Different Vars for Different Hosts

ansible

Is it possible to define different variables for different hosts? like:

- hosts:
    server_a:
      var_a: "xxx"
    server_b:
      var_a: "yyy"
  roles:
    - some_role

Best Answer

Host (and group) variables are not stored in the playbook. They are stored separately, in the inventory file itself, or in files in a directory host_vars or group_vars relative to the inventory file. These files are named after the host or group for which they contain variables.

See Organizing host and group variables in the Ansible documentation for a more complete description of the variable files.