本文介绍了var_prompt 可以包含在角色中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我写了一个简单的剧本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_prompt 应该从剧本的顶层调用,而不是任务:

vars_prompt should be called from the top level of the playbook, not a task:

这是一个最基本的例子:

---
- 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/ 目录的外观:

This is how your roles/ directory should look:

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 可以包含在角色中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 13:20