Answers: <p>Q: <strong>&quot;<em>Override certain parameters that pass them into the include_role task.</em>&quot;</strong></p>
  <p>A: The parameter <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_role_module.html#parameter-tasks_from" rel="nofollow noreferrer"><em>tasks_from</em></a> serves this use-case. For example, create the task file</p>
  <pre class="lang-yaml prettyprint-override"><code>shell&gt; cat roles/git_cached/tasks/git.yml
  - git:
      repo: &quot;{{ params.repo }}&quot;
      dest: &quot;{{ params.dest }}&quot;
      recursive: &quot;{{ params.recursive|default(ommit) }}&quot;
      depth: &quot;{{ params.depth|default('1') }}&quot;
  </code></pre>
  <p>Then &quot;call&quot; this task</p>
  <pre class="lang-yaml prettyprint-override"><code>    - name: Download something via Git
        include_role:
          name: git_cached
          tasks_from: git.yml
        vars:
          params:
            repo: https://somerepo
            dest: /some/path
  </code></pre>
  <p>You might want to complete the task with all <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/git_module.html#ansible-builtin-git-deploy-software-or-files-from-git-checkouts" rel="nofollow noreferrer">git</a> parameters:</p>
  <ul>
  <li><p>The parameters <em>repo</em> and <em>dest</em> are required.</p>
  </li>
  <li><p>In some parameters, like <em>recursive</em>, 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.</p>
  </li>
  <li><p>Some parameters, like <em>depth</em>, 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.</p>
  </li>
  <li><p>It's a good idea to &quot;namespace&quot; the variables.</p>
  </li>
  <li><p>It's a good idea to put the default variables into roles/git_cached/defaults/main.yml</p>
  </li>
  </ul>
  
Answers: CC BY-SA 4.0
Answers: 2020-10-21T21:21:59.923Z
Answers: 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)).
Answers: Ryan O.
Answers: CC BY-SA 4.0
Answers: 2020-10-21T21:25:52.843Z
Answers: 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).
Answers: Vladimir Botka
Answers: CC BY-SA 4.0
Answers: 2020-10-21T21:38:24.323Z
Answers: I see. Thank you for the explanation!
Answers: Ryan O.
Answers: CC BY-SA 4.0
Answers: 2020-10-20T23:16:35.823Z
Answers: 2020-10-20T23:16:35.823Z
Answers: Vladimir Botka
Body: <p>I have a role that contains this task:</p>
  <pre><code>- name: Download something via Git
    include_role:
      name: git_cached
    vars:
      params:
        repo: https://somerepo
        dest: /some/path
  </code></pre>
  <p>I would really like to then have the git_cached role look like this:</p>
  <pre><code>- git: {{ params }}
  </code></pre>
  <p>I don't want to have to duplicate all of the parameters like this:</p>
  <pre><code>- git:
      repo: &quot;{{ repo }}&quot;
      dest: &quot;{{ dest }}&quot;
  </code></pre>
  <p>Because then, I have to do it for all the parameters, even the ones I don't typically use. I just want to basically &quot;override&quot; certain parameters that I pass into the include_role task.</p>
  <p>Is what I'm trying to do possible?</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-10-20T23:21:35.273Z
Comments: FWIW, see [example](https://github.com/vbotka/ansible-lib/blob/master/tasks/al_git_checkout.yml)
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2020-10-21T21:24:06.637Z
Comments: Vladimir, could you briefly explain how you think the example you posted helps in this case? Thanks!
Comments: Ryan O.
Comments: CC BY-SA 4.0
Comments: 2020-10-21T21:27:50.610Z
Comments: You're welcome! It's out of the scope if it's not self-explaining.
Comments: Vladimir Botka
Tags: <ansible>
Title: Pass Ansible parameters to include_role
UserDisplayName: Ryan O.
ThreadText: # Pass Ansible parameters to include_role
  
  (Asked by: username_0 on 2020-10-20)
  
  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?
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: FWIW, see [example](https://github.com/vbotka/ansible-lib/blob/master/tasks/al_git_checkout.yml)  
  > **username_0**: username_1, could you briefly explain how you think the example you posted helps in this case? Thanks!  
  > **username_1**: You're welcome! It's out of the scope if it's not self-explaining.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-10-20. Score: 3
  
  Q: **"*Override certain parameters that pass them into the include\_role task.*"**
  
  A: The parameter [*tasks\_from*](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_role_module.html#parameter-tasks_from) serves this use-case. For example, create the task file
  
  ```yaml
  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
  
  ```yaml
      - 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](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/git_module.html#ansible-builtin-git-deploy-software-or-files-from-git-checkouts) 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
  
  > **Comments:**
  > **username_0**: 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)).  
  > **username_1**: 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).  
  > **username_0**: I see. Thank you for the explanation!  
  
  ---
  
  