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

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-provide-default-values-to-a-set-of-tasks/25653>\
**Category:** Ansible Project\
**Created:** [October 2, 2017, 2:49am UTC](https://forum.ansible.com/t/is-there-a-way-to-provide-default-values-to-a-set-of-tasks/25653 "2017-10-02T02:49:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Behrang](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Behrang](https://forum.ansible.com/u/Behrang)\
**Post date:** [October 2, 2017, 2:49am UTC](https://forum.ansible.com/t/is-there-a-way-to-provide-default-values-to-a-set-of-tasks/25653/1 "2017-10-02T02:49:26Z")

</div>

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:

```auto
- 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:

```auto
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:

```auto
- 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:

```auto
- 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:

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

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [October 2, 2017, 7:18am UTC](https://forum.ansible.com/t/is-there-a-way-to-provide-default-values-to-a-set-of-tasks/25653/2 "2017-10-02T07:18:17Z")

</div>

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  
more info here: [http://docs.ansible.com/ansible/latest/playbooks\_best\_practices.html#directory-layout](http://docs.ansible.com/ansible/latest/playbooks_best_practices.html#directory-layout)
3. Use the ‘include\_vars’ module  
more info here: [http://docs.ansible.com/ansible/latest/include\_vars\_module.html](http://docs.ansible.com/ansible/latest/include_vars_module.html)

More information about variables is here [http://docs.ansible.com/ansible/latest/playbooks\_variables.html](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](http://docs.ansible.com/ansible/latest/playbooks_reuse_roles.html#using-roles)

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![Behrang](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Behrang](https://forum.ansible.com/u/Behrang)\
**Post date:** [October 3, 2017, 12:02am UTC](https://forum.ansible.com/t/is-there-a-way-to-provide-default-values-to-a-set-of-tasks/25653/3 "2017-10-03T00:02:33Z")

</div>

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:

```auto
my_validate_certs: False

```

```auto
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:

```auto
- 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”.

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [October 3, 2017, 12:12am UTC](https://forum.ansible.com/t/is-there-a-way-to-provide-default-values-to-a-set-of-tasks/25653/4 "2017-10-03T00:12:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 3, 2017, 11:54am UTC](https://forum.ansible.com/t/is-there-a-way-to-provide-default-values-to-a-set-of-tasks/25653/5 "2017-10-03T11:54:39Z")

</div>

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.
