How to split an ansible role’s `defaults/main.yml` file into multiple files?

The feature I’m describing below has been available since Ansible 2.6, but got a bugfix in v2.6.2 and another (minor) one in v2.7.
To see a solution for older versions, see Paul’s answer.


defaults/main/

Instead of creating defaults/main.yml, create a directorydefaults/main/ — and place all YAML files in there.

  • defaults/main.ymldefaults/main/*.yml

Ansible will load any *.yml file inside that directory, so you can name your files like roles/my-role/defaults/main/{1,2}.yml.

Furthermore, it’s even possible to create sub-directories like roles/my-role/defaults/main/subdir_1/foo.yml.

Note, the old file — defaults/main.yml — must not exist. See this Github comment.


vars/main/

By the way, the above solution also works for vars/:

  • vars/main.ymlvars/main/*.yml

further details

The feature has been introduced in v2.6 — git commit, Pull Request, main Github issue.

There have been two bugfixes:

  • v2.7 fix: git commit, Pull Request — backported to v2.6.2: commit, Pull Request
  • v2.7 fix: git commit, Pull Request, bug discussion

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)