patternMinor
Override Ansible task parameters from role
Viewed 0 times
roleoverridefromparametersansibletask
Problem
I'd like to be able to define a task with default parameters:
Then, from my playbook, I'd like to be able to do something like this:
...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.)
- name: Create a new user
user:
name: "default"
password: "password"
state: presentThen, 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.)
Solution
Defining default value for ansible variables and overriding them via ansible playbooks can be achieved using Jinja2 templating.
Then if these variables are passed from the playbook it will be used, or else the default values.
- name: Create a new user
user:
name: "{{ username_variable | default('default_value') }}"
password: "{{ password_variable | default('default_value') }}"
state: presentThen 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"Code Snippets
- name: Create a new user
user:
name: "{{ username_variable | default('default_value') }}"
password: "{{ password_variable | default('default_value') }}"
state: presentroles:
- role: common
vars:
username_variable: "frank"
password_variable: "/bib/zsh"Context
StackExchange DevOps Q#6784, answer score: 2
Revisions (0)
No revisions yet.