How to use a variable in a vars file

Hello team,

I would like to use variables in a vars file, like this :

Hard to say why that would fail. I copy-n-pasted your file and it worked for me, although I named it slightly differently.

For completeness, here’s my playbook and file:

[utoddl@tango ansible]$ cat include_vars.yml
---
- name: testing include_vars
  hosts: localhost
  gather_facts: false
  tasks:
  - name: include the vars file
    ansible.builtin.include_vars:
      file: ./include_vars_file.yml
  - name: show users
    debug:
      msg: "{{ users }}"
[utoddl@tango ansible]$ cat include_vars_file.yml 
---
# file vars/users.yml

timestamp:
  sep2023: 1693864800 # date --date=09/05/2023 +%s

users:

  a_user:
    name: a_user
    passwd: $6$FF.DN/vbue.2i9/vla6h8xpZhx4L/dppBbnnCWN8hZ0
    uid: 3007
    comment: log receiver
    expires: '{{timestamp.sep2023}}'
[utoddl@tango ansible]$ ansible-playbook include_vars.yml -vv
ansible-playbook [core 2.12.9]
[...]
PLAYBOOK: include_vars.yml *****************************************************
1 plays in include_vars.yml

PLAY [testing include_vars] ****************************************************
META: ran handlers

TASK [include the vars file] ***************************************************
task path: /home/utoddl/ansible/include_vars.yml:6
ok: [localhost] => {"ansible_facts": {"timestamp": {"sep2023": 1693864800}, "users": {"a_user": {"comment": "log receiver", "expires": "{{timestamp.sep2023}}", "name": "a_user", "passwd": "$6$FF.DN/vbue.2i9/vla6h8xpZhx4L/dppBbnnCWN8hZ0", "uid": 3007}}}, "ansible_included_var_files": ["/home/utoddl/ansible/./include_vars_file.yml"], "changed": false}

TASK [show users] **************************************************************
task path: /home/utoddl/ansible/include_vars.yml:9
ok: [localhost] => {
    "msg": {
        "a_user": {
            "comment": "log receiver",
            "expires": "1693864800",
            "name": "a_user",
            "passwd": "$6$FF.DN/vbue.2i9/vla6h8xpZhx4L/dppBbnnCWN8hZ0",
            "uid": 3007
        }
    }
}
META: ran handlers
META: ran handlers

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

How do you source the vars file? You have two options.

A vars_files directive before the tasks section.

vars_files:

  • your_vars_file.yml

A task that explicitly includes a vars file.

include_vars:
file: your_vars_file.yml

There are other variants of this so read the docs on the ansible website.

Walter

I source the file with :

ansible.builtin.include_vars:
file: users.yml
name: users

Ah, that’s the problem. You’re using name: users parameter, so in your vars file,

expires: ‘{{ timestamp.sep2023 }}’

isn’t going to exist. It’s going to be

expires: ‘{{ users.timestamp.sep2023 }}’

But that’s also going to throw errors.

I think you’re running into a bug with how name: is implemented in ansible.builtin.include_vars.