Is there a way to provide default values to a set of tasks?

55 views
Skip to first unread message

Behrang Saeedzadeh

unread,
Oct 1, 2017, 10:49:26 PM10/1/17
to Ansible Project
Hi,

I am writing a role that contains a dozen of tasks that invoke REST APIs. All tasks have a set of common and identical configurations:

- name: Task 1
uri:
validate_certs: False
method: POST
user: "{{ user }}"
password: "{{ pass }}"
force_basic_auth: yes
body_format: json

I am looking for a way to avoid repeating these parameters for all the tasks. One way is to use anchors:

api_defaults: &API_DEFAULTS
validate_certs: False
method: POST
user: "{{ user }}"
password: "{{ pass }}"
force_basic_auth: yes
body_format: json

Then I can reuse this similar to:

- name: Task 1
uri:
<< *API_DEFAULTS

However, I still have to repeat the "<< *API_DEFAULTS" everywhere. Are there any other options available, other than developing new modules?

For example to "define" modules based on existing modules, in a way similar to:

- name: Define api
extend_module:
parent_module: uri
module_name: api
defaults:
validate_certs: False
method: POST
user: "{{ user }}"
password: "{{ pass }}"
force_basic_auth: yes
body_format: json

And then use it like:

- name: Task 1
api:
body:
a: 1
b: c

Thanks in advance.

Pshem Kowalczyk

unread,
Oct 2, 2017, 3:18:42 AM10/2/17
to ansible...@googlegroups.com
Hi,

If you want to use 'global' variables like that you have a few choices:

1. Use 'group_vars/all.yaml'
(all hosts belong to 'all' group by default)
2. Use 'defaults/main.yaml' under your role
3. Use the 'include_vars' module

More information about variables is here http://docs.ansible.com/ansible/latest/playbooks_variables.html

I think that the easiest way of building your 'own' modules (as a way of abstracting things out) is to use roles and pass parameters to those roles - more info here: http://docs.ansible.com/ansible/latest/playbooks_reuse_roles.html#using-roles

kind regards
Pshem


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4b750222-7c27-40b8-830f-010e8858fc1d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Behrang Saeedzadeh

unread,
Oct 2, 2017, 8:02:33 PM10/2/17
to Ansible Project
Hi Pshem,

Looks like my question was not phrased clearly enough. It is not about defining variables, I already use group, host, role, and play variables in my various Ansible projects.

Let's pretend we have defined these variables:

my_validate_certs: False
my_method: POST
my_user: admin
my_password: p@ss
my_force_basic_auth: yes
my_body_format: json

Almost all my URI tasks will use this configuration. So even though that I have defined these settings in variables, I still have to reference these variables every time I use the uri task:

- name: Task 1
uri:
    validate_certs: "{{my_validate_certs}}"
method: "{{my_method}}"
user: "{{my_user}}"
password: "{{my_pass}}"
force_basic_auth: "{{my_force_basic_auth}}"
body_format: "{{my_body_format}}"

This is what I am trying to avoid. I want to tell Ansible: "Hey, for all the uri tasks, use these values as defaults".

Pshem Kowalczyk

unread,
Oct 2, 2017, 8:13:00 PM10/2/17
to ansible...@googlegroups.com
I'm not aware of any generic way of providing those sort of values. Some modules  provide their own way of using environmental variables or external config files to facilitate what you're asking for. 

kind regards
Pshem


Kai Stian Olstad

unread,
Oct 3, 2017, 7:55:01 AM10/3/17
to ansible...@googlegroups.com
Nothing like this exist, but you could use a loop with a list of arrays
in with_items.

Other alternative is to put the task in a file and use include and
provide only the parameters that changes.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages