<p>Defining default value for ansible variables and overriding them via ansible playbooks can be achieved using Jinja2 templating.</p>

<pre><code>- name: Create a new user
  user:
    name: "{{ username_variable | default('default_value') }}"
    password: "{{ password_variable | default('default_value') }}"
    state: present
</code></pre>

<p>Then if these variables are passed from the playbook it will be used, or else the default values.</p>

<pre><code>  roles:
    - role: common
      vars:
        username_variable: "frank"
        password_variable: "/bib/zsh"
</code></pre>

CC BY-SA 4.0
2019-04-07T12:08:55.523Z
I see. That's still very useful, thank you, but is there no way to refer directly to the module parameter (such as "password") without duplicating all of the module parameters as vars?
Ryan O.
CC BY-SA 4.0
2019-11-26T10:42:47.493Z
Excellent answer, thank you vishnu.
bviktor
CC BY-SA 4.0
2019-04-06T07:25:28.610Z
2019-04-06T07:25:28.610Z
vishnu narayanan
<p>I'd like to be able to define a task with default parameters:</p>

<pre><code>- name: Create a new user
  user:
    name: "default"
    password: "password"
    state: present
</code></pre>

<p>Then, from my playbook, I'd like to be able to do something like this:</p>

<pre><code>  roles:
    - role: common
      vars:
        name: "frank"
        shell: "/bin/zsh"
</code></pre>

<p>...and have it create the user "frank", with the password "password" and the /bin/zsh shell. Is it possible to override task parameters from an Ansible playbook? (For the record, the above doesn't work, I tried it.)</p>

<ansible>
Override Ansible task parameters from role
Ryan O.
# Override Ansible task parameters from role

(Asked by: username_0 on 2019-04-04)

I'd like to be able to define a task with default parameters:
- name: Create a new user
  user:
    name: "default"
    password: "password"
    state: present

Then, from my playbook, I'd like to be able to do something like this:
  roles:
    - role: common
      vars:
        name: "frank"
        shell: "/bin/zsh"

...and have it create the user "frank", with the password "password" and the /bin/zsh shell. Is it possible to override task parameters from an Ansible playbook? (For the record, the above doesn't work, I tried it.)

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2019-04-06. Score: 2

Defining default value for ansible variables and overriding them via ansible playbooks can be achieved using Jinja2 templating.
- name: Create a new user
  user:
    name: "{{ username_variable | default('default_value') }}"
    password: "{{ password_variable | default('default_value') }}"
    state: present

Then if these variables are passed from the playbook it will be used, or else the default values.
  roles:
    - role: common
      vars:
        username_variable: "frank"
        password_variable: "/bib/zsh"

> **Comments:**
> **username_0**: I see. That's still very useful, thank you, but is there no way to refer directly to the module parameter (such as "password") without duplicating all of the module parameters as vars?  
> **username_2**: Excellent answer, thank you vishnu.  

---

