include_vars and include before hosts play i need to implement

Hi,

I have created my own custom library , i added my custom library in the common folder of my repository.In that i need to pass variables dyanamically .Its a confidential password,so iam using “vault” in ansible.

In that my requirement is how to pass include_vars in the tasks\main.yml before hosts.

e.g: mytasks.yml

  • include_vars: sample_vault.yml
  • include: sample_tasks.yml
  • hosts: localhost
    tasks:
    name: “free task”
    command: ls -a

my directory structure like this:

myfolder

  • common
    -library
    -my file.py

  • sample_tasks.yml

  • mytasks
    -mytasks.yml(my main master playbook file)
    -sample_vault.yml (note:i create this using vault for confidential purpose)

  • roles
    -myrole

Here i need to run sample_tasks file using a variables passed in sample_vault.yml file before i execute the hosts tasks using ansible.If i use extra variable means password is visible so i dont need that.

When i use include_vars in my tasks/main.yml file, it shows the following error:

“ERROR! ‘include_vars’ is not a valid attribute for a Play”

Thanks

No tasks can live outside of a play, except for include and this exception only applies when it is including plays.

include_vars must be inside a play.