剧本中的 vars_prompt [英] Vars_prompt in playbooks

查看:24
本文介绍了剧本中的 vars_prompt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的剧本,

---
- hosts: alpha
  vars:
  company: vogo
  tasks:
     - name: debugging
       debug:
         msg: "{{ansible_hostname}}"
  vars_prompt:
     - name: "company"
       prompt: "Where do you work"
       private: no

- hosts: webservers
  vars_prompt:
     - name: "fathercompany"
       prompt: "Where your father works"
       private: no
  tasks:
     - name: test
       debug:
         msg: just testing "{{company2}}"

这是我运行剧本时的执行流程步骤,

Here are the steps of execution flow when I run the playbook,

1 - 提示 1(你在哪里工作)

1 - prompt 1 (Where do you work)

2 - 任务调试

3 - 提示 2(你父亲工作的地方)

3 - prompt 2 (Where your father works)

4 - 任务测试

我有一些问题.

首先,当我运行这个剧本时,调试"任务应该首先运行,然后提示应该询问公司名称.但是,当我运行这个剧本时,在第一步,它会询问你在哪里工作?".为什么先提示?我是否错过了我应该通过的某种参数?

First, When I run this playbook, the task "debugging" should run first and then the prompt should ask for the company name. But, when I run this playbook, at the very first step, it asks for "Where do you work ?". Why prompt first ? Am I missing some kind of paramater which I should have passed ?

其次,我在这里放了两个提示,

Second, I have put 2 prompts here,

我想在 webservers 主机块中使用提示值 company.但是当我尝试这样做时它给了我错误.

I want to use the prompt value company, in the webservers host block. But it gives me error when I try to do that.

我不能使用从一个主机块到另一个的提示值吗?

Cannot I use the prompt value from one host block into another ?

第三,

如何在角色中使用提示?

How can I use prompts in roles ?

推荐答案

vars_prompt 绑定到特定的播放,所以你不能直接在其他播放中使用它们.虽然您可以在第一场比赛中使用 set_fact 为某个主机设置一个事实,并在以后的比赛中通过 hostvars 访问它.

vars_prompt are bound to specific play, so you can't use them in other plays directly. Though you can use set_fact in the first play to set a fact for some host, and access it in later plays via hostvars.

提示在每次播放开始时执行,您将 vars_prompt 块放置在何处无关紧要 - 在 tasks 部分之前或之后(它是 YAML 字典之后全部,其中键的顺序没有任何意义).

Prompts are executed in the beginning of every play, it doesn't matter where you place vars_prompt block - before or after tasks section (it is a YAML dictionary after all, where sequence of keys doesn't mean anything).

我建议根本不要使用提示,如果您需要一些外部数据,请通过 额外变量.

I'd suggest not to use prompts at all, if you need some external data, pass it via extra variables.

这篇关于剧本中的 vars_prompt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆