Ansible – Use default if a variable is not defined

Question:

I’m customizing linux users creation inside my role. I need to let users of my role customize home_directory, group_name, name, password.

I was wondering if there’s a more flexible way to cope with default values.

I know that the code below is possible:

But as I mentioned, there’s a lot of optional variables and this creates a lot of possibilities.

Is there something like the code above?

The code should set name="default_name" when my_variable isn’t defined.

I could set all variables on defaults/main.yml and create the user like that:

But those variables are inside a really big hash and there are some hashes inside that hash that can’t be a default.

Answer:

You can use Jinja’s default:

Leave a Reply