本文介绍了Ansible - 当有条件在剧本级别对 vars_prompt 不起作用时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ansible 2.1.2.0

ansible 2.1.2.0

我在这里遇到了一个情况,我试图与大家核对一下,看看使用 Ansible vars_prompt 功能和 when 条件是否可以解决该解决方案.

I have a situation here for which I'm trying to check with you all to see if the solution is even possible with Ansible vars_prompt feature and when conditional.

Ansible 有一个名为:vars_prompt 的功能,我想与when 条件(when: ... 我们通常在任务或剧本级动作的任务中使用)

Ansible has this feature called: vars_prompt and I want to club this with the when conditional (when: ... which we generally use in tasks at task or playbook level actions)

PS:我的问题不同于这篇文章:Ansible to Conditionally Prompt for a Variable? 即我的 vars: 变量不依赖于任何机器的 OS ENV 变量,就我而言,我只有vars: 变量的简单硬编码值.我还在主要剧本级别(而不是任务级别)运行 when 条件.

PS: My question is different than this post: Ansible to Conditionally Prompt for a Variable? i.e. my vars: variables are not dependent upon any machine's OS ENV variables, in my case, I just have a simple hardcoded value for vars: variables. I'm also running the when condition at the main playbook level (and not at the task's level).

我想做的是提示用户输入一个变量值,只有在命令行中,用户调用了 ansible-playbook install_tool.yml ... ... 使用 --extra-vars "install_tool=true"

What I'm trying to do is to prompt a user for entering a variable value, only if at command line, the user has called the ansible-playbook install_tool.yml ... ... with --extra-vars "install_tool=true"

workspace/ansible/install_tool.yml 我有:

In workspace/ansible/install_tool.yml I have:

- hosts: all
  become: true
  gather_facts: false

  vars:
    instance: test
    #install_tool: false
    company_api_url: DUMMY_INVALID_URL

  vars_prompt:
    - name: "company_api_url"
      prompt: "Enter Company API URL: "
      default: "https://{{ instance }}.company.com"
      private: no
      when: install_tool == "true" and company_api_url == "DUMMY_INVALID_URL"
      # or
      #when: ( "{{ install_tool }}" == "true" and "{{ company_api_url }}" == "DUMMY_INVALID_URL" )

  pre_tasks:
    - debug: msg="install_tool = {{ install_tool }} and instance = {{ instance }}" 
      when: install_tool == "true"

我的问题:
我怎样才能让上述条件提示工作(使用有效的替换 default 值作为提示变量)SKIP完全提示用户取决于 install_tool 变量的值?

My question:
How can I get the above conditional prompt working (with a valid substituted default value for a prompt variable) and also SKIP to prompt a user altogether depending upon the value of install_tool variable?

我的理解:在顶级剧本文件 (install_tool.yml) 中,我定义了一个变量 instance,其值为 test.为什么 vars_prompt 部分的 default 值没有选择 test 并在它在剧本级别定义时替换其值(--或者如果用户传递 ex: --extra-vars "instance=qa" 命令行中的 instance 变量?在这种情况下,我希望默认值为 [https://qa.company.com],我该怎么做)?在为任何语法运行它时,我没有看到任何错误.

My understanding:In top level playbook file (install_tool.yml), I'm defining a variable instance with value as test. Why the default value for the vars_prompt section did NOT pick test and substituted its value when it's defined at the playbook level (--OR what if a user is passing instance variable at command line for ex: --extra-vars "instance=qa"? In that case, I would want the default value as [https://qa.company.com], how can I do that)? I don't see any errors while running it for any syntax.

我得到以下输出(但我期待一个替代的 URL 值,例如:[https://test.company.com])并且它仍然是即使 install_tool 在命令行中设置或传递为 false 也会提示:

I'm getting the following output (but I was expecting a substituted URL value like: [https://test.company.com]) and it's still prompting even when install_tool was set or passed at command line as false:

运行:

$ ansible-playbook -i "localhost," --connection=local install_tool.yml --extra-vars "install_tool=false instance=qa"

$ ansible-playbook -i inventory -l localhost install_tool.yml --extra-vars "install_tool=false"
...
.....some lines here
...
PLAYBOOK: install_tool.yml *******************************************
1 plays in install_tool.yml
Enter Company API URL:  [https://{{ instance }}.company.com]: 

使用 when: ( "{{ install_tool }}" == "true" and ...) 条件在 vars_prompt 部分,在/for - 名称:company_api_url 变量,它没有因为任何语法问题等而出错(也就是说,Ansible 不认为它是错误的),然后 WHY,Ansible 仍在提示我输入上述 vars_prompts 变量,即使我正在传递 --extra-vars "install_tool=false" --OR-- 如果我取消注释行:install_tool: falsevars: 部分下?

As using when: ( "{{ install_tool }}" == "true" and ...) condition within vars_prompt section, under/for - name: company_api_url variable, it didn't error out for any syntax issues or etc (that means, Ansible doesn't think it's wrong), then WHY, Ansible is still prompting me to enter the above vars_prompts variable, even when I'm passing --extra-vars "install_tool=false" --OR-- if I uncomment the line: install_tool: false under vars: section?

PS: when: ... 条件在 pre_tasks 部分成功工作,即当 install_tool 为真时,它显示这两个变量,当 install_tool 设置为 false 时,它​​不会运行 - debug 步骤并跳过它(这是预期的).

PS: when: ... condition is successfully working for the pre_tasks section i.e. when install_tool is true, it shows the value of those 2 variables and when install_tool is set to false, then it does NOT run the - debug step and skips it (which is expected).

谢谢.

推荐答案

您可以检查提示变量的所有支持属性 这里.

You can check all supported attributes for prompted vars here.

when 语句.从来没有.

提示变量的模板默认值也是不支持.

Templated default values for prompted vars is not supported also.

防止 vars 提示的唯一方法是通过 extra_vars 传递有问题的变量.在您的示例中,如果您通过 -e company_api_url=my_custom_url,Ansible 将跳过提示.

The only way to prevent vars prompt is to pass the variable in question via extra_vars. In your example, if you pass -e company_api_url=my_custom_url, Ansible will skip prompting for it.

这篇关于Ansible - 当有条件在剧本级别对 vars_prompt 不起作用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:55