4

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.)

Ryan O.
  • 275
  • 2
  • 11

1 Answers1

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"
  • 1
    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. Apr 07 '19 at 12:08
  • Excellent answer, thank you vishnu. – bviktor Nov 26 '19 at 10:42