HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Pass Ansible parameters to include_role

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
include_roleparametersansiblepass

Problem

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?

Solution

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

Context

StackExchange DevOps Q#12609, answer score: 3

Revisions (0)

No revisions yet.