2

I have a role that contains this task:

- name: Download something via Git
  include_role:
    name: git_cached
  vars:
    params:
      repo: https://somerepo
      dest: /some/path

I would really like to then have the git_cached role look like this:

- git: {{ params }}

I don't want to have to duplicate all of the parameters like this:

- git:
    repo: "{{ repo }}"
    dest: "{{ dest }}"

Because then, I have to do it for all the parameters, even the ones I don't typically use. I just want to basically "override" certain parameters that I pass into the include_role task.

Is what I'm trying to do possible?

3
  • FWIW, see example Oct 20, 2020 at 23:21
  • Vladimir, could you briefly explain how you think the example you posted helps in this case? Thanks!
    – Ryan O.
    Oct 21, 2020 at 21:24
  • You're welcome! It's out of the scope if it's not self-explaining. Oct 21, 2020 at 21:27

1 Answer 1

3

Q: "Override certain parameters that pass them into the include_role task."

A: The parameter tasks_from serves this use-case. For example, create the task file

shell> cat roles/git_cached/tasks/git.yml
- git:
    repo: "{{ params.repo }}"
    dest: "{{ params.dest }}"
    recursive: "{{ params.recursive|default(ommit) }}"
    depth: "{{ params.depth|default('1') }}"

Then "call" this task

    - name: Download something via Git
      include_role:
        name: git_cached
        tasks_from: git.yml
      vars:
        params:
          repo: https://somerepo
          dest: /some/path

You might want to complete the task with all git parameters:

  • The parameters repo and dest are required.

  • In some parameters, like recursive, the default values are defined. You can either omit the parameter, in which case the default value will be used, or define your own default.

  • Some parameters, like depth, don't have defaults. Also here you can either omit the parameter, in which case the parameter won't be used or define your own default.

  • It's a good idea to "namespace" the variables.

  • It's a good idea to put the default variables into roles/git_cached/defaults/main.yml

3
  • How is what you're suggesting different than leaving off the "tasks_from" entirely? Because it looks a lot like what I'm trying to avoid (duplicating all of the git parameters with default(omit)).
    – Ryan O.
    Oct 21, 2020 at 21:21
  • Imagine the role is a dynamic library. "task_from" calls a single function (task) and not the whole library (role). You write the library (role) with the functions (tasks) once and then you call the functions (include role tasks_from). Oct 21, 2020 at 21:25
  • I see. Thank you for the explanation!
    – Ryan O.
    Oct 21, 2020 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.