How to use a variable in a vars file

561 views
Skip to first unread message

Jacquelin C

unread,
Oct 25, 2022, 10:58:04 AM10/25/22
to Ansible Project
Hello team,

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

##############
---
# 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}}'    
##############

but when load the file with ansible.builtin.include_vars
I get the error : timestamp' is undefined  

How can i do that ?

Todd Lewis

unread,
Oct 25, 2022, 11:15:47 AM10/25/22
to ansible...@googlegroups.com, uto...@gmail.com
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   


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d8b1759f-8f14-419b-baf9-d33809283298n%40googlegroups.com.

-- 
Todd

Rowe, Walter P. (Fed)

unread,
Oct 25, 2022, 11:18:30 AM10/25/22
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Jacquelin C

unread,
Oct 25, 2022, 11:26:32 AM10/25/22
to ansible...@googlegroups.com
I source the file with :

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

Todd Lewis

unread,
Oct 25, 2022, 11:59:13 AM10/25/22
to ansible...@googlegroups.com, uto...@gmail.com
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.
Reply all
Reply to author
Forward
0 new messages