197

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:

- name: Create default
  user:
    name: "default_name"
  when: my_variable is not defined

- name: Create custom
  user:
    name: "{{my_variable}}"
  when: my_variable is defined

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?

- user:
    name: "default_name", "{{my_variable}}"

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:

- name: Create user
  user:
    name: "{{my_variable}}"

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

2

9 Answers 9

328

You can use Jinja's default:

- name: Create user
  user:
    name: "{{ my_variable | default('default_value') }}"
Sign up to request clarification or add additional context in comments.

9 Comments

And if the default variable depends upon another variable. For example default('/home/' {{ anothervar }}), is there a way to concatenate those values?
Just remove the ' around the default_value and it would use a variable called default_value
Doesn't work if variable is nested like {{ dict.key | default(5) }}. It will fail if dict is undefined.
@JordanStewart I had the same case - with nested variables. I posted an answer with possible solutions below.
@BernardoVale if the default variable depends from another variable. As on your example default('/home/' {{ anothervar }}). I had to do the following to get it to work with for example a path construction: {{ my_variable | default('/opt/'+ another_var_dir) }}
|
17

If anybody is looking for an option which handles nested variables, there are several such options in this github issue.

In short, you need to use "default" filter for every level of nested vars. For a variable "a.nested.var" it would look like:

- hosts: 'localhost'
  tasks:
    - debug:
        msg: "{{ ((a | default({})).nested | default({}) ).var | default('bar') }}"

or you could set default values of empty dicts for each level of vars, maybe using "combine" filter. Or use "json_query" filter. But the option I chose seems simpler to me if you have only one level of nesting.

Comments

16

Not totally related, but you can also check for both undefined AND empty (for e.g my_variable:) variable. (NOTE: only works with ansible version > 1.9, see: link)

- name: Create user
  user:
    name: "{{ ((my_variable == None) | ternary('default_value', my_variable)) \
    if my_variable is defined else 'default_value' }}"

1 Comment

This is great, very useful.
7

In case you using lookup to set default read from environment you have also set the second parameter of default to true:

- set_facts:
    ansible_ssh_user: "{{ lookup('env', 'SSH_USER') | default('foo', true) }}"

You can also concatenate multiple default definitions:

- set_facts:
    ansible_ssh_user: "{{ some_var.split('-')[1] | default(lookup('env','USER'), true) | default('foo') }}"

Comments

7

If you are assigning default value for boolean fact then ensure that no quotes is used inside default().

- name: create bool default
  set_fact:
    name: "{{ my_bool | default(true) }}"

For other variables used the same method given in verified answer.

- name: Create user
  user:
    name: "{{ my_variable | default('default_value') }}"

Comments

5

If you have a single play that you want to loop over the items, define that list in group_vars/all or somewhere else that makes sense:

all_items:
  - first
  - second
  - third
  - fourth

Then your task can look like this:

  - name: List items or default list
    debug:
      var: item
    with_items: "{{ varlist | default(all_items) }}"

Pass in varlist as a JSON array:

ansible-playbook <playbook_name> --extra-vars='{"varlist": [first,third]}'

Prior to that, you might also want a task that checks that each item in varlist is also in all_items:

  - name: Ensure passed variables are in all_items
    fail:
      msg: "{{ item }} not  in all_items list"
    when: item not in all_items
    with_items: "{{ varlist | default(all_items) }}"

Comments

4

The question is quite old, but what about:

- hosts: 'localhost'
  tasks:
    - debug:
        msg: "{{ ( a | default({})).get('nested', {}).get('var','bar') }}"

It looks less cumbersome to me...

1 Comment

The problem is you need to repeat this wherever the variable is used. The question is how to set a default once and for all.
0

@Roman Kruglov mentioned json_query. It's perfect for nested queries.

An example of json_query sample playbook for existing and non-existing value:

- hosts: localhost
  gather_facts: False
  vars:
    level1:
      level2:
        level3:
          level4: "LEVEL4"
  tasks:
    - name: Print on existing level4
      debug:
         var: level1 | json_query('level2.level3.level4')  # prints 'LEVEL4'
      when: level1 | json_query('level2.level3.level4')

    - name: Skip on inexistent level5
      debug:
         var: level1 | json_query('level2.level3.level4.level5')  # skipped
      when: level1 | json_query('level2.level3.level4.level5')

Comments

0

You can also use an if statement:

# Firewall manager: firewalld or ufw
firewall: "{{ 'firewalld' if ansible_os_family == 'RedHat' else 'ufw' }}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.