Issue with using task files/playbook in ad hoc commands and in roles

Hi,

Newbie to Ansible, but I already made a lot of things with it.
One thing, though, I can’t do is re-use a task file for adhoc commands or re-use a playbook in another playbook :

For example, I use a task file (R_packages.yml) in a role, that starts like this :

  • name: Recuperation de la version de R installee
    win_shell: (Get-ItemProperty HKLM:Software\Microsoft\Windows\CurrentVersion\Uninstall* | Where-Object DisplayName -like “R for Win*”).DisplayVersion
    register: Rversion

I’d like to be able to launch it with an ad hoc command like :

ansible-playbook R_packages.yml …
But it doesn’t work :

ERROR! ‘register’ is not a valid attribute for a Play

I had to create a playbook to launch it :

  • hosts: fst-win-tp-bio
    gather_facts: “false”
    tasks:
  • name: Install packages R
    import_tasks: /etc/ansible/roles/fst-win-tp-bio/tasks/R_packages.yml

I didn’t fond anything in the doc that makes me understand how I could make an “hybrid” task/playbook file I could launch both ways

To be clear : This task file is part of a role, and I like to be able to use it from times to times on adhoc command.

Thanks for your help
E.T.

ansible-playbook is meant to run..... ansible playbooks :slight_smile:

What you have is a task file.
Playbooks contain tasks. Playbook can import/include tasks files.
To run a playbook you do need the playbook items such as hosts, tasks, etc.

I was hoping, maybe I could pass the missing items in ansible-playbook arguments.
The problem is the hosts item : in the playbook used to launch a task file, you tend to give a large scope to it, and I find it dangerous, if one forget the -l parameter in the adhoc command.
I was searching a way to avoid specify hosts in the playbook, what would oblige to pass it in arguments.

Thanks for your answer
E.T.