include_vars in folders

according to https://docs.ansible.com/ansible/2.5/modules/include_vars_module.html

this task should load all yaml files in /home/yaml/users and assign to var ‘users’

  • name: load yaml using include_vars with dir
    include_vars:
    name: ‘users’
    dir: /home/yaml/users/
    delegate_to: localhost

  • name: debug users
    debug:
    var: ‘users’

but why debug message shows only the last yaml file content , not the all ??

Works as expected for me. For example the files

  $ cat yaml/users/user1.yml
  user1: 'user1_var'
  $ cat yaml/users/user2.yml
  user2: 'user2_var'
  $ cat yaml/users/user3.yml
  user3: 'user3_var'

give

    "users": {
        "user1": "user1_var",
        "user2": "user2_var",
        "user3": "user3_var"
    }

Probably, you might be interested in "DEFAULT_HASH_BEHAVIOUR"
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-hash-behaviour

With the changed variables in the files

  $ cat yaml/users/user1.yml
  user: 'user1_var'
  $ cat yaml/users/user2.yml
  user: 'user2_var'
  $ cat yaml/users/user3.yml
  user: 'user3_var'

the variables overwrite each other, as expected

    "users": {
        "user": "user3_var"
    }

HTH,

  -vlado

so any suggesions to combine multiple yaml files with same attributes ?

Sure. For example

    - set_fact:
        users: "{{ users|
                   default({})|
                   combine({my_key: my_value}) }}"
      vars:
        my_key: "{{ (item|basename|splitext).0 }}"
        my_value: "{{ lookup('file', item)|from_yaml }}"
      with_fileglob: "yaml/users2/*.yml"
    - debug:
        var: users

give

    "users": {
        "user1": {
            "user": "user1_var"
        },
        "user2": {
            "user": "user2_var"
        },
        "user3": {
            "user": "user3_var"
        }
    }

HTH,

  -vlado