how to use include_vars in ansible

Question:

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 dynamically. It’s a confidential password, so I am 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

my directory structure like this:

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 don’t 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

Answer:

You can’t use include_vars this way, it’s only available for use under tasks.
If sample_tasks.yml is a list of tasks, you also can’t use it on playbook level.

You can use vars_files like this:


Or pass a file as extra variables:

Leave a Reply