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

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.

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
  3. Use the ‘include_vars’ module
    more info here: 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

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

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

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

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.