roles - apply variable values from /defaults/main.yml

56 views
Skip to first unread message

dulh...@mailbox.org

unread,
Jan 20, 2022, 11:16:01 AM1/20/22
to ansible...@googlegroups.com
using roles with this type of structure and face a problem with variable values apparently not being applied from the /defaults/main.yml

role01
    |__/tasks
    |     |
    |     |__main.yml
    |
    |__/defaults
           |
           |__main.yml

As far my understanding goes the /tasks/main.yml should read the variables from /defaults/main.yml no matter what (without any vars_files: declaration. Is that understanding correct?

If not, how exactly do I have to reference the /defaults/main.yml ?

Vladimir Botka

unread,
Jan 20, 2022, 11:39:39 AM1/20/22
to ansible...@googlegroups.com
On Thu, 20 Jan 2022 17:15:42 +0100 (CET)
dulhaver via Ansible Project <ansible...@googlegroups.com> wrote:

> As far my understanding goes the /tasks/main.yml should read the variables from /defaults/main.yml no matter what (without any vars_files: declaration. Is that understanding correct?

Yes. This is correct. For example

shell> tree roles
roles
└── role01
├── defaults
│   └── main.yml
└── tasks
└── main.yml

3 directories, 2 files

shell> cat roles/role01/defaults/main.yml
test_var: Hello

shell> cat roles/role01/tasks/main.yml
- debug:
var: test_var

shell> cat playbook.yml
- hosts: localhost
roles:
- role01

shell> ansible-playbook playbook.yml

gives (abridged)

TASK [role01 : debug]
**********************************
ok: [localhost] =>
test_var: Hello


If this is not working for you provide "Minimal nonworking example"
https://en.wikipedia.org/wiki/Minimal_working_example

--
Vladimir Botka

dulh...@mailbox.org

unread,
Jan 20, 2022, 1:47:39 PM1/20/22
to ansible...@googlegroups.com
so, that is a clear yes (and great little exercise) then.
I'll have to figure out, why it does't work atm

thx for sharing
> --
> 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/20220120173923.6b4ce819%40gmail.com.

Stefan Hornburg (Racke)

unread,
Jan 21, 2022, 1:51:00 AM1/21/22
to dulhaver via Ansible Project
On 20/01/2022 19:47, dulhaver via Ansible Project wrote:
> so, that is a clear yes (and great little exercise) then.
> I'll have to figure out, why it does't work atm
>
> thx for sharing

Well to get help you need to elaborate on "why it does't work atm".

The variables in defaults/main.yml do have very low priority BTW.

Regards

              Racke
--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.

OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages