如何在ansible中使用include_vars [英] how to use include_vars in ansible
问题描述
我已经创建了自己的自定义库,我将自定义库添加到了存储库的common文件夹中.在这种情况下,我需要动态传递变量.这是一个机密密码,因此我在ansible中使用"vault".
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.
我的要求是如何在主机之前通过tasks\main.yml
中的include_vars
.
In that my requirement is how to pass include_vars
in the tasks\main.yml
before hosts.
例如:mytasks.yml
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
在这里,我需要使用sample_vault.yml
文件中传递的变量来运行sample_tasks文件,然后再使用ansible执行主机任务.如果我使用额外的变量,则意味着密码可见,因此我不需要.
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.
当我在tasks/main.yml
文件中使用include_vars
时,它显示以下错误:
When I use include_vars
in my tasks/main.yml
file, it shows the following error:
错误! "include_vars"不是Play的有效属性
ERROR! 'include_vars' is not a valid attribute for a Play
推荐答案
您不能以这种方式使用include_vars
,它只能在tasks
下使用.
如果sample_tasks.yml
是任务列表,则也不能在剧本级别使用它.请参阅我的其他答案以获取解释.
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. See my other answer for explanation.
您可以像这样使用vars_files
:
- hosts: localhost
vars_files:
- sample_vault.yml
tasks:
name: "free task"
command: ls -a
或将文件作为多余的变量传递:
Or pass a file as extra variables:
ansible-playbook --ask-vault-pass -e @sample_vault.yml myplaybook.yml
这篇关于如何在ansible中使用include_vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!