Is there a way to reuse a generic task file but include different variables?

191 Views Asked by At

I'm new to ansible so please forgive any incorrect syntax errors. I have a simple playbook that installs packages

My project structure looks like the following:

TopDir
  dev
    |roles
    |  |ant
    |  |  |install
    |  |  |  tasks
    |  |  |    main.yml
    |  |  |vars
    |  |    main.yml
    |  |ant-junit
    |    |install
    |    |  tasks
    |    |    main.yml
    |    |vars
    |      main.yml
    |install_tools.yml
    

Playbook (install_tools.yml):

- name: Install tools
  hosts: hostA
  tags: ['hostA']
  become: yes
  roles:
    - ant/install
    - ant-junit/install
    - doxygen/install

Each install/main.yml looks exactly like this

- name: Include variables in vars
  include_vars: ../../vars/main.yml
  
- name: Install {{tool_name}}
  yum:
    name: "{{tool_name}}-{{tool_version}}"
    state: present

And in each vars/main.yml, i define tool_name and tool_version. So I would like to do something like the following but vars_files isn't a valid attribute for a TaskInclude. The idea is that generic_install will use the variables defined from vars_files

- name: Call generic install
  include_tasks: ../../../generic_install.yml
  vars_files: ../../vars/main.yml
1

There are 1 best solutions below

0
On

I was using the wrong attributes. The following worked for me. Note that I had to use import_task instead of include_tasks. Im not exactly sure of the differences, but it only worked with import_tasks and vars

- name: Call generic install
  import_tasks: ../../../generic_install.yml
  vars:
    tool_name: "{{tool_name}}"
    tool_version: "{{tool_version}}"