2

Suppose I want to pass a long list of arguments to multiple tasks - is there an easy way to define them once and then share them in all tasks?

tasks:                                 
   - name: test1                         
     test1:                       
       param1=value1
       ...
       param99=value99
     ...
   - name: test10                         
     test10:                       
       param1=value1
       ...
       param99=value99
Andrei
  • 1,862
  • 4
  • 15
  • 15

2 Answers2

2

In a playbook, you can add a vars section.

- hosts: all
  vars:
    param1: value1
    param99: value99
  tasks:
     - name: hello world
  roles:
     - some_role
tedder42
  • 23,519
  • 13
  • 86
  • 102
  • I am actually developing some new modules and I don't think there is a way to access the variables from the module code (without having to pass them as parameters). – Andrei Jun 28 '14 at 11:29
  • you should reword your question. Ansible modules avoid using implicit parameters, likely because side effects are a code smell. However, you can pass a data structure to each module. – tedder42 Jun 29 '14 at 22:38
0

Consider module_defaults:

vars:
  common_parameters:
    param1: value1
    ...
    param99: value99
module_defaults:
  test1: "{{ common_parameters }}"
  test10: "{{ common_parameters }}"
tasks:                                 
- name: test1                         
  test1:                       
    ...
- name: test10                         
  test10:                       
    ...

If all modules are from a common collection, then consider to ask the deverloper of that collection to add a module defaults group to their meta/runtime.yml:

# meta/runtime.yml
action_groups:
  test1and10:
  - test1
  - test10

and then in your playbook

module_defaults:
  group/namespace.collection.test1and10:
    param1: value1
    ...
    param99: value99
Elrond
  • 901
  • 9
  • 23