可以将var_prompt包含在角色中吗? [英] can var_prompt be included in a role?
问题描述
我写了一个简单的剧本master.yml,主要功能是查找角色并输入一个作为第二本剧本的输入.但是即使我使用了var_prompt角色,它也不会提示输入变量.
I have written a simple play book master.yml,the main function is to lookup the roles and enter one as input for second playbook. But it doesn't prompt for the variables eventhough I used var_prompt role.
--- #master.yml
- name: show roles.
hosts: nodes
connection: ssh
gather_facts: no
tasks:
- include: showroles.yml
- name: createdb and map roles.
hosts: nodes
connection: ssh
gather_facts: no
roles:
- vars_prompt_role
tasks:
- include: createdb.yml
我的目录结构如下:
/playbooks/createdbandmaprole/
[user@localhost createdbandmaprole]$ ls
createdb.yml hosts master.yml roles script.sql.j2 showroles.yml
[user@localhost createdbandmaprole]$cd roles
[user@localhost roles]$ ls
var_prompt_role
[user@localhost roles]$ cd var_prompt_role/
[user@localhost var_prompt_role]$ ls
defaults handlers library main.yml meta tasks vars
[user@localhost var_prompt_role]$ vim main.yml
vars_prompt:
- name: "database"
prompt: "enter the name of database"
private: no
- name: "role"
prompt: "enter the name of role"
private: no
- name: "ad_group"
prompt: "enter the AD_Group"
private: no
[user@localhost createdbandmaprole]$ ansible-playbook master.yml -i hosts --check
但它不会提示输入vars
but it doesn't prompt for vars
推荐答案
vars_prompt
应该从剧本的顶层而不是任务上调用:
vars_prompt
should be called from the top level of the playbook, not a task:
这是最基本的示例:
Here is a most basic example:
---
- hosts: all
remote_user: root
vars:
from: "camelot"
vars_prompt:
- name: "name"
prompt: "what is your name?"
- name: "quest"
prompt: "what is your quest?"
- name: "favcolor"
prompt: "what is your favorite color?"
根据此页面.
如果您具有调用vars_prompt
的角色,则会在它自己的文件中定义在roles/
目录下的目录中,该目录应与您的剧本位于同一级别.在您的情况下,可能看起来像这样:
If you had a role to call vars_prompt
, it would be defined in its own file under a roles/
directory, which should live at the same level as your playbook. In your case, it might look like this:
--- #master.yml
...
- name: createdb and map roles.
hosts: nodes
roles:
- vars_prompt_role
...
然后在roles/vars_prompt_role/tasks/main.yml
中,您将定义vars_prompt_role
角色的任务.有关角色的更多信息,请查看此页面,或参见此页面,以获取有关内容组织的最佳做法.
Then in roles/vars_prompt_role/tasks/main.yml
, you would define the vars_prompt_role
role's tasks. For more information on roles, check out this page, or see this page for best practices on content organization.
这是您的roles/
目录的外观:
roles/
var_prompt_role/
files/
templates/
tasks/
main.yml
handlers/
vars/
defaults/
meta/
main.yml
是您要为此角色执行的主要任务.
Where main.yml
is the main task you want to carry out for this role.
这篇关于可以将var_prompt包含在角色中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!